phpv8 / v8js

V8 Javascript Engine for PHP — This PHP extension embeds the Google V8 Javascript Engine
http://pecl.php.net/package/v8js
MIT License
1.84k stars 200 forks source link

Module loader doesn't allow relative paths #343

Closed tillsanders closed 6 years ago

tillsanders commented 6 years ago

I've been trying to implement my own module loader (simulating the behaviour of node) using setModuleLoader() and setModuleNormaliser(). The callback I wrote for setModuleNormaliser() gets two arguments:

  1. base path of the current module
  2. the literate string provided to the require method

Now, if the required path is relative (e.g. ./some-sub-module.js), I have no way of finding this required file because my callback only knows the base path of the current module, but not the path of the current file.

stesie commented 6 years ago

You should use the normaliser callback to map from module name to filepath, then you can interpret relative paths in subsequent calls correctly.

See https://gist.github.com/stesie/c9143b98355295420470 for a Node.js module loader implementation. (that at least can load .js files; fails on .json files though)

chenos commented 6 years ago

Might as well try this:

https://github.com/chenos/v8js-module-loader