diff --git a/lib/MainTemplate.js b/lib/MainTemplate.js index 3ca229a4510..bb9e2920b17 100644 --- a/lib/MainTemplate.js +++ b/lib/MainTemplate.js @@ -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 */ @@ -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 }); diff --git a/lib/dependencies/HarmonyExportImportedSpecifierDependency.js b/lib/dependencies/HarmonyExportImportedSpecifierDependency.js index dfaad404d61..2672e1ed76d 100644 --- a/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +++ b/lib/dependencies/HarmonyExportImportedSpecifierDependency.js @@ -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"; @@ -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"; } diff --git a/lib/dependencies/HarmonyExportSpecifierDependency.js b/lib/dependencies/HarmonyExportSpecifierDependency.js index 0883c1849dd..ee4f166fd9d 100644 --- a/lib/dependencies/HarmonyExportSpecifierDependency.js +++ b/lib/dependencies/HarmonyExportSpecifierDependency.js @@ -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); diff --git a/lib/dependencies/HarmonyImportDependency.js b/lib/dependencies/HarmonyImportDependency.js index 1d55d0b8ee4..4f2e46c9b0a 100644 --- a/lib/dependencies/HarmonyImportDependency.js +++ b/lib/dependencies/HarmonyImportDependency.js @@ -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 = ""; diff --git a/test/cases/parsing/issue-2618/index.js b/test/cases/parsing/issue-2618/index.js new file mode 100644 index 00000000000..1220384df7a --- /dev/null +++ b/test/cases/parsing/issue-2618/index.js @@ -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); +}); diff --git a/test/cases/parsing/issue-2618/module.js b/test/cases/parsing/issue-2618/module.js new file mode 100644 index 00000000000..003ba7cbee5 --- /dev/null +++ b/test/cases/parsing/issue-2618/module.js @@ -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"; diff --git a/test/cases/parsing/issue-2618/module2.js b/test/cases/parsing/issue-2618/module2.js new file mode 100644 index 00000000000..b8104da3df8 --- /dev/null +++ b/test/cases/parsing/issue-2618/module2.js @@ -0,0 +1 @@ +export var value3 = 123;