Skip to content

Commit 4513b7d

Browse files
author
Sean Larkin
committed
Merge remote-tracking branch 'upstream/master'
2 parents e6dcf53 + ee3a0bc commit 4513b7d

23 files changed

Lines changed: 193 additions & 33 deletions

File tree

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ or packaging just about any resource or asset.
2727

2828
**TL; DR**
2929

30-
* Bundles both [CommonJs](http://www.commonjs.org/specs/modules/1.0/) and [AMD](https://github.com/amdjs/amdjs-api/wiki/AMD) modules (even combined).
30+
* Bundles both [CommonJs](http://wiki.commonjs.org/) and [AMD](https://github.com/amdjs/amdjs-api/wiki/AMD) modules (even combined).
3131
* Can create a single bundle or multiple chunks that are asynchronously loaded at runtime (to reduce initial loading time).
3232
* Dependencies are resolved during compilation reducing the runtime size.
3333
* Loaders can preprocess files while compiling, e.g. coffeescript to JavaScript, handlebars strings to compiled functions, images to Base64, etc.
@@ -45,7 +45,8 @@ project:
4545

4646
global:
4747
`npm install webpack -g`
48-
Usage
48+
49+
Usage:
4950
https://webpack.github.io/docs/tutorials/getting-started/
5051

5152
# Examples

bin/convert-argv.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ module.exports = function(optimist, argv, convertOptions) {
3131
var configFileLoaded = false;
3232
var configPath, ext;
3333
var extensions = Object.keys(interpret.extensions).sort(function(a, b) {
34-
return a.length - b.length;
34+
return a === '.js' ? -1 : b === '.js' ? 1 : a.length - b.length;
3535
});
3636
var configFiles = ["webpack.config", "webpackfile"].map(function(filename) {
3737
return extensions.map(function(ext) {
@@ -94,14 +94,19 @@ module.exports = function(optimist, argv, convertOptions) {
9494
configFileLoaded = true;
9595
}
9696

97-
if(typeof options === "function") {
97+
var isES6DefaultExportedFunc = (
98+
typeof options === "object" && options !== null && typeof options.default === "function"
99+
);
100+
101+
if(typeof options === "function" || isES6DefaultExportedFunc) {
102+
options = isES6DefaultExportedFunc ? options.default : options;
98103
options = options(argv.env, argv);
99104
}
100105

101106
return processConfiguredOptions(options);
102107

103108
function processConfiguredOptions(options) {
104-
if(typeof options !== "object" || options === null) {
109+
if(options === null || typeof options !== "object") {
105110
console.error("Config did not export an object or a function returning an object.");
106111
process.exit(-1); // eslint-disable-line
107112
}
@@ -112,8 +117,8 @@ module.exports = function(optimist, argv, convertOptions) {
112117
}
113118

114119
// process ES6 default
115-
if(typeof options === "object" && typeof options["default"] === "object") {
116-
return processConfiguredOptions(options["default"]);
120+
if(typeof options === "object" && typeof options.default === "object") {
121+
return processConfiguredOptions(options.default);
117122
}
118123

119124
if(Array.isArray(options)) {

examples/code-splitting/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
This example illustrates a very simple case of Code Splitting with `require.ensure`.
22

33
* `a` and `b` are required normally via CommonJS
4-
* `c` is depdended through the `require.ensure` array.
4+
* `c` is depended through the `require.ensure` array.
55
* This means: make it available, but don't execute it
66
* webpack will load it on demand
77
* `b` and `d` are required via CommonJs in the `require.ensure` callback

examples/code-splitting/template.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
This example illustrates a very simple case of Code Splitting with `require.ensure`.
22

33
* `a` and `b` are required normally via CommonJS
4-
* `c` is depdended through the `require.ensure` array.
4+
* `c` is depended through the `require.ensure` array.
55
* This means: make it available, but don't execute it
66
* webpack will load it on demand
77
* `b` and `d` are required via CommonJs in the `require.ensure` callback

examples/harmony-unused/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ This example demonstrates how webpack tracks the using of ES6 imports and export
22

33
Excluding unused exports from bundles is known as "[tree-shaking](http://www.2ality.com/2015/12/webpack-tree-shaking.html)".
44

5-
In this example, only `add` and `multiply` in `./math.js` are used used by the app. `list` is unused and is not included in the minimized bundle (Look for `Array.from` in the minimized bundle).
5+
In this example, only `add` and `multiply` in `./math.js` are used by the app. `list` is unused and is not included in the minimized bundle (Look for `Array.from` in the minimized bundle).
66

77
In addition to that, `library.js` simulates an entry point to a big library. `library.js` re-exports multiple identifiers from submodules. Often big parts of that is unused, like `abc.js`. Note how the usage information flows from `example.js` through `library.js` into `abc.js` and all declarations in `abc.js` are not included in the minimized bundle (Look for `console.log("a")` in the minimized bundle).
88

lib/Compiler.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ Watching.prototype.watch = function(files, dirs, missing) {
105105
this.compiler.fileTimestamps = fileTimestamps;
106106
this.compiler.contextTimestamps = contextTimestamps;
107107
this.invalidate();
108-
}.bind(this), function() {
109-
this.compiler.applyPlugins("invalid");
108+
}.bind(this), function(fileName, changeTime) {
109+
this.compiler.applyPlugins("invalid", fileName, changeTime);
110110
}.bind(this));
111111
};
112112

lib/NormalModule.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ NormalModule.prototype.source = function(dependencyTemplates, outputOptions, req
268268
});
269269
emitFunction();
270270
var start = block.range ? block.range[0] : -10;
271-
var end = block.range ? block.range[1] : _source.size();
271+
var end = block.range ? block.range[1] : (_source.size() + 1);
272272
if(varStartCode) source.insert(start + 0.5, varStartCode);
273273
if(varEndCode) source.insert(end + 0.5, "\n/* WEBPACK VAR INJECTION */" + varEndCode);
274274
}

lib/dependencies/HarmonyExportSpecifierDependency.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ HarmonyExportSpecifierDependency.Template.prototype.apply = function(dep, source
3939
} else if(dep.immutable) {
4040
content = "/* harmony export */ exports[" + JSON.stringify(used) + "] = " + dep.id + ";";
4141
} else {
42-
content = "/* harmony export */ Object.defineProperty(exports, " + JSON.stringify(used) + ", {configurable: false, enumerable: true, get: function() { return " + dep.id + "; }});";
42+
content = "\n/* harmony export */ Object.defineProperty(exports, " + JSON.stringify(used) + ", {configurable: false, enumerable: true, get: function() { return " + dep.id + "; }});";
4343
}
4444
source.insert(dep.position, content);
4545

lib/optimize/RemoveParentModulesPlugin.js

Lines changed: 101 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,58 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
5-
function hasModule(chunk, module, checkedChunks) {
6-
if(chunk.modules.indexOf(module) >= 0) return [chunk];
7-
if(chunk.entry) return false;
8-
return allHaveModule(chunk.parents.filter(function(c) {
9-
return checkedChunks.indexOf(c) < 0;
10-
}), module, checkedChunks);
5+
function listToSet(list, chunk) {
6+
var set = {};
7+
list.forEach(function(module) {
8+
set[module._RemoveParentModulesPlugin_index] = {
9+
module: module,
10+
chunks: [chunk]
11+
};
12+
});
13+
return set;
14+
}
15+
16+
function mergeSets(a, b) {
17+
var newSet = {};
18+
Object.keys(a).forEach(function(key) {
19+
var item = a[key];
20+
newSet[key] = {
21+
module: item.module,
22+
chunks: item.chunks
23+
};
24+
});
25+
Object.keys(b).forEach(function(key) {
26+
var item = b[key];
27+
newSet[key] = {
28+
module: item.module,
29+
chunks: item.chunks
30+
};
31+
});
32+
return newSet;
33+
}
34+
35+
function intersectSets(a, b) {
36+
var newSet = {};
37+
Object.keys(a).forEach(function(key) {
38+
var aItem = a[key];
39+
var bItem = b[key];
40+
if(bItem) {
41+
newSet[key] = {
42+
module: aItem.module,
43+
chunks: aItem.chunks.concat(bItem.chunks)
44+
};
45+
}
46+
});
47+
return newSet;
1148
}
1249

13-
function allHaveModule(someChunks, module, checkedChunks) {
14-
if(!checkedChunks) checkedChunks = [];
15-
var chunks = [];
16-
for(var i = 0; i < someChunks.length; i++) {
17-
checkedChunks.push(someChunks[i]);
18-
var subChunks = hasModule(someChunks[i], module, checkedChunks);
19-
if(!subChunks) return false;
20-
addToSet(chunks, subChunks);
21-
}
22-
return chunks;
50+
function intersectAll(map) {
51+
var keys = Object.keys(map);
52+
if(keys.length === 0)
53+
return null;
54+
return keys.map(function(key) {
55+
return map[key];
56+
}).reduce(intersectSets);
2357
}
2458

2559
function addToSet(set, items) {
@@ -29,16 +63,65 @@ function addToSet(set, items) {
2963
});
3064
}
3165

66+
function toStr(set) {
67+
return Object.keys(set).map(function(key) {
68+
return set[key].module.request.substr(-12);
69+
}).join(", ");
70+
}
71+
3272
function RemoveParentModulesPlugin() {}
3373
module.exports = RemoveParentModulesPlugin;
3474

3575
RemoveParentModulesPlugin.prototype.apply = function(compiler) {
3676
compiler.plugin("compilation", function(compilation) {
3777
compilation.plugin(["optimize-chunks-basic", "optimize-extracted-chunks-basic"], function(chunks) {
78+
this.modules.forEach(function(module, idx) {
79+
module._RemoveParentModulesPlugin_index = idx;
80+
})
81+
var todo = chunks.slice();
82+
todo.forEach(function(chunk, idx) {
83+
chunk._RemoveParentModulesPlugin_processed = false;
84+
chunk._RemoveParentModulesPlugin_availableModulesByChunk = {};
85+
chunk._RemoveParentModulesPlugin_index = idx;
86+
})
87+
for(var i = 0; i < todo.length; i++) {
88+
var chunk = todo[i];
89+
var index = chunk._RemoveParentModulesPlugin_index;
90+
var availableModules = chunk._RemoveParentModulesPlugin_availableModules = intersectAll(chunk._RemoveParentModulesPlugin_availableModulesByChunk);
91+
if(chunk.chunks.length === 0) {
92+
chunk._RemoveParentModulesPlugin_processed = true;
93+
continue;
94+
}
95+
var set = listToSet(chunk.modules, chunk);
96+
if(availableModules)
97+
set = mergeSets(set, availableModules);
98+
chunk.chunks.forEach(function(child) {
99+
var availableModules = child._RemoveParentModulesPlugin_availableModulesByChunk[index];
100+
child._RemoveParentModulesPlugin_availableModulesByChunk[index] = set;
101+
if(!availableModules || Object.keys(availableModules).length !== Object.keys(set).length) {
102+
if(child._RemoveParentModulesPlugin_processed) {
103+
todo.push(child);
104+
child._RemoveParentModulesPlugin_processed = false;
105+
}
106+
}
107+
});
108+
chunk._RemoveParentModulesPlugin_processed = true;
109+
}
38110
chunks.forEach(function(chunk) {
111+
var availableModules = chunk._RemoveParentModulesPlugin_availableModules;
112+
delete chunk._RemoveParentModulesPlugin_availableModulesByChunk;
113+
delete chunk._RemoveParentModulesPlugin_availableModules;
114+
delete chunk._RemoveParentModulesPlugin_processed;
115+
delete chunk._RemoveParentModulesPlugin_index;
116+
if(chunk.entry) return;
117+
if(!availableModules) return;
39118
chunk.modules.slice().forEach(function(module) {
40-
if(chunk.entry) return;
41-
var parentChunksWithModule = allHaveModule(chunk.parents, module);
119+
var info = availableModules[module._RemoveParentModulesPlugin_index];
120+
if(!info) return;
121+
var parentChunksWithModule = info.chunks;
122+
parentChunksWithModule = parentChunksWithModule.filter(function(chunk, idx) {
123+
return parentChunksWithModule.indexOf(chunk) === idx;
124+
});
42125
if(parentChunksWithModule) {
43126
module.rewriteChunkInReasons(chunk, parentChunksWithModule);
44127
chunk.removeModule(module);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "webpack",
3-
"version": "2.1.0-beta.3",
3+
"version": "2.1.0-beta.4",
44
"author": "Tobias Koppers @sokra",
55
"description": "Packs CommonJs/AMD modules for the browser. Allows to split your codebase into multiple bundles, which can be loaded on demand. Support loaders to preprocess files, i.e. json, jsx, es7, css, less, ... and your custom stuff.",
66
"dependencies": {

0 commit comments

Comments
 (0)