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')
?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With