-
Notifications
You must be signed in to change notification settings - Fork 522
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1273 from stealjs/loads-twice-warning
Warn if a module is loaded at the same path twice
- Loading branch information
Showing
11 changed files
with
198 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* Extension to warn users when a module is instantiated twice | ||
* | ||
* Multiple module instantiation might cause unexpected side effects | ||
*/ | ||
addStealExtension(function(loader) { | ||
var superInstantiate = loader.instantiate; | ||
|
||
var warn = typeof console === "object" ? | ||
Function.prototype.bind.call(console.warn, console) : | ||
null; | ||
|
||
loader._instantiatedModules = loader._instantiatedModules || {}; | ||
|
||
loader.instantiate = function(load) { | ||
var instantiated = loader._instantiatedModules; | ||
|
||
if (warn && instantiated[load.address]) { | ||
var loads = (loader._traceData && loader._traceData.loads) || {}; | ||
var map = (loader._traceData && loader._traceData.parentMap) || {}; | ||
|
||
var parents = (map[load.name] ? Object.keys(map[load.name]) : []) | ||
.map(function(parent) { | ||
// module names might confuse people | ||
return "\t " + loads[parent].address; | ||
}) | ||
.join("\n"); | ||
|
||
warn( | ||
[ | ||
"The module with address " + load.address + | ||
" is being instantiated twice", | ||
"This happens when module identifiers normalize to different module names.\n", | ||
"HINT: Import the module using the ~/[modulePath] identifier" + | ||
(parents ? " in " : ""), | ||
(parents || "") + "\n", | ||
"Learn more at https://stealjs.com/docs/moduleName.html and " + | ||
"https://stealjs.com/docs/tilde.html" | ||
].join("\n") | ||
); | ||
} else { | ||
instantiated[load.address] = load; | ||
} | ||
|
||
return superInstantiate.apply(loader, arguments); | ||
}; | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>module is loaded twice with different paths</title> | ||
</head> | ||
<body> | ||
<script> | ||
window.assert = window.parent.assert; | ||
window.done = window.parent.done; | ||
|
||
window.WARN = Function.prototype.bind.call( | ||
window.console.warn, | ||
window.console | ||
); | ||
|
||
window.console.warn = function(msg) { | ||
window.assert.ok( | ||
/is being instantiated twice/.test(msg), | ||
"steal should warn users when module is instantiated twice" | ||
); | ||
window.WARN(msg); | ||
window.done(); | ||
}; | ||
</script> | ||
<script src="../../../steal-sans-promises.js" base-url="." config="package.json!npm"> | ||
|
||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
var foo = require("./foo"); | ||
var foo2 = require("foo"); | ||
|
||
if (!window || !window.assert) { | ||
console.log("foo: ", foo); | ||
console.log("foo2: ", foo2); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"name": "load_module_twice", | ||
"version": "0.0.1", | ||
"main": "main.js" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters