简体   繁体   English

“命名空间”是systemJS中的一个导入

[英]“Namespacing” an import in systemJS

I want to use a library ip-address with SystemJS (note, this question may look similar but it is a different problem I ran into trying while trying to accomplish this task). 我想使用带有SystemJS的库ip-address (注意, 这个问题可能看起来很相似,但这是我在尝试完成此任务时遇到的另一个问题)。

The library ip-address depends on util-deprecate. 库ip-address依赖于util-deprecate。 It imports it as follows: 它导入如下:

var util = require('util');

And then uses it as follows: 然后使用如下:

Address4.prototype.toV6Group =
  util.deprecate(Address4.prototype.toGroup6,
    'deprecated: `toV6Group` has been renamed to `toGroup6`');

When I import ip-address in a node project as... 当我在节点项目中导入ip-address时...

var ipAddress = require('ip-address');

...then I don't get any problems. ......然后我没有遇到任何问题。

When I import ip-address in a SystemJS project... 当我在SystemJS项目中导入ip-address时......

System.import('ip-address');

...then I get an error: ...然后我收到一个错误:

util.deprecate is not a function

How can I configure SystemJS to perform this import? 如何配置SystemJS以执行此导入? Currently I am configuring it as so... 目前我正在配置它......

const map: any = {
  'ip-address':'vendor/ip-address',
  'util':'vendor/util-deprecate'
}

const packages: any = {
  'ip-address': {main:'ip-address.js'},
  'util': {main: 'browser'}
};

Just to save a lookup, the util-deprecate's browser.js file is here , it is exporting the deprecate function directly. 为了保存查找,util-deprecate的browser.js文件在这里 ,它直接导出deprecate函数。

Note, I can get this to work if I modify the ip-address module so that all calls are of the form: 注意,如果我修改ip-address模块​​以便所有调用都是以下形式,我可以使用它:

Address4.prototype.toV6Group =
  util(Address4.prototype.toGroup6,
    'deprecated: `toV6Group` has been renamed to `toGroup6`');

I'd rather not modify a 3rd-party library if I can avoid it however. 如果我可以避免它,我宁愿不修改第三方库。

Ok, it turns out the issue was that I thought the ip-address module was using util-deprecate . 好吧,原来问题是我认为 ip-address模块使用的是util-deprecate It turns out that the way the ip-address module was importing util... 事实证明, ip-address模块导入util的方式......

var util = require('util');

It was not importing util-deprecate but importing Node's built in package util . 它不是导入util-deprecate而是导入Node的内置包util So, in order for ip-address to truly use util-deprecate a change will have to be made to the ip-address module. 因此,为了使ip-address真正使用util-deprecate ,必须对ip-address模块进行更改。

Since you tagged with there's a pretty simple solution. 由于您使用标记, 有一个非常简单的解决方案。

Using jspm you can simply install ip-address right from npm using: 使用jspm您只需使用以下jspmnpm直接安装ip-address

jspm install npm:ip-address

which will do all the dependency management for you. 这将为您完成所有依赖管理。

I've tested this in the browser and node.js using the example code ip-address provides: 我已经在浏览器和node.js中测试了这个,使用示例代码ip-address提供:

import {Address6} from 'ip-address'

const address = new Address6('2001:0:ce49:7601:e866:efff:62c3:fffe');

console.log(address.isValid());  // true

const teredo = address.inspectTeredo();

console.log(teredo.client4);  // '157.60.0.1'

and it works totally fine. 它完全没问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM