js: require: don't use ~~/scripts/modules.js/

Directories inside ~~/scripts/ are now loaded as scripts, so don't use
it also for modules. Now there are no default module paths.

To compensate, we now try to run ~~/.init.js right after defaults.js,
so the user may extend the js init procedure via this script, e.g. for
adding default paths to mp.module_paths .
This commit is contained in:
Avi Halachmi (:avih) 2020-02-07 16:36:00 +02:00
parent 10a97f7cc3
commit 68a1b47d4d
2 changed files with 26 additions and 12 deletions

View File

@ -300,19 +300,19 @@ do work. In general, this is for mpv modules and not a node.js replacement.
A ``.js`` file extension is always added to ``id``, e.g. ``require("./foo")`` A ``.js`` file extension is always added to ``id``, e.g. ``require("./foo")``
will load the file ``./foo.js`` and return its ``exports`` object. will load the file ``./foo.js`` and return its ``exports`` object.
An id is relative (to the script which ``require``'d it) if it starts with An id which starts with ``./`` or ``../`` is relative to the script or module
``./`` or ``../``. Otherwise, it's considered a "top-level id" (CommonJS term). which ``require`` it. Otherwise it's considered a top-level id (CommonJS term).
Top level id is evaluated as absolute filesystem path if possible, e.g. ``/x/y`` Top-level id is evaluated as absolute filesystem path if possible, e.g. ``/x/y``
or ``~/x``. Otherwise, it's considered a global module id and searched at or ``~/x``. Otherwise it's considered a global module id and searched according
``scripts/modules.js/`` in mpv config dirs - in normal config search order. E.g. ``mp.module_paths`` in normal array order, e.g. ``require("x")`` tries to
``require("x")`` is searched as file ``x.js`` at those dirs, and id ``foo/x`` is load ``x.js`` at one of the array paths, and id ``foo/x`` tries to load ``x.js``
searched as file ``x.js`` inside dir ``foo`` at those dirs. inside dir ``foo`` at one of the paths.
Search paths for global module id's are at the array ``mp.module_paths``, which The ``mp.module_paths`` array is empty by default.
is searched in order. Initially it contains one item: ``~~/scripts/modules.js`` ``mp.module_paths`` may be updated from a script (preferably via custom init -
such that it behaves as described above. Modifying it will affect future see below) which will affect future calls to ``require`` for global module id's
``require`` calls with global module id's which are not already loaded/cached. which are not already loaded/cached.
No ``global`` variable, but a module's ``this`` at its top lexical scope is the No ``global`` variable, but a module's ``this`` at its top lexical scope is the
global object - also in strict mode. If you have a module which needs ``global`` global object - also in strict mode. If you have a module which needs ``global``
@ -320,6 +320,15 @@ as the global object, you could do ``this.global = this;`` before ``require``.
Functions and variables declared at a module don't pollute the global object. Functions and variables declared at a module don't pollute the global object.
Custom initialization
---------------------
After mpv initializes the JavaScript environment for a script but before it
loads the script - it tries to run the file ``.init.js`` at the root of the mpv
configuration directory. Code at this file can update the environment further
for all scripts. E.g. if it contains ``mp.module_paths.push("/foo")`` then
``require`` at all scripts will search global module id's also at ``/foo``.
The event loop The event loop
-------------- --------------

View File

@ -462,7 +462,7 @@ function process_timers() {
- Module id supports mpv path enhancements, e.g. ~/foo, ~~/bar, ~~desktop/baz - Module id supports mpv path enhancements, e.g. ~/foo, ~~/bar, ~~desktop/baz
*********************************************************************/ *********************************************************************/
mp.module_paths = ["~~/scripts/modules.js"]; // global modules search paths mp.module_paths = []; // global modules search paths
// Internal meta top-dirs. Users should not rely on these names. // Internal meta top-dirs. Users should not rely on these names.
var MODULES_META = "~~modules", var MODULES_META = "~~modules",
@ -740,3 +740,8 @@ g.mp_event_loop = function mp_event_loop() {
}; };
})(this) })(this)
try {
// let the user extend us, e.g. for updating mp.module_paths
require("~~/.init");
} catch(e) {}