Named register extra (#1855)

* named register extra

* use bundle: scheme

* document named register extra

* correct resolution adjustment
This commit is contained in:
Guy Bedford 2018-10-06 22:09:24 +02:00 committed by GitHub
parent cb25b39f57
commit b34c829086
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 62 additions and 2 deletions

View File

@ -40,6 +40,7 @@ The following [pluggable extras](dist/extras) are provided which can be dropped
* [AMD loading](dist/extras/amd.js) support (through `Window.define` which is created).
* [Global loading](dist/extras/global.js) support for loading global scripts and detecting the defined global as the default export. Useful for loading common library scripts from CDN like `System.import('//unpkg.com/lodash')`. _(Already included in the system.js loader build)_.
* [Named exports](dist/extras/named-exports.js) convenience extension support for global and AMD module formats (`import { x } from './global.js'` instead of `import G from './global.js'; G.x`)
* [Named register](dist/extras/named-register.js) supports `System.register('name', ...)` named bundles (deprecated in the main build), that can be imported through a `bundle:` scheme, `System.import('bundle:name')`
* [Transform loader](dist/extras/transform.js) support, using fetch and eval, supporting a hookable `loader.transform`
Since all loader features are hookable, custom extensions can be easily made following the same approach as the bundled extras. See the [hooks documentation](docs/hooks.md) for more information.

View File

@ -42,7 +42,7 @@ export function resolveIfNotPlainOrUrl (relUrl, parentUrl) {
}
else {
// resolving to :/ so pathname is the /... part
pathname = parentUrl.slice(parentProtocol.length + 1);
pathname = parentUrl.slice(parentProtocol.length + (parentUrl[parentProtocol.length] === '/'));
}
if (relUrl[0] === '/')

View File

@ -0,0 +1,25 @@
/*
* SystemJS named register extension
* Supports System.register('name', [..deps..], function (_export, _context) { ... })
*
* Names are resolved origin-relative, so
* System.register(['x']) must be imported as System.import('/x')
*/
(function () {
const systemJSPrototype = System.constructor.prototype;
const registerRegistry = Object.create(null);
const register = systemJSPrototype.register;
systemJSPrototype.register = function (name, deps, declare) {
if (typeof name !== 'string')
return register.apply(this, arguments);
registerRegistry['bundle:' + name] = [deps, declare];
};
const instantiate = systemJSPrototype.instantiate;
systemJSPrototype.instantiate = function (url, firstParentUrl) {
return registerRegistry[url] || instantiate.call(this, url, firstParentUrl);
};
})();

View File

@ -0,0 +1,11 @@
suite('Named System.register', function() {
test('Loading a named System.register bundle', function () {
return System.import('./fixtures/browser/named-bundle.js').then(function (m) {
assert.equal(Object.keys(m).length, 0);
return System.import('bundle:a');
})
.then(function (m) {
assert.equal(m.a, 'b');
});
});
});

23
test/fixtures/browser/named-bundle.js vendored Normal file
View File

@ -0,0 +1,23 @@
System.register('a', ['./b'], function (exports) {
var b;
return {
setters: [function (m) {
b = m.b;
}],
execute: function () {
exports({
a: b
});
}
};
});
System.register('b', [], function (exports) {
return {
execute: function () {
exports({
b: 'b'
});
}
};
});

View File

@ -33,7 +33,7 @@
}
};
const extras = ['amd', 'named-exports', 'translate'];
const extras = ['amd', 'named-exports', 'transform', 'named-register'];
function runNextExtra () {
mocha.suite.suites.shift();
const extra = extras.shift();