forked from systemjs/systemjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnamed-exports.js
57 lines (54 loc) · 2.2 KB
/
named-exports.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
* Named exports support for legacy module formats in SystemJS 2.0
*/
(function (global) {
const systemJSPrototype = global.System.constructor.prototype;
// hook System.register to know the last declaration binding
let lastRegisterDeclare;
const systemRegister = systemJSPrototype.register;
systemJSPrototype.register = function (name, deps, declare) {
lastRegisterDeclare = typeof name === 'string' ? declare : deps;
systemRegister.apply(this, arguments);
};
const getRegister = systemJSPrototype.getRegister;
systemJSPrototype.getRegister = function () {
const register = getRegister.call(this);
// if it is an actual System.register call, then its ESM
// -> dont add named exports
if (!register || register[1] === lastRegisterDeclare || register[1].length === 0)
return register;
// otherwise it was provided by a custom instantiator
// -> extend the registration with named exports support
const registerDeclare = register[1];
register[1] = function (_export, _context) {
// hook the _export function to note the default export
let defaultExport, hasDefaultExport = false;
const declaration = registerDeclare.call(this, function (name, value) {
if (typeof name === 'object' && name.__useDefault)
defaultExport = name.default, hasDefaultExport = true;
else if (name === 'default')
defaultExport = value;
else if (name === '__useDefault')
hasDefaultExport = true;
_export(name, value);
}, _context);
// hook the execute function
const execute = declaration.execute;
if (execute)
declaration.execute = function () {
execute.call(this);
// do a bulk export of the default export object
// to export all its names as named exports
if (hasDefaultExport && typeof defaultExport === 'object')
for (let name in defaultExport) {
// default is not a named export
if (name !== 'default') {
_export(name, defaultExport[name]);
}
}
};
return declaration;
};
return register;
};
})(typeof self !== 'undefined' ? self : global);