History

js: require: allow custom module search paths via mp.module_paths

Avi Halachmi (:avih) committed 6 years ago

Commit 4fc5cd31 parent 3d2e30d2 files · +18 −4Browse files

Unified · Split

2 files
  1. DOCS/man/javascript.rst +8 −0
  2. player/javascript/defaults.js +10 −4
DOCS/man/javascript.rst+8−0
@@ -240,6 +240,9 @@ Note: ``read_file`` and ``write_file`` throw on errors, allow text content only. · 239 unmodified lines
240 anything from the filesystem), and returns it as a function. Very similar240 anything from the filesystem), and returns it as a function. Very similar
241 to a ``Function`` constructor, but shows at stack traces as ``fname``.241 to a ``Function`` constructor, but shows at stack traces as ``fname``.
242 242
243``mp.module_paths``
244 Global modules search paths array for the ``require`` function (see below).
245
243Timers (global)246Timers (global)
244---------------247---------------
245 248
@@ -301,6 +304,11 @@ or ``~/x``. Otherwise, it's considered a global module id and searched at · 55 unmodified lines
301``require("x")`` is searched as file ``x.js`` at those dirs, and id ``foo/x`` is304``require("x")`` is searched as file ``x.js`` at those dirs, and id ``foo/x`` is
302searched as file ``x.js`` inside dir ``foo`` at those dirs.305searched as file ``x.js`` inside dir ``foo`` at those dirs.
303 306
307Search paths for global module id's are at the array ``mp.module_paths``, which
308is searched in order. Initially it contains one item: ``~~/scripts/modules.js``
309such that it behaves as described above. Modifying it will affect future
310``require`` calls with global module id's which are not already loaded/cached.
311
304No ``global`` variable, but a module's ``this`` at its top lexical scope is the312No ``global`` variable, but a module's ``this`` at its top lexical scope is the
305global object - also in strict mode. If you have a module which needs ``global``313global object - also in strict mode. If you have a module which needs ``global``
306as the global object, you could do ``this.global = this;`` before ``require``.314as the global object, you could do ``this.global = this;`` before ``require``.
player/javascript/defaults.js+10−4
@@ -375,6 +375,8 @@ function process_timers() { · 374 unmodified lines
375 - Module id supports mpv path enhancements, e.g. ~/foo, ~~/bar, ~~desktop/baz375 - Module id supports mpv path enhancements, e.g. ~/foo, ~~/bar, ~~desktop/baz
376 *********************************************************************/376 *********************************************************************/
377 377
378mp.module_paths = ["~~/scripts/modules.js"]; // global modules search paths
379
378// Internal meta top-dirs. Users should not rely on these names.380// Internal meta top-dirs. Users should not rely on these names.
379var MODULES_META = "~~modules",381var MODULES_META = "~~modules",
380 SCRIPTDIR_META = "~~scriptdir", // relative script path -> meta absolute id382 SCRIPTDIR_META = "~~scriptdir", // relative script path -> meta absolute id
@@ -389,10 +391,14 @@ function resolve_module_file(id) { · 8 unmodified lines
389 return mp.utils.join_path(main_script[0], rest);391 return mp.utils.join_path(main_script[0], rest);
390 392
391 if (base == MODULES_META) {393 if (base == MODULES_META) {
392 var path = mp.find_config_file("scripts/modules.js/" + rest);394 for (var i = 0; i < mp.module_paths.length; i++) {
393 if (!path)395 try {
394 throw(Error("Cannot find module file '" + rest + "'"));396 var f = mp.utils.join_path(mp.module_paths[i], rest);
395 return path;397 mp.utils.read_file(f, 1); // throws on any error
398 return f;
399 } catch (e) {}
400 }
401 throw(Error("Cannot find module file '" + rest + "'"));
396 }402 }
397 403
398 return id + ".js";404 return id + ".js";

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t