Skip to content
This repository was archived by the owner on Jul 13, 2020. It is now read-only.

Commit 7b86c85

Browse files
committed
revert back to former registerDynamic form to ensure a smooth upgrade path
1 parent 12f24cd commit 7b86c85

File tree

2 files changed

+36
-37
lines changed

2 files changed

+36
-37
lines changed

core/register-loader.js

+34-29
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ function createLoadRecord (state, key, registration) {
6767
instantiatePromise: undefined,
6868
dependencies: undefined,
6969
execute: undefined,
70+
executingRequire: false,
71+
7072
// underlying module object bindings
7173
moduleObj: undefined,
7274

@@ -210,7 +212,8 @@ function instantiate (loader, load, link, registry, state) {
210212
if (registration[2]) {
211213
link.moduleObj.default = {};
212214
link.moduleObj.__useDefault = true;
213-
link.execute = registration[1];
215+
link.executingRequire = registration[1];
216+
link.execute = registration[2];
214217
}
215218

216219
// process System.register declaration
@@ -452,48 +455,34 @@ RegisterLoader.prototype.register = function (key, deps, declare) {
452455

453456
// anonymous modules get stored as lastAnon
454457
if (declare === undefined) {
455-
state.lastRegister = [key, deps, false];
458+
state.lastRegister = [key, deps, undefined];
456459
}
457460

458461
// everything else registers into the register cache
459462
else {
460463
var load = state.records[key] || createLoadRecord(state, key, undefined);
461-
load.registration = [deps, declare, false];
464+
load.registration = [deps, declare, undefined];
462465
}
463466
};
464467

465468
/*
466469
* System.registerDyanmic
467470
*/
468-
RegisterLoader.prototype.registerDynamic = function (key, deps, execute) {
471+
RegisterLoader.prototype.registerDynamic = function (key, deps, executingRequire, execute) {
469472
var state = this[REGISTER_INTERNAL];
470473

471474
// anonymous modules get stored as lastAnon
472475
if (typeof key !== 'string') {
473-
state.lastRegister = [key, typeof deps === 'boolean' ? dynamicExecuteCompat(key, deps, execute) : deps, true];
476+
state.lastRegister = [key, deps, executingRequire];
474477
}
475478

476479
// everything else registers into the register cache
477480
else {
478481
var load = state.records[key] || createLoadRecord(state, key, undefined);
479-
load.registration = [deps, typeof execute === 'boolean' ? dynamicExecuteCompat(deps, execute, arguments[3]) : execute, true];
482+
load.registration = [deps, executingRequire, execute];
480483
}
481484
};
482485

483-
function dynamicExecuteCompat (deps, executingRequire, execute) {
484-
return function (require, exports, module) {
485-
// evaluate deps first
486-
if (!executingRequire)
487-
for (var i = 0; i < deps.length; i++)
488-
require(deps[i]);
489-
490-
// then run execution function
491-
// also provide backwards compat for no return value
492-
// previous 4 argument form of System.register had "this" as global value
493-
module.exports = execute.apply(global, arguments) || module.exports;
494-
};
495-
}
496-
497486
// ContextualLoader class
498487
// backwards-compatible with previous System.register context argument by exposing .id
499488
function ContextualLoader (loader, key) {
@@ -526,7 +515,7 @@ function ensureEvaluate (loader, load, link, registry, state, seen) {
526515

527516
// for ES loads we always run ensureEvaluate on top-level, so empty seen is passed regardless
528517
// for dynamic loads, we pass seen if also dynamic
529-
var err = doEvaluate(loader, load, link, registry, state, load.setters ? [] : seen || []);
518+
var err = doEvaluate(loader, load, link, registry, state, link.setters ? [] : seen || []);
530519
if (err) {
531520
clearLoadErrors(loader, load);
532521
throw err;
@@ -593,7 +582,7 @@ function doEvaluate (loader, load, link, registry, state, seen) {
593582
// ES System.register execute
594583
// "this" is null in ES
595584
if (link.setters) {
596-
err = doExecute(link.execute, nullContext);
585+
err = declarativeExecute(link.execute);
597586
}
598587
// System.registerDynamic execute
599588
// "this" is "exports" in CJS
@@ -609,11 +598,15 @@ function doEvaluate (loader, load, link, registry, state, seen) {
609598
return moduleObj.default;
610599
}
611600
});
612-
err = doExecute(link.execute, module.exports, [
613-
makeDynamicRequire(loader, load.key, link.dependencies, link.dependencyInstantiations, registry, state, seen),
614-
module.exports,
615-
module
616-
]);
601+
602+
var require = makeDynamicRequire(loader, load.key, link.dependencies, link.dependencyInstantiations, registry, state, seen);
603+
604+
// evaluate deps first
605+
if (!link.executingRequire)
606+
for (var i = 0; i < link.dependencies.length; i++)
607+
require(link.dependencies[i]);
608+
609+
err = dynamicExecute(link.execute, require, moduleObj.default, module);
617610
// __esModule flag extension support
618611
if (moduleObj.default && moduleObj.default.__esModule)
619612
for (var p in moduleObj.default)
@@ -645,9 +638,21 @@ function doEvaluate (loader, load, link, registry, state, seen) {
645638
var nullContext = {};
646639
if (Object.freeze)
647640
Object.freeze(nullContext);
648-
function doExecute (execute, context, args) {
641+
642+
function declarativeExecute (execute) {
643+
try {
644+
execute.call(nullContext);
645+
}
646+
catch (e) {
647+
return e;
648+
}
649+
}
650+
651+
function dynamicExecute (execute, require, exports, module) {
649652
try {
650-
execute.apply(context, args);
653+
var output = execute.call(global, require, exports, module);
654+
if (output !== undefined)
655+
module.exports = output;
651656
}
652657
catch (e) {
653658
return e;

docs/system-register-dynamic.md

+2-8
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ optional. This allows it to work as both a bundle transport format and a wrapper
1717
The format of `System.registerDynamic` is designed to closely match CommonJS:
1818

1919
```javascript
20-
System.registerDynamic('optional-name', ['unnormalized-dependency'], function (require, exports, module) {
20+
System.registerDynamic('optional-name', ['unnormalized-dependency'], true, function (require, exports, module) {
2121
// require is executing - the dependency is only executed when we hit this require
2222
// Note that we can only require modules that have already been declared through the dependencies
2323
// array above. This is because synchronous module instantiation is not supported in the loader.
@@ -37,10 +37,4 @@ System.registerDynamic('optional-name', ['unnormalized-dependency'], function (r
3737
```
3838

3939
The resultant `ModuleNamespace` object is taken to be the object with the `default` export equal to the `module.exports` value
40-
from the CommonJS module, and this object is created once execution has completed only.
41-
42-
The `default` binding is still made available to ES modules that might depend on this CommonJS module while execution is in progress,
43-
so that `import x from 'cjs'` will have a defined binding to `module.exports` even while `cjs` is still executing.
44-
45-
Once executed, the named exports are populated from the iterable properties of `module.exports` so that `import {readFile} from 'fs'`
46-
can work as long as `fs` is fully executed at the time of `readFile` being accessed.
40+
from the CommonJS module, with this property set to the exports object from the beginning of the linking phase.

0 commit comments

Comments
 (0)