A way to map the relative path to the absolute in the SystemJS loader environment

I use the SystemJS loader (with commonJS modules, but this should not be important), mainly for direct access to components in node_modules .

Now at runtime, you can find the absolute path in the current environment from relative?

those. if I do require('./myComponent') it will extract http://localhost:3000/app/myComponent.js , but in case this component is installed via npm install (hence it is under node_modules ) SystemJS will be correct download http://localhost:3000/node_modules/dist/myComponent.js . Is there any way to find this absolute path from the relative?

those. something like var absPath = SystemJS.lookup('./myComponent') ?

+6
source share
1 answer

It is called normalize , and it returns a promise that resolves an absolute URL, as determined by the current SystemJS configuration:

 SystemJS.normalize('./myComponent').then(function(url) { }); 

Returning a promise means that there is no guarantee that it will not perform network booting — for example, if plugins or user loaders are involved, you may need to download plugins first.

In addition, it takes an optional second argument, the name of the parent module, because in the configuration you can define package-specific mappings.

+4
source

All Articles