Skip to content

Commit

Permalink
smaller bundles by using module.e instead of module.exports
Browse files Browse the repository at this point in the history
same for module.loaded and module.id
  • Loading branch information
sokra committed Feb 13, 2016
1 parent 4f74648 commit 902220a
Show file tree
Hide file tree
Showing 22 changed files with 90 additions and 51 deletions.
16 changes: 16 additions & 0 deletions buildin/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions lib/ContextModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand All @@ -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"
];
}
Expand Down
2 changes: 1 addition & 1 deletion lib/DelegatedModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) + ");";
Expand Down
2 changes: 1 addition & 1 deletion lib/DllModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
12 changes: 6 additions & 6 deletions lib/ExternalModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand All @@ -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) {
Expand Down
14 changes: 7 additions & 7 deletions lib/MainTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,28 +50,28 @@ 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] = {",
this.indent(this.applyPluginsWaterfall("module-obj", "", chunk, hash, "moduleId")),
"};",
"",
"// 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) {
Expand Down
2 changes: 1 addition & 1 deletion lib/MultiModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 + " */");
Expand Down
17 changes: 13 additions & 4 deletions lib/NodeStuffPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions lib/dependencies/AMDDefineDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -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__))"],
Expand Down
6 changes: 6 additions & 0 deletions lib/dependencies/CommonJsPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/node/NodeMainTemplatePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()"),
"});"
]),
"};"
Expand Down
8 changes: 8 additions & 0 deletions test/cases/parsing/polyfill-module/index.js
Original file line number Diff line number Diff line change
@@ -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);
});
8 changes: 4 additions & 4 deletions test/statsCases/chunks/expected.txt
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
2 changes: 1 addition & 1 deletion test/statsCases/color-disabled/expected.txt
Original file line number Diff line number Diff line change
@@ -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]
2 changes: 1 addition & 1 deletion test/statsCases/color-enabled-custom/expected.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Hash: <CLR=BOLD>0fca0b37055d42f98682</CLR>
Time: <CLR=BOLD>X</CLR>ms
<CLR=BOLD>Asset</CLR> <CLR=BOLD>Size</CLR> <CLR=BOLD>Chunks</CLR> <CLR=39,BOLD><CLR=22> <CLR=BOLD>Chunk Names</CLR>
<CLR=32>main.js</CLR> 1.41 kB <CLR=BOLD>0</CLR> <CLR=32>[emitted]</CLR> main
<CLR=32>main.js</CLR> 1.37 kB <CLR=BOLD>0</CLR> <CLR=32>[emitted]</CLR> main
chunk {<CLR=33>0</CLR>} <CLR=32>main.js</CLR> (main) 0 bytes<CLR=32> [rendered]</CLR>
[0] <CLR=BOLD>(webpack)/test/statsCases/color-enabled-custom/index.js</CLR> 0 bytes {<CLR=33>0</CLR>}<CLR=32> [built]</CLR>
2 changes: 1 addition & 1 deletion test/statsCases/color-enabled/expected.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Hash: <CLR=BOLD>0fca0b37055d42f98682</CLR>
Time: <CLR=BOLD>X</CLR>ms
<CLR=BOLD>Asset</CLR> <CLR=BOLD>Size</CLR> <CLR=BOLD>Chunks</CLR> <CLR=39,BOLD><CLR=22> <CLR=BOLD>Chunk Names</CLR>
<CLR=32,BOLD>main.js</CLR> 1.41 kB <CLR=BOLD>0</CLR> <CLR=32,BOLD>[emitted]</CLR> main
<CLR=32,BOLD>main.js</CLR> 1.37 kB <CLR=BOLD>0</CLR> <CLR=32,BOLD>[emitted]</CLR> main
chunk {<CLR=33,BOLD>0</CLR>} <CLR=32,BOLD>main.js</CLR> (main) 0 bytes<CLR=32,BOLD> [rendered]</CLR>
[0] <CLR=BOLD>(webpack)/test/statsCases/color-enabled/index.js</CLR> 0 bytes {<CLR=33,BOLD>0</CLR>}<CLR=32,BOLD> [built]</CLR>
10 changes: 5 additions & 5 deletions test/statsCases/define-plugin/expected.txt
Original file line number Diff line number Diff line change
@@ -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]
4 changes: 2 additions & 2 deletions test/statsCases/external/expected.txt
Original file line number Diff line number Diff line change
@@ -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]
2 changes: 1 addition & 1 deletion test/statsCases/preset-normal/expected.txt
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
8 changes: 4 additions & 4 deletions test/statsCases/preset-verbose/expected.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/statsCases/simple-more-info/expected.txt
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
2 changes: 1 addition & 1 deletion test/statsCases/simple/expected.txt
Original file line number Diff line number Diff line change
@@ -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]

0 comments on commit 902220a

Please sign in to comment.