Skip to content

Commit

Permalink
refactoring and aggressive-splitting plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Jul 13, 2016
1 parent 3a6b649 commit 2f618e7
Show file tree
Hide file tree
Showing 76 changed files with 2,152 additions and 218 deletions.
12 changes: 12 additions & 0 deletions bin/webpack.js
Expand Up @@ -68,6 +68,11 @@ yargs.options({
group: DISPLAY_GROUP,
describe: "Display chunks in the output"
},
"display-entrypoints": {
type: "boolean",
group: DISPLAY_GROUP,
describe: "Display entry points in the output"
},
"display-origins": {
type: "boolean",
group: DISPLAY_GROUP,
Expand Down Expand Up @@ -110,6 +115,7 @@ var argv = yargs.argv;

if(argv.verbose) {
argv["display-reasons"] = true;
argv["display-entrypoints"] = true;
argv["display-used-exports"] = true;
argv["display-error-details"] = true;
argv["display-modules"] = true;
Expand Down Expand Up @@ -185,6 +191,10 @@ function processOptions(options) {
outputOptions.chunks = bool;
});

ifArg("display-entrypoints", function(bool) {
outputOptions.entrypoints = bool;
});

ifArg("display-reasons", function(bool) {
outputOptions.reasons = bool;
});
Expand Down Expand Up @@ -216,6 +226,8 @@ function processOptions(options) {
} else {
if(typeof outputOptions.chunks === "undefined")
outputOptions.chunks = true;
if(typeof outputOptions.entrypoints === "undefined")
outputOptions.entrypoints = true;
if(typeof outputOptions.modules === "undefined")
outputOptions.modules = true;
if(typeof outputOptions.chunkModules === "undefined")
Expand Down
4 changes: 2 additions & 2 deletions examples/build-common.js
Expand Up @@ -11,7 +11,7 @@ var extraArgs = "";
var targetArgs = global.NO_TARGET_ARGS ? "" : " ./example.js js/output.js";
var displayReasons = global.NO_REASONS ? "" : " --display-reasons --display-used-exports";
(function doIt(remainingTimes) {
cp.exec("node ../../bin/webpack.js" + displayReasons + " --display-chunks --display-modules --display-origins --output-public-path \"js/\" -p " + extraArgs + targetArgs, function (error, stdout, stderr) {
cp.exec("node ../../bin/webpack.js" + displayReasons + " --display-chunks --display-modules --display-origins --display-entrypoints --output-public-path \"js/\" -p " + extraArgs + targetArgs, function (error, stdout, stderr) {
if(stderr && remainingTimes === 1)
console.log(stderr);
if (error !== null && remainingTimes === 1)
Expand All @@ -22,7 +22,7 @@ var displayReasons = global.NO_REASONS ? "" : " --display-reasons --display-used
console.log(stderr);
throw e;
}
cp.exec("node ../../bin/webpack.js" + displayReasons + " --display-chunks --display-modules --display-origins --output-public-path \"js/\" --output-pathinfo " + extraArgs + targetArgs, function (error, stdout, stderr) {
cp.exec("node ../../bin/webpack.js" + displayReasons + " --display-chunks --display-modules --display-origins --display-entrypoints --output-public-path \"js/\" --output-pathinfo " + extraArgs + targetArgs, function (error, stdout, stderr) {
if(remainingTimes === 1)
console.log(stdout);
if(stderr && remainingTimes === 1)
Expand Down
843 changes: 843 additions & 0 deletions examples/http2-aggressive-splitting/README.md

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions examples/http2-aggressive-splitting/build.js
@@ -0,0 +1,3 @@
global.NO_TARGET_ARGS = true;
global.NO_REASONS = true;
require("../build-common");
2 changes: 2 additions & 0 deletions examples/http2-aggressive-splitting/example.js
@@ -0,0 +1,2 @@
require("react");
require(["react-dom"]);
19 changes: 19 additions & 0 deletions examples/http2-aggressive-splitting/template.md
@@ -0,0 +1,19 @@
# Info

## Uncompressed

```
{{stdout}}
```

## Minimized (uglify-js, no zip)

```
{{min:stdout}}
```

## Records

```
{{js/records.json}}
```
20 changes: 20 additions & 0 deletions examples/http2-aggressive-splitting/webpack.config.js
@@ -0,0 +1,20 @@
var path = require("path");
var webpack = require("../../");
module.exports = {
entry: "./example",
output: {
path: path.join(__dirname, "js"),
filename: "[chunkhash].js",
chunkFilename: "[chunkhash].js"
},
plugins: [
new webpack.optimize.AggressiveSplittingPlugin({
minSize: 30000,
maxSize: 50000
}),
new webpack.DefinePlugin({
"process.env.NODE_ENV": JSON.stringify("production")
})
],
recordsOutputPath: path.join(__dirname, "js", "records.json")
};
22 changes: 22 additions & 0 deletions lib/AsyncDependenciesBlock.js
Expand Up @@ -38,3 +38,25 @@ AsyncDependenciesBlock.prototype.disconnect = function() {
this.chunks = null;
DependenciesBlock.prototype.disconnect.call(this);
};

AsyncDependenciesBlock.prototype.unseal = function() {
this.chunks = null;
DependenciesBlock.prototype.unseal.call(this);
};

AsyncDependenciesBlock.prototype.sortItems = function() {
DependenciesBlock.prototype.sortItems.call(this);
if(this.chunks) {
this.chunks.sort(function(a, b) {
var i = 0;
while(true) {
if(!a.modules[i] && !b.modules[i]) return 0;
if(!a.modules[i]) return -1;
if(!b.modules[i]) return 1;
if(a.modules[i].id > b.modules[i].id) return 1;
if(a.modules[i].id < b.modules[i].id) return -1;
i++;
}
});
}
};
108 changes: 91 additions & 17 deletions lib/Chunk.js
Expand Up @@ -2,6 +2,7 @@
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
var compareLocations = require("./compareLocations");
var debugId = 1000;

function Chunk(name, module, loc) {
Expand All @@ -10,14 +11,13 @@ function Chunk(name, module, loc) {
this.debugId = debugId++;
this.name = name;
this.modules = [];
this.entrypoints = [];
this.chunks = [];
this.parents = [];
this.blocks = [];
this.origins = [];
this.files = [];
this.rendered = false;
this.entry = false;
this.initial = false;
if(module) {
this.origins.push({
module: module,
Expand All @@ -28,6 +28,39 @@ function Chunk(name, module, loc) {
}
module.exports = Chunk;

Object.defineProperty(Chunk.prototype, "entry", {
configurable: false,
get: function() {
throw new Error("Chunk.entry was removed. Use hasRuntime()");
},
set: function() {
throw new Error("Chunk.entry was removed. Use hasRuntime()");
}
});

Object.defineProperty(Chunk.prototype, "initial", {
configurable: false,
get: function() {
throw new Error("Chunk.initial was removed. Use isInitial()");
},
set: function() {
throw new Error("Chunk.initial was removed. Use isInitial()");
}
});

Chunk.prototype.hasRuntime = function() {
if(this.entrypoints.length === 0) return false;
return this.entrypoints[0].chunks[0] === this;
};

Chunk.prototype.isInitial = function() {
return this.entrypoints.length > 0;
};

Chunk.prototype.hasEntryModule = function() {
return !!this.entryModule;
};

Chunk.prototype.addModule = function(module) {
if(this.modules.indexOf(module) >= 0) {
return false;
Expand Down Expand Up @@ -117,6 +150,13 @@ Chunk.prototype.remove = function(reason) {
}, this);
};

Chunk.prototype.moveModule = function(module, other) {
module.removeChunk(this);
module.addChunk(other);
other.addModule(module);
module.rewriteChunkInReasons(this, [other]);
};

Chunk.prototype.integrate = function(other, reason) {
if(!this.canBeIntegrated(other)) {
return false;
Expand Down Expand Up @@ -161,44 +201,65 @@ Chunk.prototype.integrate = function(other, reason) {
}, this);
other.blocks.length = 0;
other.origins.forEach(function(origin) {
this.origins.push(origin);
}, this);
this.origins.forEach(function(origin) {
if(!origin.reasons) {
origin.reasons = [reason];
} else if(origin.reasons[0] !== reason) {
origin.reasons.unshift(reason);
}
this.origins.push(origin);
}, this);
})
return true;
};

Chunk.prototype.split = function(newChunk) {
var _this = this;
this.blocks.forEach(function(b) {
newChunk.blocks.push(b);
b.chunks.push(newChunk);
});
this.chunks.forEach(function(c) {
newChunk.chunks.push(c);
c.parents.push(newChunk);
});
this.parents.forEach(function(p) {
p.chunks.push(newChunk);
newChunk.parents.push(p);
});
this.entrypoints.forEach(function(e) {
e.insertChunk(newChunk, _this);
});
};

Chunk.prototype.isEmpty = function() {
return this.modules.length === 0;
};

Chunk.prototype.updateHash = function(hash) {
hash.update(this.id + " ");
hash.update(this.ids ? this.ids.join(",") : "");
hash.update(this.name + "");
hash.update((this.name || "") + " ");
this.modules.forEach(function(m) {
m.updateHash(hash);
});
};

Chunk.prototype.size = function(options) {
var CHUNK_OVERHEAD = options.chunkOverhead || 10000;
var CHUNK_OVERHEAD = typeof options.chunkOverhead === "number" ? options.chunkOverhead : 10000;
var ENTRY_CHUNK_MULTIPLICATOR = options.entryChunkMultiplicator || 10;

var modulesSize = this.modules.reduce(function(a, b) {
return a + b.size();
}, 0);
return modulesSize * (this.initial ? ENTRY_CHUNK_MULTIPLICATOR : 1) + CHUNK_OVERHEAD;
return modulesSize * (this.isInitial() ? ENTRY_CHUNK_MULTIPLICATOR : 1) + CHUNK_OVERHEAD;
};

Chunk.prototype.canBeIntegrated = function(other) {
if(other.initial) {
if(other.isInitial()) {
return false;
}
if(this.initial) {
if(this.isInitial()) {
if(other.parents.length !== 1 || other.parents[0] !== this) {
return false;
}
Expand All @@ -225,23 +286,17 @@ Chunk.prototype.integratedSize = function(other, options) {
var modulesSize = mergedModules.reduce(function(a, m) {
return a + m.size();
}, 0);
return modulesSize * (this.initial || other.initial ? ENTRY_CHUNK_MULTIPLICATOR : 1) + CHUNK_OVERHEAD;
return modulesSize * (this.isInitial() || other.isInitial() ? ENTRY_CHUNK_MULTIPLICATOR : 1) + CHUNK_OVERHEAD;
};

Chunk.prototype.hasEntryModule = function() {
return this.modules.some(function(module) {
return module.entry;
});
}

Chunk.prototype.getChunkMaps = function(includeEntries, realHash) {
var chunksProcessed = [];
var chunkHashMap = {};
var chunkNameMap = {};
(function addChunk(c) {
if(chunksProcessed.indexOf(c) >= 0) return;
chunksProcessed.push(c);
if(!c.entry || includeEntries) {
if(!c.hasRuntime() || includeEntries) {
chunkHashMap[c.id] = realHash ? c.hash : c.renderedHash;
if(c.name)
chunkNameMap[c.id] = c.name;
Expand All @@ -254,6 +309,25 @@ Chunk.prototype.getChunkMaps = function(includeEntries, realHash) {
};
};

function byId(a, b) {
return a.id - b.id;
}

Chunk.prototype.sortItems = function() {
this.modules.sort(byId);
this.origins.sort(function(a, b) {
var aIdent = a.module.identifier();
var bIdent = b.module.identifier();
if(aIdent < bIdent) return -1;
if(aIdent > bIdent) return 1;
return compareLocations(a.loc, b.loc);
});
this.origins.forEach(function(origin) {
if(origin.reasons)
origin.reasons.sort();
});
};

Chunk.prototype.toString = function() {
return "Chunk[" + this.modules.join() + "]";
};

0 comments on commit 2f618e7

Please sign in to comment.