-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixing bug - named AMD modules were instantiated twice. Resolves #2103. #2104
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ | |
throw Error('AMD require not supported.'); | ||
} | ||
|
||
let tmpRegister; | ||
let tmpRegister, firstNamedDefine; | ||
|
||
function emptyFn () {} | ||
|
||
|
@@ -88,6 +88,10 @@ | |
if (register && register[1] === lastRegisterDeclare) | ||
return register; | ||
|
||
// If the script registered a named module, return that module instead of re-instantiating it. | ||
if (firstNamedDefine) | ||
return firstNamedDefine; | ||
|
||
// otherwise AMD takes priority | ||
// no registration -> attempt AMD detection | ||
if (!amdDefineDeps) | ||
|
@@ -135,6 +139,13 @@ | |
global.define.amd = {}; | ||
|
||
function addToRegisterRegistry(name, define) { | ||
if (!firstNamedDefine) { | ||
firstNamedDefine = define; | ||
setTimeout(function () { | ||
firstNamedDefine = null; | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We shouldn't rely on a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought through how to do this without setTimeout earlier - one approach I considered to was to reset Could you explain how this |
||
} | ||
|
||
// We must call System.getRegister() here to give other extras, such as the named-exports extra, | ||
// a chance to modify the define before it's put into the registerRegistry. | ||
// See https://github.com/systemjs/systemjs/issues/2073 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ suite('Named System.register', function() { | |
|
||
test('Loading a named AMD bundle', function () { | ||
return System.import('./fixtures/browser/named-amd.js').then(function (m) { | ||
assert.equal(Object.keys(m).length, 2); | ||
assert.equal(m.a, 'b'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test was making a weird assertion that I think was wrong. On the master branch, With my changes in this PR, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems ok to me. |
||
return System.import('c'); | ||
}) | ||
.then(function (m) { | ||
|
@@ -51,4 +51,16 @@ suite('Named System.register', function() { | |
assert.ok(m.foo); | ||
}); | ||
}); | ||
|
||
test('loading named define module should work and should not instantiate it twice', function () { | ||
return System.import('fixtures/amd-named-single-execute.js').then(function (m) { | ||
assert.equal(m.default, 'The first named AMD module'); | ||
assert.equal(numNamedAMDExecutions, 1); | ||
}).then(function () { | ||
return System.import('fixtures/amd-named-single-execute.js').then(function (m) { | ||
assert.equal(m.default, 'The first named AMD module'); | ||
assert.equal(numNamedAMDExecutions, 1); | ||
}); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
var numNamedAMDExecutions = 0; | ||
define('named-amd-single-execute', ['exports'], function(exports) { | ||
numNamedAMDExecutions++; | ||
return 'The first named AMD module'; | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before this PR, we were calling
createAMDRegister()
multiple times for the same AMD module. This is problematic because (1) some modules rely on their code only being executed once, and (2) performance of running the execute function multiple times.