Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"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).

The library ip-address depends on 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...

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

...then I don't get any problems.

When I import ip-address in a SystemJS project...

System.import('ip-address');

...then I get an error:

util.deprecate is not a function

How can I configure SystemJS to perform this import? 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.

Note, I can get this to work if I modify the ip-address module so that all calls are of the form:

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.

like image 437
Pace Avatar asked Jul 22 '16 20:07

Pace


2 Answers

Ok, it turns out the issue was that I thought the ip-address module was using util-deprecate. It turns out that the way the ip-address module was importing util...

var util = require('util');

It was not importing util-deprecate but importing Node's built in package util. So, in order for ip-address to truly use util-deprecate a change will have to be made to the ip-address module.

like image 159
Pace Avatar answered Nov 05 '22 12:11

Pace


Since you tagged with jspm there's a pretty simple solution.

Using jspm you can simply install ip-address right from npm using:

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:

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.

like image 31
mash Avatar answered Nov 05 '22 12:11

mash