Skip to content

Commit

Permalink
Merge pull request #6583 from webpack/bugfix/import-cjs
Browse files Browse the repository at this point in the history
CJS fake namespace object contains exports now
  • Loading branch information
sokra committed Feb 26, 2018
2 parents 213226e + 8bf1574 commit 9f9c3d1
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 21 deletions.
4 changes: 2 additions & 2 deletions lib/ContextModule.js
Expand Up @@ -269,12 +269,12 @@ class ContextModule extends Module {
getReturn(type) {
if (type === 1) return "module";
if (type === 2)
return 'Object.assign({/* fake namespace object */}, module, { "default": module })';
return 'Object.assign({/* fake namespace object */}, typeof module === "object" && module, { "default": module })';
if (type === 0) {
if (this.options.namespaceObject === "strict") {
return '/* fake namespace object */ { "default": module }';
} else {
return '(typeof module === "object" && module && module.__esModule ? module : /* fake namespace object */ { "default": module })';
return '(typeof module === "object" && module && module.__esModule ? module : Object.assign({/* fake namespace object */}, typeof module === "object" && module, { "default": module }))';
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/RuntimeTemplate.js
Expand Up @@ -94,7 +94,7 @@ module.exports = class RuntimeTemplate {
} else {
return `Object(function() { var module = ${
rawModule
}; return typeof module === "object" && module && module.__esModule ? module : { /* fake namespace object */ "default": module }; }())`;
}; return typeof module === "object" && module && module.__esModule ? module : Object.assign({/* fake namespace object */}, typeof module === "object" && module, { "default": module }); }())`;
}
}

Expand Down Expand Up @@ -150,7 +150,7 @@ module.exports = class RuntimeTemplate {
} else {
getModuleFunction = `function() { ${header}var module = ${
rawModule
}; return typeof module === "object" && module && module.__esModule ? module : { /* fake namespace object */ "default": module }; }`;
}; return typeof module === "object" && module && module.__esModule ? module : Object.assign({/* fake namespace object */}, typeof module === "object" && module, { "default": module }); }`;
}

return `${promise || "Promise.resolve()"}.then(${getModuleFunction})`;
Expand Down
8 changes: 4 additions & 4 deletions test/cases/mjs/non-mjs-namespace-object-lazy/index.js
@@ -1,6 +1,6 @@
it("should receive a namespace object when importing commonjs", function(done) {
import("./cjs").then(function(result) {
result.should.be.eql({ default: { named: "named", default: "default" } });
result.should.be.eql({ named: "named", default: { named: "named", default: "default" } });
done();
}).catch(done);
});
Expand Down Expand Up @@ -60,9 +60,9 @@ function promiseTest(promise, equalsTo) {

it("should receive a namespace object when importing commonjs via context", function() {
return Promise.all([
promiseTest(contextCJS("one"), { default: { named: "named", default: "default" } }),
promiseTest(contextCJS("one"), { named: "named", default: { named: "named", default: "default" } }),
promiseTest(contextCJS("two"), { __esModule: true, named: "named", default: "default" }),
promiseTest(contextCJS("three"), { default: { named: "named", default: "default" } }),
promiseTest(contextCJS("three"), { named: "named", default: { named: "named", default: "default" } }),
promiseTest(contextCJS("null"), { default: null })
]);
});
Expand All @@ -77,7 +77,7 @@ it("should receive a namespace object when importing harmony via context", funct

it("should receive a namespace object when importing mixed content via context", function() {
return Promise.all([
promiseTest(contextMixed("one"), { default: { named: "named", default: "default" } }),
promiseTest(contextMixed("one"), { named: "named", default: { named: "named", default: "default" } }),
promiseTest(contextMixed("two"), { __esModule: true, named: "named", default: "default" }),
promiseTest(contextMixed("three"), { named: "named", default: "default" }),
promiseTest(contextMixed("null"), { default: null })
Expand Down
4 changes: 2 additions & 2 deletions test/statsCases/aggressive-splitting-entry/expected.txt
Expand Up @@ -5,7 +5,7 @@ Child fitting:
Built at: Thu Jan 01 1970 00:00:00 GMT
Asset Size Chunks Chunk Names
fb95acf7c457672e70d0.js 1.05 KiB 0 [emitted]
a1e683753eca705a0882.js 9.92 KiB 1 [emitted]
a1e683753eca705a0882.js 9.97 KiB 1 [emitted]
d43339a3d0f86c6b8d90.js 1.94 KiB 2 [emitted]
6c7fb52c5514dbfbf094.js 1.94 KiB 3 [emitted]
Entrypoint main = d43339a3d0f86c6b8d90.js 6c7fb52c5514dbfbf094.js a1e683753eca705a0882.js
Expand All @@ -31,7 +31,7 @@ Child content-change:
Built at: Thu Jan 01 1970 00:00:00 GMT
Asset Size Chunks Chunk Names
fb95acf7c457672e70d0.js 1.05 KiB 0 [emitted]
a1e683753eca705a0882.js 9.92 KiB 1 [emitted]
a1e683753eca705a0882.js 9.97 KiB 1 [emitted]
d43339a3d0f86c6b8d90.js 1.94 KiB 2 [emitted]
6c7fb52c5514dbfbf094.js 1.94 KiB 3 [emitted]
Entrypoint main = d43339a3d0f86c6b8d90.js 6c7fb52c5514dbfbf094.js a1e683753eca705a0882.js
Expand Down
2 changes: 1 addition & 1 deletion test/statsCases/import-weak/expected.txt
Expand Up @@ -3,7 +3,7 @@ Time: Xms
Built at: Thu Jan 01 1970 00:00:00 GMT
Asset Size Chunks Chunk Names
0.js 149 bytes 0 [emitted]
entry.js 7.63 KiB 1 [emitted] entry
entry.js 7.79 KiB 1 [emitted] entry
Entrypoint entry = entry.js
[0] ./modules/b.js 22 bytes {0} [built]
[1] ./entry.js 120 bytes {1} [built]
Expand Down
16 changes: 8 additions & 8 deletions test/statsCases/limit-chunk-count-plugin/expected.txt
Expand Up @@ -4,7 +4,7 @@ Child 1 chunks:
Time: Xms
Built at: Thu Jan 01 1970 00:00:00 GMT
Asset Size Chunks Chunk Names
bundle.js 5.52 KiB 0 [emitted] main
bundle.js 5.68 KiB 0 [emitted] main
Entrypoint main = bundle.js
chunk {0} bundle.js (main) 191 bytes <{0}> >{0}< [entry] [rendered]
[0] ./index.js 73 bytes {0} [built]
Expand All @@ -17,9 +17,9 @@ Child 2 chunks:
Hash: f90c7b21ae5c6e853a0d
Time: Xms
Built at: Thu Jan 01 1970 00:00:00 GMT
Asset Size Chunks Chunk Names
0.bundle.js 912 bytes 0 [emitted]
bundle.js 7.14 KiB 1 [emitted] main
Asset Size Chunks Chunk Names
0.bundle.js 1 KiB 0 [emitted]
bundle.js 7.19 KiB 1 [emitted] main
Entrypoint main = bundle.js
chunk {0} 0.bundle.js 118 bytes <{0}> <{1}> >{0}< [rendered]
[0] ./d.js 22 bytes {0} [built]
Expand All @@ -34,9 +34,9 @@ Child 3 chunks:
Time: Xms
Built at: Thu Jan 01 1970 00:00:00 GMT
Asset Size Chunks Chunk Names
0.bundle.js 774 bytes 0 [emitted]
0.bundle.js 886 bytes 0 [emitted]
1.bundle.js 245 bytes 1 [emitted]
bundle.js 7.14 KiB 2 [emitted] main
bundle.js 7.19 KiB 2 [emitted] main
Entrypoint main = bundle.js
chunk {0} 0.bundle.js 74 bytes <{0}> <{2}> >{0}< >{1}< [rendered]
[0] ./d.js 22 bytes {0} [built]
Expand All @@ -54,8 +54,8 @@ Child 4 chunks:
Asset Size Chunks Chunk Names
0.bundle.js 236 bytes 0 [emitted]
1.bundle.js 245 bytes 1 [emitted]
2.bundle.js 603 bytes 2 [emitted]
bundle.js 7.14 KiB 3 [emitted] main
2.bundle.js 715 bytes 2 [emitted]
bundle.js 7.19 KiB 3 [emitted] main
Entrypoint main = bundle.js
chunk {0} 0.bundle.js 44 bytes <{2}> <{3}> [rendered]
[0] ./d.js 22 bytes {0} [built]
Expand Down
4 changes: 2 additions & 2 deletions test/statsCases/named-chunks-plugin-async/expected.txt
Expand Up @@ -2,9 +2,9 @@ Hash: c5b0089a4015e8744f8e
Time: Xms
Built at: Thu Jan 01 1970 00:00:00 GMT
Asset Size Chunks Chunk Names
chunk-containing-__a_js.js 453 bytes chunk-containing-__a_js [emitted]
chunk-containing-__a_js.js 509 bytes chunk-containing-__a_js [emitted]
chunk-containing-__b_js.js 173 bytes chunk-containing-__b_js [emitted]
entry.js 7.17 KiB entry [emitted] entry
entry.js 7.28 KiB entry [emitted] entry
Entrypoint entry = entry.js
[0] ./modules/b.js 22 bytes {chunk-containing-__b_js} [built]
[1] ./modules/a.js 37 bytes {chunk-containing-__a_js} [built]
Expand Down

0 comments on commit 9f9c3d1

Please sign in to comment.