Skip to content

Commit

Permalink
fixes webpack#2618
Browse files Browse the repository at this point in the history
This increases bundle size for non-es6-modules users.
But decreases size of es6-modules users.
We should fix this later.
  • Loading branch information
sokra committed Jun 7, 2016
1 parent d9e8ac0 commit 1ad7e7c
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 6 deletions.
23 changes: 23 additions & 0 deletions lib/MainTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ var Template = require("./Template");
* __webpack_require__.c = the module cache
* __webpack_require__.m = the module functions
* __webpack_require__.p = the bundle public path
* __webpack_require__.i = the identity function used for harmony imports
* __webpack_require__.e = the chunk ensure function
* __webpack_require__.d = the exported propery define getter function
* __webpack_require__.o = Object.prototype.hasOwnProperty.call
* __webpack_require__.h = the webpack hash
* __webpack_require__.oe = the uncatched error handler for the webpack runtime
*/

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

buf.push("");
buf.push("// define getter function for harmory exports");
buf.push(this.requireFn + ".d = function(exports, name, getter) {");
buf.push(this.indent([
"Object.defineProperty(exports, name, {",
this.indent([
"configurable: false,",
"enumerable: true,",
"get: getter"
]),
"});"
]));
buf.push("};");

buf.push("");
buf.push("// Object.prototype.hasOwnProperty.call");
buf.push(this.requireFn + ".o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };");

var publicPath = this.getPublicPath({
hash: hash
});
Expand Down
8 changes: 4 additions & 4 deletions lib/dependencies/HarmonyExportImportedSpecifierDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ HarmonyExportImportedSpecifierDependency.Template.prototype.apply = function(dep
var content;
var activeExports;
function getReexportStatement(key, valueKey) {
return (importIsHarmony || !valueKey ? "" : "if(Object.prototype.hasOwnProperty.call(" + name + ", " + valueKey + ")) ") +
"Object.defineProperty(exports, " + key + ", "+
"{configurable: false, enumerable: true, get: function() { return " + name + (valueKey === null ? "_default.a" : valueKey && "[" + valueKey + "]") + "; }});"
return (importIsHarmony || !valueKey ? "" : "if(__webpack_require__.o(" + name + ", " + valueKey + ")) ") +
"__webpack_require__.d(exports, " + key + ", "+
"function() { return " + name + (valueKey === null ? "_default.a" : valueKey && "[" + valueKey + "]") + "; });"
}
if(!used) { // we want to rexport something, but the export isn't used
content = "/* unused harmony reexport " + dep.name + " */\n";
Expand Down Expand Up @@ -96,7 +96,7 @@ HarmonyExportImportedSpecifierDependency.Template.prototype.apply = function(dep
content += "if(" + JSON.stringify(activeExports.concat("default")) + ".indexOf(__WEBPACK_IMPORT_KEY__) < 0) ";
else
content += "if(__WEBPACK_IMPORT_KEY__ !== 'default') ";
content += "(function(key) { Object.defineProperty(exports, key, {configurable: false, enumerable: true, get: function() { return " + name + "[key]; }}) }(__WEBPACK_IMPORT_KEY__));\n";
content += "(function(key) { __webpack_require__.d(exports, key, function() { return " + name + "[key]; }) }(__WEBPACK_IMPORT_KEY__));\n";
} else {
content = "/* unused harmony reexport namespace */\n";
}
Expand Down
2 changes: 1 addition & 1 deletion lib/dependencies/HarmonyExportSpecifierDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ HarmonyExportSpecifierDependency.Template.prototype.apply = function(dep, source
} else if(dep.immutable) {
content = "/* harmony export */ exports[" + JSON.stringify(used) + "] = " + dep.id + ";";
} else {
content = "\n/* harmony export */ Object.defineProperty(exports, " + JSON.stringify(used) + ", {configurable: false, enumerable: true, get: function() { return " + dep.id + "; }});";
content = "\n/* harmony export */ __webpack_require__.d(exports, " + JSON.stringify(used) + ", function() { return " + dep.id + "; });";
}
source.insert(dep.position, content);

Expand Down
2 changes: 1 addition & 1 deletion lib/dependencies/HarmonyImportDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ HarmonyImportDependency.makeStatement = function(declare, dep, outputOptions, re
content = "/* harmony import */ " + declaration + dep.importedVar + " = __webpack_require__(" + comment + JSON.stringify(dep.module.id) + ");" + newline;
if(!(dep.module.meta && dep.module.meta.harmonyModule)) {
content += "/* harmony import */ " + declaration + dep.importedVar + "_default = " + dep.importedVar + " && " + dep.importedVar + ".__esModule ? function() { return " + dep.importedVar + "['default'] } : function() { return " + dep.importedVar + "; };" + newline;
content += "/* harmony import */ Object.defineProperty(" + dep.importedVar + "_default, 'a', { get: " + dep.importedVar + "_default });" + newline;
content += "/* harmony import */ __webpack_require__.d(" + dep.importedVar + "_default, 'a', " + dep.importedVar + "_default);" + newline;
}
} else {
content = "";
Expand Down
9 changes: 9 additions & 0 deletions test/cases/parsing/issue-2618/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import defaultValue, { value, value2, value3, value4 } from "./module";

it("should be possible to redefine Object in a module", function() {
value.should.be.eql(123);
value2.should.be.eql(123);
value3.should.be.eql(123);
value4.should.be.eql(123);
defaultValue.should.be.eql(123);
});
7 changes: 7 additions & 0 deletions test/cases/parsing/issue-2618/module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var Object = 123;

export const value = Object;
export var value2 = Object;
export default Object;
export * from "./module2";
export { value3 as value4 } from "./module2";
1 change: 1 addition & 0 deletions test/cases/parsing/issue-2618/module2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export var value3 = 123;

0 comments on commit 1ad7e7c

Please sign in to comment.