Import Map Utility
Generic ImportMap class utility for the manipulation and resolution of import maps, used by JSPM.
Getting Started
Installation
Node.js:
npm install @jspm/import-map
Usage
@jspm/import-map
only ships as an ES module.
example.mjs
import { ImportMap } from '@jspm/import-map';
const mapUrl = import.meta.url;
const map = new ImportMap({
mapUrl,
map: {
imports: {
react: 'https://cdn.com/react.js'
},
scopes: {
'https://site.com/': {
react: 'https://cdn.com/react2.js'
}
},
integrity: {
'https://cdn.com/react.js': 'sha384-...'
}
}
});
map.resolve('react') === 'https://cdn.com/react.js';
map.resolve('react', 'https://site.com/') === 'https://cdn.com/react2.js';
map.resolve('./hello.js', 'https://site.com/') === 'https://site.com/hello.js';
map.set('react', './custom-react.js');
map.resolve('react') === new URL('./custom-react.js', mapUrl).href;
map.set('react', './custom-react2.js', 'https://another.com/');
map.resolve('react', 'https://another.com/') === new URL('./custom-react2.js', mapUrl).href;
console.log(JSON.stringify(map.toJSON(), null, 2));
map.rebase('./map/');
console.log(JSON.stringify(map.toJSON(), null, 2));
map.set('react', '../custom-react.js', 'https://site.com/');
map.flatten();
console.log(JSON.stringify(map.toJSON(), null, 2));
map.replace('https://cdn.com/', 'https://cdn-mirror.com/');
map.replace('https://another.com/', 'https://another-site.com/');
console.log(JSON.stringify(map.toJSON(), null, 2));
console.log(new ImportMap({
map: {
scopes: {
"/": {
"pkg/a.js": "/pkg/a.js",
"pkg/b.js": "/pkg/b.js"
}
}
}
}).combineSubpaths().toJSON());
API
See src/map.ts.
Support is also provided for conditional maps supporting a way to manage generic maps for multiple environment targets, before serializing or resolving for exact environment targets.
License
MIT