Skip to content

Commit bad4250

Browse files
committed
added a simple mode for a single module
later this can be optimized by ast compression
1 parent d673a7c commit bad4250

File tree

20 files changed

+100
-78
lines changed

20 files changed

+100
-78
lines changed

examples/harmony-unused-inline/README.md

Lines changed: 13 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -51,47 +51,19 @@ export { add as reexportedAdd, multiply as reexportedMultiply } from "./math";
5151

5252
``` javascript
5353
/******/ (function(modules) { // webpackBootstrap
54-
/******/ // The module cache
55-
/******/ var installedModules = {};
54+
/******/ // Context information
55+
/******/ var __webpack_require__ = {};
5656

57-
/******/ // The require function
58-
/******/ function __webpack_require__(moduleId) {
5957

60-
/******/ var module = installedModules[moduleId];
61-
62-
/******/ // Check if module is in cache
63-
/******/ if(!module) {
64-
65-
/******/ // Create a new module (and put it into the cache)
66-
/******/ module = installedModules[moduleId] = {
67-
/******/ e: {},
68-
/******/ i: moduleId,
69-
/******/ l: false
70-
/******/ };
71-
72-
/******/ // Execute the module function
73-
/******/ modules[moduleId].call(module.e, module, module.e, __webpack_require__);
74-
75-
/******/ // Flag the module as loaded
76-
/******/ module.l = true;
77-
/******/ }
78-
79-
/******/ // Return the exports of the module
80-
/******/ return module.e;
81-
/******/ }
82-
83-
84-
/******/ // expose the modules object (__webpack_modules__)
85-
/******/ __webpack_require__.m = modules;
86-
87-
/******/ // expose the module cache
88-
/******/ __webpack_require__.c = installedModules;
8958

9059
/******/ // __webpack_public_path__
9160
/******/ __webpack_require__.p = "js/";
9261

9362
/******/ // Load entry module and return exports
94-
/******/ return __webpack_require__(__webpack_require__.s = 0);
63+
/******/ var __webpack_module__ = {l: false, i: 0, e: {}};
64+
/******/ modules[0].call(__webpack_module__.e, __webpack_module__, __webpack_module__.e, __webpack_require__);
65+
/******/ __webpack_module__.l = true;
66+
/******/ return __webpack_module__.e;
9567
/******/ })
9668
/************************************************************************/
9769
/******/ ([
@@ -102,7 +74,7 @@ export { add as reexportedAdd, multiply as reexportedMultiply } from "./math";
10274
!*** inlined: ./library.js ***!
10375
!*** ./example.js ***!
10476
\********************/
105-
/***/ function(module, exports, __webpack_require__) {
77+
/***/ function(module, exports) {
10678

10779
/*!****************!*\
10880
!*** ./abc.js ***!
@@ -175,7 +147,7 @@ export { add as reexportedAdd, multiply as reexportedMultiply } from "./math";
175147
# js/output.js
176148

177149
``` javascript
178-
!function(n){function r(a){var t=e[a];return t||(t=e[a]={e:{},i:a,l:!1},n[a].call(t.e,t,t.e,r),t.l=!0),t.e}var e={};return r.m=n,r.c=e,r.p="js/",r(r.s=0)}([function(n,r,e){var a={};(function(n,r){n.l=!0}).call(a,{e:a,i:1,l:!1},a);var t={};(function(n,r){function e(){for(var n=0,r=0,e=arguments,a=e.length;a>r;)n+=e[r++];return n}function a(){for(var n=1,r=0,e=arguments,a=e.length;a>r;)n*=e[r++];return n}r.a=e,r.b=a,n.l=!0}).call(t,{e:t,i:3,l:!1},t);var l={};(function(n,r){var e=t;Object.defineProperty(r,"a",{configurable:!1,enumerable:!0,get:function(){return e.b}}),n.l=!0}).call(l,{e:l,i:2,l:!1},l);var u=t,i=l;u.a.bind()(1,2),i.a.bind()(1,2)}]);
150+
!function(n){var r={};r.p="js/";var e={l:!1,i:0,e:{}};return n[0].call(e.e,e,e.e,r),e.l=!0,e.e}([function(n,r){var e={};(function(n,r){n.l=!0}).call(e,{e:e,i:1,l:!1},e);var a={};(function(n,r){function e(){for(var n=0,r=0,e=arguments,a=e.length;a>r;)n+=e[r++];return n}function a(){for(var n=1,r=0,e=arguments,a=e.length;a>r;)n*=e[r++];return n}r.a=e,r.b=a,n.l=!0}).call(a,{e:a,i:3,l:!1},a);var l={};(function(n,r){var e=a;Object.defineProperty(r,"a",{configurable:!1,enumerable:!0,get:function(){return e.b}}),n.l=!0}).call(l,{e:l,i:2,l:!1},l);var t=a,i=l;t.a.bind()(1,2),i.a.bind()(1,2)}]);
179151
```
180152
181153
# Info
@@ -185,9 +157,9 @@ export { add as reexportedAdd, multiply as reexportedMultiply } from "./math";
185157
```
186158
Hash: 263715f87c915965d5e3
187159
Version: webpack 2.0.7-beta
188-
Time: 117ms
189-
Asset Size Chunks Chunk Names
190-
output.js 4 kB 0 [emitted] main
160+
Time: 187ms
161+
Asset Size Chunks Chunk Names
162+
output.js 3.27 kB 0 [emitted] main
191163
chunk {0} output.js (main) 728 bytes [rendered]
192164
> main [0] ./example.js
193165
[0] ./example.js 121 bytes {0} [built]
@@ -205,9 +177,9 @@ chunk {0} output.js (main) 728 bytes [rendered]
205177
```
206178
Hash: 263715f87c915965d5e3
207179
Version: webpack 2.0.7-beta
208-
Time: 193ms
180+
Time: 251ms
209181
Asset Size Chunks Chunk Names
210-
output.js 653 bytes 0 [emitted] main
182+
output.js 592 bytes 0 [emitted] main
211183
chunk {0} output.js (main) 728 bytes [rendered]
212184
> main [0] ./example.js
213185
[0] ./example.js 121 bytes {0} [built]

lib/APIPlugin.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ var REPLACEMENTS = {
1818
__non_webpack_require__: "require", // eslint-disable-line camelcase
1919
"require.onError": "__webpack_require__.oe" // eslint-disable-line camelcase
2020
};
21+
var REQUIRED_API = {
22+
__webpack_require__: ["__webpack_require__", "__webpack_require__()"], // eslint-disable-line camelcase
23+
__webpack_public_path__: ["__webpack_require__"], // eslint-disable-line camelcase
24+
__webpack_modules__: ["__webpack_require__"], // eslint-disable-line camelcase
25+
__webpack_chunk_load__: ["__webpack_require__"], // eslint-disable-line camelcase
26+
__non_webpack_require__: [], // eslint-disable-line camelcase
27+
"require.onError": ["__webpack_require__"] // eslint-disable-line camelcase
28+
};
2129
var REPLACEMENT_TYPES = {
2230
__webpack_public_path__: "string", // eslint-disable-line camelcase
2331
__webpack_require__: "function", // eslint-disable-line camelcase
@@ -33,7 +41,7 @@ APIPlugin.prototype.apply = function(compiler) {
3341
Object.keys(REPLACEMENTS).forEach(function(key) {
3442
compiler.parser.plugin("expression " + key, function(expr) {
3543
var dep = new ConstDependency(REPLACEMENTS[key], expr.range);
36-
dep.requiredApi = ["__webpack_require__"];
44+
dep.requiredApi = REQUIRED_API[key];
3745
dep.loc = expr.loc;
3846
this.state.current.addDependency(dep);
3947
return true;

lib/Dependency.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Dependency.prototype.getReference = function() {
2020
}
2121
};
2222

23-
Dependency.prototype.requiredApi = ["module", "exports", "__webpack_require__"];
23+
Dependency.prototype.requiredApi = ["module", "exports", "__webpack_require__", "__webpack_require__()"];
2424

2525
Dependency.prototype.updateHash = function(hash) {
2626
hash.update((this.module && this.module.id) + "");

lib/ExtendedAPIPlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ ExtendedAPIPlugin.prototype.apply = function(compiler) {
2020
compiler.plugin("compilation", function(compilation) {
2121
compilation.dependencyFactories.set(ConstDependency, new NullFactory());
2222
compilation.dependencyTemplates.set(ConstDependency, new ConstDependency.Template());
23-
compilation.mainTemplate.plugin("require-extensions", function(source, chunk, hash) {
23+
compilation.mainTemplate.plugin("simple-require-extensions", function(source, chunk, hash) {
2424
var buf = [source];
2525
buf.push("");
2626
buf.push("// __webpack_hash__");

lib/HotModuleReplacementPlugin.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ HotModuleReplacementPlugin.prototype.apply = function(compiler) {
137137
this.assets[filename] = source;
138138
});
139139

140+
compilation.mainTemplate.plugin("simple-mode", function() {
141+
return false;
142+
});
143+
140144
compilation.mainTemplate.plugin("hash", function(hash) {
141145
hash.update("HotMainTemplateDecorator");
142146
});

lib/MainTemplate.js

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,23 @@ function MainTemplate(outputOptions) {
2626
}
2727
return this.asString(buf);
2828
});
29+
this.plugin("simple-startup", function(source, chunk, hash) {
30+
var buf = [];
31+
if(chunk.entryModule) {
32+
buf.push("// init module object");
33+
buf.push("var __webpack_module__ = {l: false, i: " + JSON.stringify(chunk.entryModule.id) + ", e: {}};");
34+
buf.push("");
35+
buf.push("// execute module");
36+
buf.push("modules[" + JSON.stringify(chunk.entryModule.id) + "].call(__webpack_module__.e, __webpack_module__, __webpack_module__.e, " + this.requireFn + ");");
37+
buf.push("");
38+
buf.push("// set loaded to true");
39+
buf.push("__webpack_module__.l = true;");
40+
buf.push("");
41+
buf.push("// return exports");
42+
buf.push("return __webpack_module__.e;");
43+
}
44+
return this.asString(buf);
45+
});
2946
this.plugin("render", function(bootstrapSource, chunk, hash, moduleTemplate, dependencyTemplates) {
3047
var source = new ConcatSource();
3148
source.add("/******/ (function(modules) { // webpackBootstrap\n");
@@ -88,7 +105,7 @@ function MainTemplate(outputOptions) {
88105
]);
89106
});
90107
this.plugin("require-extensions", function(source, chunk, hash) {
91-
var buf = [];
108+
var buf = [source];
92109
if(chunk.chunks.length > 0) {
93110
buf.push("// This file contains only the entry chunk.");
94111
buf.push("// The chunk loading function for additional chunks");
@@ -104,6 +121,11 @@ function MainTemplate(outputOptions) {
104121
buf.push("// expose the module cache");
105122
buf.push(this.requireFn + ".c = installedModules;");
106123

124+
return this.asString(buf);
125+
});
126+
this.plugin("simple-require-extensions", function(source, chunk, hash) {
127+
var buf = [source];
128+
107129
var publicPath = this.getPublicPath({
108130
hash: hash
109131
});
@@ -118,18 +140,34 @@ module.exports = MainTemplate;
118140
MainTemplate.prototype = Object.create(Template.prototype);
119141
MainTemplate.prototype.requireFn = "__webpack_require__";
120142
MainTemplate.prototype.render = function(hash, chunk, moduleTemplate, dependencyTemplates) {
143+
var simpleMode = this.applyPluginsBailResult("simple-mode") !== false && chunk.chunks.length === 0 && chunk.modules.every(function(m) {
144+
return !m.isApiRequired("__webpack_require__()");
145+
});
121146
var buf = [];
122147
buf.push(this.applyPluginsWaterfall("bootstrap", "", chunk, hash, moduleTemplate, dependencyTemplates));
123-
buf.push(this.applyPluginsWaterfall("local-vars", "", chunk, hash));
124-
buf.push("");
125-
buf.push("// The require function");
126-
buf.push("function " + this.requireFn + "(moduleId) {");
127-
buf.push(this.indent(this.applyPluginsWaterfall("require", "", chunk, hash)));
128-
buf.push("}");
129-
buf.push("");
130-
buf.push(this.asString(this.applyPluginsWaterfall("require-extensions", "", chunk, hash)));
131-
buf.push("");
132-
buf.push(this.asString(this.applyPluginsWaterfall("startup", "", chunk, hash)));
148+
if(simpleMode) {
149+
buf.push(this.applyPluginsWaterfall("simple-local-vars", "", chunk, hash));
150+
buf.push("");
151+
buf.push("// Context information");
152+
buf.push("var " + this.requireFn + " = {};");
153+
buf.push("");
154+
buf.push(this.asString(this.applyPluginsWaterfall("simple-require-extensions", "", chunk, hash)));
155+
buf.push("");
156+
buf.push(this.asString(this.applyPluginsWaterfall("simple-startup", "", chunk, hash)));
157+
} else {
158+
buf.push(this.applyPluginsWaterfall("simple-local-vars", "", chunk, hash));
159+
buf.push(this.applyPluginsWaterfall("local-vars", "", chunk, hash));
160+
buf.push("");
161+
buf.push("// The require function");
162+
buf.push("function " + this.requireFn + "(moduleId) {");
163+
buf.push(this.indent(this.applyPluginsWaterfall("require", "", chunk, hash)));
164+
buf.push("}");
165+
buf.push("");
166+
buf.push(this.asString(this.applyPluginsWaterfall("simple-require-extensions", "", chunk, hash)));
167+
buf.push(this.asString(this.applyPluginsWaterfall("require-extensions", "", chunk, hash)));
168+
buf.push("");
169+
buf.push(this.asString(this.applyPluginsWaterfall("startup", "", chunk, hash)));
170+
}
133171
var source = this.applyPluginsWaterfall("render", new OriginalSource(this.prefix(buf, " \t") + "\n", "webpack/bootstrap " + hash), chunk, hash, moduleTemplate, dependencyTemplates);
134172
if(chunk.modules.some(function(module) {
135173
return module.entry;

lib/dependencies/AMDDefineDependencyParserPlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ AMDDefineDependencyParserPlugin.prototype.apply = function(parser) {
218218
var dep, localModule;
219219
if(param.string === "require") {
220220
dep = new ConstDependency("__webpack_require__", param.range);
221-
dep.requiredApi = ["__webpack_require__"];
221+
dep.requiredApi = ["__webpack_require__", "__webpack_require__()"];
222222
} else if(["require", "exports", "module"].indexOf(param.string) >= 0) {
223223
dep = new ConstDependency(param.string, param.range);
224224
dep.requiredApi = [param.string];

lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ AMDRequireDependenciesBlockParserPlugin.prototype.apply = function(parser) {
114114
var dep, localModule;
115115
if(param.string === "require") {
116116
dep = new ConstDependency("__webpack_require__", param.string);
117-
dep.requiredApi = ["__webpack_require__"];
117+
dep.requiredApi = ["__webpack_require__", "__webpack_require__()"];
118118
} else if(["exports", "module"].indexOf(param.string) >= 0) {
119119
dep = new ConstDependency(param.string, param.range);
120120
dep.requiredApi = [param.string];

lib/dependencies/HarmonyImportDependency.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ HarmonyImportDependency.prototype.getSupportedInlineModes = function() {
2929
};
3030

3131
HarmonyImportDependency.prototype.requiredApi = function() {
32-
return ModuleHelpers.requiredApiForLoadModule(dep.module);
32+
return ModuleHelpers.requiredApiForLoadModule(this.module);
3333
};
3434

3535
HarmonyImportDependency.makeStatement = function(declare, dep, outputOptions, requestShortener) {

lib/dependencies/ModuleHelpers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ ModuleHelpers.requiredApiForLoadModule = function(module) {
2929
case "hoist":
3030
return [];
3131
}
32-
return ["__webpack_require__"];
32+
return ["__webpack_require__", "__webpack_require__()"];
3333
};

0 commit comments

Comments
 (0)