Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Way to map a relative path to an absolute one in a SystemJS loader environment

I am using SystemJS loader (with commonJS modules, but that shouldn't be important), mostly to directly access components under node_modules.

Now at runtime, is it possible to lookup the absolute path in the current environment from a relative one?

I.e. if I do require('./myComponent') it will fetch http://localhost:3000/app/myComponent.js but in case this component is installed via npm install (hence resides under node_modules) SystemJS will correctly load http://localhost:3000/node_modules/dist/myComponent.js. Is there a way to lookup this absolute path from a relative one?

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

like image 783
Dynalon Avatar asked Jul 27 '16 08:07

Dynalon


1 Answers

It's called normalize, and it returns a promise that resolves to absolute URL, as defined by the current SystemJS configuration:

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

});

Returning a promise means there is no guarantee that it will not perform any network loading - for example, if plugins or custom loaders are involved, it might need to load plugins first.

Also, it takes optional second argument - name of the parent module, because one can define package-specific mappings in the configuration.

like image 193
artem Avatar answered Oct 22 '22 14:10

artem