systemjs/dist/extras/named-register.js
Joel Denning 2ff58c9e88 6.2.1
2020-02-06 09:46:48 -07:00

71 lines
2.2 KiB
JavaScript

/*
* SystemJS named register extension
* Supports System.register('name', [..deps..], function (_export, _context) { ... })
*
* Names are written to the registry as-is
* System.register('x', ...) can be imported as System.import('x')
*/
(function (global) {
const System = global.System;
setRegisterRegistry(System);
const systemJSPrototype = System.constructor.prototype;
const constructor = System.constructor;
const SystemJS = function () {
constructor.call(this);
setRegisterRegistry(this);
};
SystemJS.prototype = systemJSPrototype;
System.constructor = SystemJS;
let firstNamedDefine;
function setRegisterRegistry(systemInstance) {
systemInstance.registerRegistry = Object.create(null);
}
const _import = systemJSPrototype.import;
systemJSPrototype.import = function() {
firstNamedDefine = null;
return _import.apply(this, arguments);
};
const register = systemJSPrototype.register;
systemJSPrototype.register = function (name, deps, declare) {
if (typeof name !== 'string')
return register.apply(this, arguments);
const define = [deps, declare];
this.registerRegistry[name] = define;
if (!firstNamedDefine) {
firstNamedDefine = define;
}
return register.apply(this, arguments);
};
const resolve = systemJSPrototype.resolve;
systemJSPrototype.resolve = function (id, parentURL) {
try {
// Prefer import map (or other existing) resolution over the registerRegistry
return resolve.call(this, id, parentURL);
} catch (err) {
if (id in this.registerRegistry)
return id;
throw err;
}
};
const instantiate = systemJSPrototype.instantiate;
systemJSPrototype.instantiate = function (url, firstParentUrl) {
return this.registerRegistry[url] || instantiate.call(this, url, firstParentUrl);
};
const getRegister = systemJSPrototype.getRegister;
systemJSPrototype.getRegister = function () {
// Calling getRegister() because other extras need to know it was called so they can perform side effects
const register = getRegister.call(this);
const result = firstNamedDefine || register;
firstNamedDefine = null;
return result;
}
})(typeof self !== 'undefined' ? self : global);