diff --git a/buildin/module.js b/buildin/module.js index 9f2e8139308..50aa3fc5d96 100644 --- a/buildin/module.js +++ b/buildin/module.js @@ -4,6 +4,22 @@ module.exports = function(module) { module.paths = []; // module.parent = undefined by default module.children = []; + Object.defineProperty(module, "exports", { + enumerable: true, + configurable: false, + get: function() { return module.e; }, + set: function(v) { return module.e = v; } + }); + Object.defineProperty(module, "loaded", { + enumerable: true, + configurable: false, + get: function() { return module.l; } + }); + Object.defineProperty(module, "id", { + enumerable: true, + configurable: false, + get: function() { return module.i; } + }); module.webpackPolyfill = 1; } return module; diff --git a/lib/ContextModule.js b/lib/ContextModule.js index a502d9494bc..05f03f60ce7 100644 --- a/lib/ContextModule.js +++ b/lib/ContextModule.js @@ -117,7 +117,7 @@ ContextModule.prototype.source = function() { "\treturn Object.keys(map);\n", "};\n", "webpackContext.resolve = webpackContextResolve;\n", - "module.exports = webpackContext;\n", + "module.e = webpackContext;\n", "webpackContext.id = " + this.id + ";\n" ]; } else if(this.blocks && this.blocks.length > 0) { @@ -160,7 +160,7 @@ ContextModule.prototype.source = function() { "webpackAsyncContext.keys = function webpackAsyncContextKeys() {\n", "\treturn Object.keys(map);\n", "};\n", - "module.exports = webpackAsyncContext;\n", + "module.e = webpackAsyncContext;\n", "webpackAsyncContext.id = " + this.id + ";\n" ]; } else { @@ -170,7 +170,7 @@ ContextModule.prototype.source = function() { "}\n", "webpackEmptyContext.keys = function() { return []; };\n", "webpackEmptyContext.resolve = webpackEmptyContext;\n", - "module.exports = webpackEmptyContext;\n", + "module.e = webpackEmptyContext;\n", "webpackEmptyContext.id = " + this.id + ";\n" ]; } diff --git a/lib/DelegatedModule.js b/lib/DelegatedModule.js index fe948392a3d..235fe762b71 100644 --- a/lib/DelegatedModule.js +++ b/lib/DelegatedModule.js @@ -47,7 +47,7 @@ DelegatedModule.prototype.source = function() { if(!sourceModule) { str = WebpackMissingModule.moduleCode(this.sourceRequest); } else { - str = "module.exports = (__webpack_require__(" + sourceModule.id + "))"; + str = "module.e = (__webpack_require__(" + sourceModule.id + "))"; switch(this.type) { case "require": str += "(" + JSON.stringify(this.request) + ");"; diff --git a/lib/DllModule.js b/lib/DllModule.js index 3d389667fe1..e9a155c0694 100644 --- a/lib/DllModule.js +++ b/lib/DllModule.js @@ -37,7 +37,7 @@ DllModule.prototype.build = function build(options, compilation, resolver, fs, c }; DllModule.prototype.source = function() { - return new RawSource("module.exports = __webpack_require__;"); + return new RawSource("module.e = __webpack_require__;"); }; DllModule.prototype.needRebuild = function needRebuild() { diff --git a/lib/ExternalModule.js b/lib/ExternalModule.js index 0ad7ef2650b..e22159a3f76 100644 --- a/lib/ExternalModule.js +++ b/lib/ExternalModule.js @@ -45,20 +45,20 @@ ExternalModule.prototype.source = function() { case "window": case "global": if(Array.isArray(request)) { - str = "(function() { module.exports = " + this.type + request.map(function(r) { + str = "(function() { module.e = " + this.type + request.map(function(r) { return "[" + JSON.stringify(r) + "]"; }).join("") + "; }());"; } else - str = "(function() { module.exports = " + this.type + "[" + JSON.stringify(request) + "]; }());"; + str = "(function() { module.e = " + this.type + "[" + JSON.stringify(request) + "]; }());"; break; case "commonjs": case "commonjs2": if(Array.isArray(request)) { - str = "module.exports = require(" + JSON.stringify(request[0]) + ")" + request.slice(1).map(function(r) { + str = "module.e = require(" + JSON.stringify(request[0]) + ")" + request.slice(1).map(function(r) { return "[" + JSON.stringify(r) + "]"; }).join("") + ";"; } else - str = "module.exports = require(" + JSON.stringify(request) + ");"; + str = "module.e = require(" + JSON.stringify(request) + ");"; break; case "amd": case "umd": @@ -67,14 +67,14 @@ ExternalModule.prototype.source = function() { if(this.optional) { str += "if(typeof __WEBPACK_EXTERNAL_MODULE_" + this.id + "__ === 'undefined') {" + WebpackMissingModule.moduleCode(request) + "}\n"; } - str += "module.exports = __WEBPACK_EXTERNAL_MODULE_" + this.id + "__;"; + str += "module.e = __WEBPACK_EXTERNAL_MODULE_" + this.id + "__;"; break; default: str = ""; if(this.optional) { str += "if(typeof " + request + " === 'undefined') {" + WebpackMissingModule.moduleCode(request) + "}\n"; } - str += "module.exports = " + request + ";"; + str += "module.e = " + request + ";"; break; } if(this.useSourceMap) { diff --git a/lib/MainTemplate.js b/lib/MainTemplate.js index db0ce909037..a3d4ceda84b 100644 --- a/lib/MainTemplate.js +++ b/lib/MainTemplate.js @@ -50,7 +50,7 @@ function MainTemplate(outputOptions) { source, "// Check if module is in cache", "if(installedModules[moduleId])", - this.indent("return installedModules[moduleId].exports;"), + this.indent("return installedModules[moduleId].e;"), "", "// Create a new module (and put it into the cache)", "var module = installedModules[moduleId] = {", @@ -58,20 +58,20 @@ function MainTemplate(outputOptions) { "};", "", "// Execute the module function", - "modules[moduleId].call(module.exports, module, module.exports, " + this.renderRequireFunctionForModule(hash, chunk, "moduleId") + ");", + "modules[moduleId].call(module.e, module, module.e, " + this.renderRequireFunctionForModule(hash, chunk, "moduleId") + ");", "", "// Flag the module as loaded", - "module.loaded = true;", + "module.l = true;", "", "// Return the exports of the module", - "return module.exports;" + "return module.e;" ]); }); this.plugin("module-obj", function( /*source, chunk, hash, varModuleId*/ ) { return this.asString([ - "exports: {},", - "id: moduleId,", - "loaded: false" + "e: {},", + "i: moduleId,", + "l: false" ]); }); this.plugin("require-extensions", function(source, chunk) { diff --git a/lib/MultiModule.js b/lib/MultiModule.js index e3c6281afd3..2b7941515d5 100644 --- a/lib/MultiModule.js +++ b/lib/MultiModule.js @@ -40,7 +40,7 @@ MultiModule.prototype.source = function(dependencyTemplates, outputOptions) { this.dependencies.forEach(function(dep, idx) { if(dep.module) { if(idx === this.dependencies.length - 1) - str.push("module.exports = "); + str.push("module.e = "); str.push("__webpack_require__("); if(outputOptions.pathinfo) str.push("/*! " + dep.request + " */"); diff --git a/lib/NodeStuffPlugin.js b/lib/NodeStuffPlugin.js index a03703acf75..e46d4bc0658 100644 --- a/lib/NodeStuffPlugin.js +++ b/lib/NodeStuffPlugin.js @@ -82,13 +82,22 @@ NodeStuffPlugin.prototype.apply = function(compiler) { this.state.module.warnings.push(new UnsupportedFeatureWarning(this.state.module, "require.extensions is not supported by webpack. Use a loader instead.")); return true; }); - compiler.parser.plugin("expression module.exports", ignore); - compiler.parser.plugin("expression module.loaded", ignore); - compiler.parser.plugin("expression module.id", ignore); + compiler.parser.plugin("expression module.loaded", function(expr) { + var dep = new ConstDependency("module.l", expr.range); + dep.loc = expr.loc; + this.state.current.addDependency(dep); + return true; + }); + compiler.parser.plugin("expression module.id", function(expr) { + var dep = new ConstDependency("module.i", expr.range); + dep.loc = expr.loc; + this.state.current.addDependency(dep); + return true; + }); compiler.parser.plugin("evaluate Identifier module.hot", function(expr) { return new BasicEvaluatedExpression().setBoolean(false).setRange(expr.range); }); - compiler.parser.plugin("expression module", function() { + compiler.parser.plugin("expression module", function(expr) { var moduleJsPath = path.join(__dirname, "..", "buildin", "module.js"); if(this.state.module.context) { moduleJsPath = path.relative(this.state.module.context, moduleJsPath); diff --git a/lib/dependencies/AMDDefineDependency.js b/lib/dependencies/AMDDefineDependency.js index 5ef7c78134b..6601eef6380 100644 --- a/lib/dependencies/AMDDefineDependency.js +++ b/lib/dependencies/AMDDefineDependency.js @@ -46,12 +46,12 @@ AMDDefineDependency.Template.prototype.apply = function(dep, source) { } var branch = (localModuleVar ? "l" : "") + (dep.arrayRange ? "a" : "") + (dep.objectRange ? "o" : "") + (dep.functionRange ? "f" : ""); var defs = { - f: ["var __WEBPACK_AMD_DEFINE_RESULT__;", "!(__WEBPACK_AMD_DEFINE_RESULT__ = #.call(exports, __webpack_require__, exports, module), __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__))"], - o: ["", "!(module.exports = #)"], - of: ["var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_RESULT__;", "!(__WEBPACK_AMD_DEFINE_FACTORY__ = (#), __WEBPACK_AMD_DEFINE_RESULT__ = (typeof __WEBPACK_AMD_DEFINE_FACTORY__ === 'function' ? (__WEBPACK_AMD_DEFINE_FACTORY__.call(exports, __webpack_require__, exports, module)) : __WEBPACK_AMD_DEFINE_FACTORY__), __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__))"], - af: ["var __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_DEFINE_RESULT__;", "!(__WEBPACK_AMD_DEFINE_ARRAY__ = #, __WEBPACK_AMD_DEFINE_RESULT__ = #.apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__), __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__))"], - ao: ["", "!(#, module.exports = #)"], - aof: ["var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_DEFINE_RESULT__;", "!(__WEBPACK_AMD_DEFINE_ARRAY__ = #, __WEBPACK_AMD_DEFINE_FACTORY__ = (#), __WEBPACK_AMD_DEFINE_RESULT__ = (typeof __WEBPACK_AMD_DEFINE_FACTORY__ === 'function' ? (__WEBPACK_AMD_DEFINE_FACTORY__.apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__)) : __WEBPACK_AMD_DEFINE_FACTORY__), __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__))"], + f: ["var __WEBPACK_AMD_DEFINE_RESULT__;", "!(__WEBPACK_AMD_DEFINE_RESULT__ = #.call(exports, __webpack_require__, exports, module), __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.e = __WEBPACK_AMD_DEFINE_RESULT__))"], + o: ["", "!(module.e = #)"], + of: ["var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_RESULT__;", "!(__WEBPACK_AMD_DEFINE_FACTORY__ = (#), __WEBPACK_AMD_DEFINE_RESULT__ = (typeof __WEBPACK_AMD_DEFINE_FACTORY__ === 'function' ? (__WEBPACK_AMD_DEFINE_FACTORY__.call(exports, __webpack_require__, exports, module)) : __WEBPACK_AMD_DEFINE_FACTORY__), __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.e = __WEBPACK_AMD_DEFINE_RESULT__))"], + af: ["var __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_DEFINE_RESULT__;", "!(__WEBPACK_AMD_DEFINE_ARRAY__ = #, __WEBPACK_AMD_DEFINE_RESULT__ = #.apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__), __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.e = __WEBPACK_AMD_DEFINE_RESULT__))"], + ao: ["", "!(#, module.e = #)"], + aof: ["var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_DEFINE_RESULT__;", "!(__WEBPACK_AMD_DEFINE_ARRAY__ = #, __WEBPACK_AMD_DEFINE_FACTORY__ = (#), __WEBPACK_AMD_DEFINE_RESULT__ = (typeof __WEBPACK_AMD_DEFINE_FACTORY__ === 'function' ? (__WEBPACK_AMD_DEFINE_FACTORY__.apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__)) : __WEBPACK_AMD_DEFINE_FACTORY__), __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.e = __WEBPACK_AMD_DEFINE_RESULT__))"], lf: ["var XXX;", "!(XXX = #.call(exports, __webpack_require__, exports, module))"], lo: ["var XXX;", "!(XXX = #)"], lof: ["var __WEBPACK_AMD_DEFINE_FACTORY__, XXX;", "!(__WEBPACK_AMD_DEFINE_FACTORY__ = (#), XXX = (typeof __WEBPACK_AMD_DEFINE_FACTORY__ === 'function' ? (__WEBPACK_AMD_DEFINE_FACTORY__.call(exports, __webpack_require__, exports, module)) : __WEBPACK_AMD_DEFINE_FACTORY__))"], diff --git a/lib/dependencies/CommonJsPlugin.js b/lib/dependencies/CommonJsPlugin.js index a04e86cee93..8929eb90c34 100644 --- a/lib/dependencies/CommonJsPlugin.js +++ b/lib/dependencies/CommonJsPlugin.js @@ -86,6 +86,12 @@ CommonJsPlugin.prototype.apply = function(compiler) { compiler.parser.plugin("evaluate typeof exports", function(expr) { return new BasicEvaluatedExpression().setString("object").setRange(expr.range); }); + compiler.parser.plugin("expression module.exports", function(expr) { + var dep = new ConstDependency("module.e", expr.range); + dep.loc = expr.loc; + this.state.current.addDependency(dep); + return true; + }); compiler.parser.apply( new CommonJsRequireDependencyParserPlugin(this.options), new RequireResolveDependencyParserPlugin(this.options) diff --git a/lib/node/NodeMainTemplatePlugin.js b/lib/node/NodeMainTemplatePlugin.js index 70e88d74202..0b87a61892d 100644 --- a/lib/node/NodeMainTemplatePlugin.js +++ b/lib/node/NodeMainTemplatePlugin.js @@ -37,7 +37,7 @@ NodeMainTemplatePlugin.prototype.apply = function(mainTemplate) { this.requireFn + ".oe = function(err) {", this.indent([ "process.nextTick(function() {", - this.indent("throw err;"), + this.indent("throw err; // catch this error by using System.import().catch()"), "});" ]), "};" diff --git a/test/cases/parsing/polyfill-module/index.js b/test/cases/parsing/polyfill-module/index.js new file mode 100644 index 00000000000..620834abaed --- /dev/null +++ b/test/cases/parsing/polyfill-module/index.js @@ -0,0 +1,8 @@ +it("should polyfill module", function() { + var m = module; + + (typeof m.id).should.be.not.eql("undefined"); + m.children.should.be.eql([]); + m.exports.should.be.eql({}); + m.loaded.should.be.eql(true); +}); \ No newline at end of file diff --git a/test/statsCases/chunks/expected.txt b/test/statsCases/chunks/expected.txt index f49bd0100dd..be4725c211a 100644 --- a/test/statsCases/chunks/expected.txt +++ b/test/statsCases/chunks/expected.txt @@ -1,10 +1,10 @@ -Hash: 55b4e00b04fd0c502b1f +Hash: e83bfb0ef3c6f854b7c5 Time: Xms Asset Size Chunks Chunk Names - bundle.js 4.42 kB 0 [emitted] main + bundle.js 4.4 kB 0 [emitted] main 1.bundle.js 250 bytes 1 [emitted] -2.bundle.js 107 bytes 2 [emitted] -3.bundle.js 202 bytes 3 [emitted] +2.bundle.js 122 bytes 2 [emitted] +3.bundle.js 232 bytes 3 [emitted] chunk {0} bundle.js (main) 73 bytes [rendered] > main [5] (webpack)/test/statsCases/chunks/index.js [0] (webpack)/test/statsCases/chunks/a.js 22 bytes {0} [built] diff --git a/test/statsCases/color-disabled/expected.txt b/test/statsCases/color-disabled/expected.txt index 46f19225ad9..25132a14fbc 100644 --- a/test/statsCases/color-disabled/expected.txt +++ b/test/statsCases/color-disabled/expected.txt @@ -1,6 +1,6 @@ Hash: 0fca0b37055d42f98682 Time: Xms Asset Size Chunks Chunk Names -main.js 1.41 kB 0 [emitted] main +main.js 1.37 kB 0 [emitted] main chunk {0} main.js (main) 0 bytes [rendered] [0] (webpack)/test/statsCases/color-disabled/index.js 0 bytes {0} [built] \ No newline at end of file diff --git a/test/statsCases/color-enabled-custom/expected.txt b/test/statsCases/color-enabled-custom/expected.txt index 4d1aa9d3c28..ff116d6d228 100644 --- a/test/statsCases/color-enabled-custom/expected.txt +++ b/test/statsCases/color-enabled-custom/expected.txt @@ -1,6 +1,6 @@ Hash: 0fca0b37055d42f98682 Time: Xms Asset Size Chunks Chunk Names -main.js 1.41 kB 0 [emitted] main +main.js 1.37 kB 0 [emitted] main chunk {0} main.js (main) 0 bytes [rendered] [0] (webpack)/test/statsCases/color-enabled-custom/index.js 0 bytes {0} [built] \ No newline at end of file diff --git a/test/statsCases/color-enabled/expected.txt b/test/statsCases/color-enabled/expected.txt index a49be8b17b1..ebe296897ef 100644 --- a/test/statsCases/color-enabled/expected.txt +++ b/test/statsCases/color-enabled/expected.txt @@ -1,6 +1,6 @@ Hash: 0fca0b37055d42f98682 Time: Xms Asset Size Chunks Chunk Names -main.js 1.41 kB 0 [emitted] main +main.js 1.37 kB 0 [emitted] main chunk {0} main.js (main) 0 bytes [rendered] [0] (webpack)/test/statsCases/color-enabled/index.js 0 bytes {0} [built] \ No newline at end of file diff --git a/test/statsCases/define-plugin/expected.txt b/test/statsCases/define-plugin/expected.txt index b0acdffe0c0..1e5ee972be1 100644 --- a/test/statsCases/define-plugin/expected.txt +++ b/test/statsCases/define-plugin/expected.txt @@ -1,15 +1,15 @@ -Hash: 9874287bb38ab21ad7571557d04931c0902dd6cd +Hash: 5f941d2606b3f10b3f0bb1cf3421f227a5b81cc6 Child - Hash: 9874287bb38ab21ad757 + Hash: 5f941d2606b3f10b3f0b Time: Xms Asset Size Chunks Chunk Names - main.js 1.46 kB 0 [emitted] main + main.js 1.41 kB 0 [emitted] main chunk {0} main.js (main) 24 bytes [rendered] [0] (webpack)/test/statsCases/define-plugin/index.js 24 bytes {0} [built] Child - Hash: 1557d04931c0902dd6cd + Hash: b1cf3421f227a5b81cc6 Time: Xms Asset Size Chunks Chunk Names - main.js 1.46 kB 0 [emitted] main + main.js 1.41 kB 0 [emitted] main chunk {0} main.js (main) 24 bytes [rendered] [0] (webpack)/test/statsCases/define-plugin/index.js 24 bytes {0} [built] \ No newline at end of file diff --git a/test/statsCases/external/expected.txt b/test/statsCases/external/expected.txt index 26e9b7797cf..c4330b4ed81 100644 --- a/test/statsCases/external/expected.txt +++ b/test/statsCases/external/expected.txt @@ -1,7 +1,7 @@ Hash: ea561aa543731a7f84b9 Time: Xms - Asset Size Chunks Chunk Names -main.js 1.54 kB 0 [emitted] main + Asset Size Chunks Chunk Names +main.js 1.5 kB 0 [emitted] main chunk {0} main.js (main) 59 bytes [rendered] [0] (webpack)/test/statsCases/external/index.js 17 bytes {0} [built] [1] external "test" 42 bytes {0} [not cacheable] \ No newline at end of file diff --git a/test/statsCases/preset-normal/expected.txt b/test/statsCases/preset-normal/expected.txt index 9a05bf53ee9..c64fbdf47e1 100644 --- a/test/statsCases/preset-normal/expected.txt +++ b/test/statsCases/preset-normal/expected.txt @@ -1,4 +1,4 @@ -Hash: 8ca45b6d16d1f1e8be32 +Hash: 677dbd916b1145147e69 Time: Xms chunk {0} main.js (main) 73 bytes [rendered] chunk {1} 1.js 54 bytes {0} [rendered] diff --git a/test/statsCases/preset-verbose/expected.txt b/test/statsCases/preset-verbose/expected.txt index 4b25ffb244d..12b79932a27 100644 --- a/test/statsCases/preset-verbose/expected.txt +++ b/test/statsCases/preset-verbose/expected.txt @@ -1,10 +1,10 @@ -Hash: 8ca45b6d16d1f1e8be32 +Hash: 677dbd916b1145147e69 Time: Xms Asset Size Chunks Chunk Names -main.js 4.42 kB 0 [emitted] main +main.js 4.39 kB 0 [emitted] main 1.js 250 bytes 1 [emitted] - 2.js 107 bytes 2 [emitted] - 3.js 202 bytes 3 [emitted] + 2.js 122 bytes 2 [emitted] + 3.js 232 bytes 3 [emitted] chunk {0} main.js (main) 73 bytes [rendered] [0] (webpack)/test/statsCases/preset-verbose/a.js 22 bytes {0} [built] cjs require ./a [5] (webpack)/test/statsCases/preset-verbose/index.js 1:0-14 diff --git a/test/statsCases/simple-more-info/expected.txt b/test/statsCases/simple-more-info/expected.txt index a5fd1ce198f..f7ed4c23b59 100644 --- a/test/statsCases/simple-more-info/expected.txt +++ b/test/statsCases/simple-more-info/expected.txt @@ -1,7 +1,7 @@ Hash: b85ca8f6c28cbbc3320d Time: Xms Asset Size Chunks Chunk Names -bundle.js 1.41 kB 0 [emitted] main +bundle.js 1.37 kB 0 [emitted] main chunk {0} bundle.js (main) 0 bytes [rendered] > main [0] (webpack)/test/statsCases/simple-more-info/index.js [0] (webpack)/test/statsCases/simple-more-info/index.js 0 bytes {0} [built] diff --git a/test/statsCases/simple/expected.txt b/test/statsCases/simple/expected.txt index ce44e082f0f..a0a35c7cbbe 100644 --- a/test/statsCases/simple/expected.txt +++ b/test/statsCases/simple/expected.txt @@ -1,6 +1,6 @@ Hash: b85ca8f6c28cbbc3320d Time: Xms Asset Size Chunks Chunk Names -bundle.js 1.41 kB 0 [emitted] main +bundle.js 1.37 kB 0 [emitted] main chunk {0} bundle.js (main) 0 bytes [rendered] [0] (webpack)/test/statsCases/simple/index.js 0 bytes {0} [built] \ No newline at end of file