在SystemJS加载器环境中将相对path映射到绝对path的方法
我正在使用SystemJS加载程序(与commonJS模块,但不应该是重要的),主要是直接访问node_modules
下的node_modules
。
现在在运行时,是否有可能从相对位置查找当前环境中的绝对path?
也就是说,如果我require('./myComponent')
,它将获取http://localhost:3000/app/myComponent.js
但如果通过npm install
(因此驻留在node_modules
下) npm install
该组件,SystemJS将正确加载http://localhost:3000/node_modules/dist/myComponent.js
。 有没有办法从相对的一个查找这个绝对path?
即类似var absPath = SystemJS.lookup('./myComponent')
?
它被称为normalize
,它返回一个承诺,parsing为绝对URL,由当前的SystemJSconfiguration定义:
SystemJS.normalize('./myComponent').then(function(url) { });
返回承诺意味着不保证它不会执行任何networking加载 – 例如,如果涉及插件或自定义加载器,则可能需要首先加载插件。
此外,它还需要可选的第二个参数 – 父模块的名称,因为可以在configuration中定义特定于软件包的映射。