Skip to content

Commit 1ad7e7c

Browse files
committed
fixes webpack#2618
This increases bundle size for non-es6-modules users. But decreases size of es6-modules users. We should fix this later.
1 parent d9e8ac0 commit 1ad7e7c

7 files changed

Lines changed: 46 additions & 6 deletions

File tree

‎lib/MainTemplate.js‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ var Template = require("./Template");
1212
* __webpack_require__.c = the module cache
1313
* __webpack_require__.m = the module functions
1414
* __webpack_require__.p = the bundle public path
15+
* __webpack_require__.i = the identity function used for harmony imports
16+
* __webpack_require__.e = the chunk ensure function
17+
* __webpack_require__.d = the exported propery define getter function
18+
* __webpack_require__.o = Object.prototype.hasOwnProperty.call
19+
* __webpack_require__.h = the webpack hash
1520
* __webpack_require__.oe = the uncatched error handler for the webpack runtime
1621
*/
1722

@@ -95,6 +100,24 @@ function MainTemplate(outputOptions) {
95100
buf.push("// identity function for calling harmory imports with the correct context");
96101
buf.push(this.requireFn + ".i = function(value) { return value; };");
97102

103+
buf.push("");
104+
buf.push("// define getter function for harmory exports");
105+
buf.push(this.requireFn + ".d = function(exports, name, getter) {");
106+
buf.push(this.indent([
107+
"Object.defineProperty(exports, name, {",
108+
this.indent([
109+
"configurable: false,",
110+
"enumerable: true,",
111+
"get: getter"
112+
]),
113+
"});"
114+
]));
115+
buf.push("};");
116+
117+
buf.push("");
118+
buf.push("// Object.prototype.hasOwnProperty.call");
119+
buf.push(this.requireFn + ".o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };");
120+
98121
var publicPath = this.getPublicPath({
99122
hash: hash
100123
});

‎lib/dependencies/HarmonyExportImportedSpecifierDependency.js‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ HarmonyExportImportedSpecifierDependency.Template.prototype.apply = function(dep
5454
var content;
5555
var activeExports;
5656
function getReexportStatement(key, valueKey) {
57-
return (importIsHarmony || !valueKey ? "" : "if(Object.prototype.hasOwnProperty.call(" + name + ", " + valueKey + ")) ") +
58-
"Object.defineProperty(exports, " + key + ", "+
59-
"{configurable: false, enumerable: true, get: function() { return " + name + (valueKey === null ? "_default.a" : valueKey && "[" + valueKey + "]") + "; }});"
57+
return (importIsHarmony || !valueKey ? "" : "if(__webpack_require__.o(" + name + ", " + valueKey + ")) ") +
58+
"__webpack_require__.d(exports, " + key + ", "+
59+
"function() { return " + name + (valueKey === null ? "_default.a" : valueKey && "[" + valueKey + "]") + "; });"
6060
}
6161
if(!used) { // we want to rexport something, but the export isn't used
6262
content = "/* unused harmony reexport " + dep.name + " */\n";
@@ -96,7 +96,7 @@ HarmonyExportImportedSpecifierDependency.Template.prototype.apply = function(dep
9696
content += "if(" + JSON.stringify(activeExports.concat("default")) + ".indexOf(__WEBPACK_IMPORT_KEY__) < 0) ";
9797
else
9898
content += "if(__WEBPACK_IMPORT_KEY__ !== 'default') ";
99-
content += "(function(key) { Object.defineProperty(exports, key, {configurable: false, enumerable: true, get: function() { return " + name + "[key]; }}) }(__WEBPACK_IMPORT_KEY__));\n";
99+
content += "(function(key) { __webpack_require__.d(exports, key, function() { return " + name + "[key]; }) }(__WEBPACK_IMPORT_KEY__));\n";
100100
} else {
101101
content = "/* unused harmony reexport namespace */\n";
102102
}

‎lib/dependencies/HarmonyExportSpecifierDependency.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ HarmonyExportSpecifierDependency.Template.prototype.apply = function(dep, source
3939
} else if(dep.immutable) {
4040
content = "/* harmony export */ exports[" + JSON.stringify(used) + "] = " + dep.id + ";";
4141
} else {
42-
content = "\n/* harmony export */ Object.defineProperty(exports, " + JSON.stringify(used) + ", {configurable: false, enumerable: true, get: function() { return " + dep.id + "; }});";
42+
content = "\n/* harmony export */ __webpack_require__.d(exports, " + JSON.stringify(used) + ", function() { return " + dep.id + "; });";
4343
}
4444
source.insert(dep.position, content);
4545

‎lib/dependencies/HarmonyImportDependency.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ HarmonyImportDependency.makeStatement = function(declare, dep, outputOptions, re
4040
content = "/* harmony import */ " + declaration + dep.importedVar + " = __webpack_require__(" + comment + JSON.stringify(dep.module.id) + ");" + newline;
4141
if(!(dep.module.meta && dep.module.meta.harmonyModule)) {
4242
content += "/* harmony import */ " + declaration + dep.importedVar + "_default = " + dep.importedVar + " && " + dep.importedVar + ".__esModule ? function() { return " + dep.importedVar + "['default'] } : function() { return " + dep.importedVar + "; };" + newline;
43-
content += "/* harmony import */ Object.defineProperty(" + dep.importedVar + "_default, 'a', { get: " + dep.importedVar + "_default });" + newline;
43+
content += "/* harmony import */ __webpack_require__.d(" + dep.importedVar + "_default, 'a', " + dep.importedVar + "_default);" + newline;
4444
}
4545
} else {
4646
content = "";
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import defaultValue, { value, value2, value3, value4 } from "./module";
2+
3+
it("should be possible to redefine Object in a module", function() {
4+
value.should.be.eql(123);
5+
value2.should.be.eql(123);
6+
value3.should.be.eql(123);
7+
value4.should.be.eql(123);
8+
defaultValue.should.be.eql(123);
9+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
var Object = 123;
2+
3+
export const value = Object;
4+
export var value2 = Object;
5+
export default Object;
6+
export * from "./module2";
7+
export { value3 as value4 } from "./module2";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export var value3 = 123;

0 commit comments

Comments
 (0)