Skip to content

Commit 209f96c

Browse files
committed
added option to prefetch modules
1 parent 9627518 commit 209f96c

File tree

10 files changed

+122
-24
lines changed

10 files changed

+122
-24
lines changed

bin/config-optimist.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ module.exports = function(optimist) {
5555

5656
.boolean("optimize-occurence-order").describe("optimize-occurence-order")
5757

58+
.string("prefetch").describe("prefetch")
59+
5860
.string("provide").describe("provide")
5961

6062
.string("plugin").describe("plugin")

bin/convert-argv.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,11 @@ module.exports = function(optimist, argv, convertOptions) {
255255
options.optimize.occurenceOrder = true;
256256
});
257257

258+
ifArg("prefetch", function(request) {
259+
ensureArray(options, "prefetch");
260+
options.prefetch.push(request);
261+
});
262+
258263
ifArg("provide", function(value) {
259264
ensureObject(options, "provide");
260265
var idx = value.indexOf("=");

lib/Compilation.js

Lines changed: 63 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -234,63 +234,106 @@ Compilation.prototype.processModuleDependencies = function(module, callback) {
234234
});
235235
};
236236

237-
Compilation.prototype.addEntry = function process(context, entry, name, callback) {
237+
Compilation.prototype._addModuleChain = function process(context, dependency, onModule, callback) {
238238
var errorAndCallback = this.bail ? function errorAndCallback(err) {
239239
callback(err);
240240
} : function errorAndCallback(err) {
241-
err.dependencies = [entry];
241+
err.dependencies = [dependency];
242242
this.errors.push(err);
243243
callback();
244244
}.bind(this);
245245

246-
if(!(typeof entry == "object" && entry != null && entry.Class))
247-
return callback(new Error("Parameter 'entry' must be a Dependency"));
246+
if(!(typeof dependency == "object" && dependency != null && dependency.Class))
247+
throw new Error("Parameter 'dependency' must be a Dependency");
248248

249-
var moduleFactory = this.dependencyFactories.get(entry.Class);
249+
var moduleFactory = this.dependencyFactories.get(dependency.Class);
250250
if(!moduleFactory)
251-
return callback(new Error("No dependency factory availible for this entry dependency type: " + entry.Class.name));
251+
throw new Error("No dependency factory availible for this dependency type: " + dependency.Class.name);
252252

253-
moduleFactory.create(context, entry, function(err, module) {
253+
if(this.profile) var start = +new Date();
254+
moduleFactory.create(context, dependency, function(err, module) {
254255
if(err) return errorAndCallback(new EntryModuleNotFoundError(err));
255256

257+
if(this.profile) {
258+
if(!module.profile) module.profile = {};
259+
var afterFactory = +new Date();
260+
module.profile.factory = afterFactory - start;
261+
}
262+
256263
var result = this.addModule(module);
257264
if(!result) {
258-
return callback(new Error("Entry module is already added"));
265+
module = this.getModule(module);
266+
267+
onModule(module);
268+
269+
return callback(null, module);
259270
}
260271

261272
if(result instanceof Module) {
273+
if(this.profile) {
274+
result.profile = module.profile;
275+
}
276+
262277
module = result;
263278
}
264279

265-
this.entries.push(module);
266-
module.id = 0;
280+
onModule(module);
267281

268282
if(result instanceof Module) {
269-
entryReady.call(this);
283+
moduleReady.call(this);
270284
} else {
271285
this.buildModule(module, function(err) {
272286
if(err) return errorAndCallback(err);
273287

274-
entryReady.call(this);
288+
if(this.profile) {
289+
var afterBuilding = +new Date();
290+
module.profile.building = afterBuilding - afterFactory;
291+
}
292+
293+
moduleReady.call(this);
275294
}.bind(this));
276295
}
277296

278-
function entryReady() {
297+
function moduleReady() {
279298
this.processModuleDependencies(module, function(err) {
280299
if(err) return callback(err);
281300

282-
var chunk = this.addChunk(name);
283-
chunk.id = 0;
284-
chunk.entry = true;
285-
chunk.addModule(module);
286-
module.addChunk(chunk);
287-
this.processDependenciesBlockForChunk(module, chunk);
288-
return callback();
301+
return callback(null, module);
289302
}.bind(this));
290303
}
291304
}.bind(this));
292305
};
293306

307+
Compilation.prototype.addEntry = function process(context, entry, name, callback) {
308+
this._addModuleChain(context, entry, function(module) {
309+
310+
this.entries.push(module);
311+
module.id = 0;
312+
313+
}.bind(this), function(err, module) {
314+
if(err) return callback(err);
315+
316+
if(module) {
317+
var chunk = this.addChunk(name);
318+
chunk.id = 0;
319+
chunk.entry = true;
320+
chunk.addModule(module);
321+
module.addChunk(chunk);
322+
this.processDependenciesBlockForChunk(module, chunk);
323+
}
324+
return callback();
325+
}.bind(this));
326+
};
327+
328+
329+
Compilation.prototype.prefetch = function process(context, dependency, callback) {
330+
this._addModuleChain(context, dependency, function(module) {
331+
332+
module.prefetched = true;
333+
334+
}, callback);
335+
};
336+
294337
Compilation.prototype.seal = function seal(callback) {
295338
this.applyPlugins("seal");
296339
this.applyPlugins("optimize");

lib/Compiler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ Compiler.prototype.compile = function(callback) {
278278

279279
var compilation = this.newCompilation(params);
280280

281-
this.applyPluginsAsync("make", compilation, function(err) {
281+
this.applyPluginsParallel("make", compilation, function(err) {
282282
if(err) return callback(err);
283283

284284
compilation.seal(function(err) {

lib/PrefetchPlugin.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
var PrefetchDependency = require("./dependencies/PrefetchDependency");
6+
7+
function PrefetchPlugin(context, request) {
8+
this.context = context;
9+
this.request = request;
10+
}
11+
module.exports = PrefetchPlugin;
12+
PrefetchPlugin.prototype.apply = function(compiler) {
13+
compiler.plugin("compilation", function(compilation, params) {
14+
var normalModuleFactory = params.normalModuleFactory;
15+
16+
compilation.dependencyFactories.set(PrefetchDependency, normalModuleFactory);
17+
});
18+
compiler.plugin("make", function(compilation, callback) {
19+
compilation.prefetch(this.context, new PrefetchDependency(this.request), callback);
20+
}.bind(this));
21+
};

lib/Stats.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ Stats.prototype.toJson = function toJson(options, forToString) {
114114
size: module.size(),
115115
cacheable: !!module.cacheable,
116116
built: !!module.built,
117+
prefetched: !!module.prefetched,
117118
chunks: module.chunks.map(function(chunk) {
118119
return chunk.id;
119120
}),
@@ -406,6 +407,9 @@ Stats.jsonToString = function jsonToString(obj, useColors) {
406407
if(module.built) {
407408
green(" [built]");
408409
}
410+
if(module.prefetched) {
411+
magenta(" [prefetched]");
412+
}
409413
if(module.chunks) {
410414
module.chunks.forEach(function(chunk) {
411415
normal(" {");
@@ -453,6 +457,9 @@ Stats.jsonToString = function jsonToString(obj, useColors) {
453457
if(module.built) {
454458
green(" [built]");
455459
}
460+
if(module.prefetched) {
461+
magenta(" [prefetched]");
462+
}
456463
if(module.chunks) {
457464
module.chunks.forEach(function(chunk) {
458465
normal(" {");

lib/WebpackOptionsApply.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var EvalDevToolModulePlugin = require("./EvalDevToolModulePlugin");
1212
var SourceMapDevToolPlugin = require("./SourceMapDevToolPlugin");
1313
var LibraryTemplatePlugin = require("./LibraryTemplatePlugin");
1414

15+
var PrefetchPlugin = require("./PrefetchPlugin");
1516
var SingleEntryPlugin = require("./SingleEntryPlugin");
1617
var MultiEntryPlugin = require("./MultiEntryPlugin");
1718
var CachePlugin = require("./CachePlugin");
@@ -111,6 +112,11 @@ WebpackOptionsApply.prototype.process = function(options, compiler) {
111112
compiler.apply(itemToPlugin(options.entry[name], name));
112113
});
113114
}
115+
if(options.prefetch) {
116+
options.prefetch.map(function(request) {
117+
compiler.apply(new PrefetchPlugin(options.context, request));
118+
});
119+
}
114120
compiler.apply(
115121
new CompatibilityPlugin(),
116122
new NodeStuffPlugin(options.node),
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
var ModuleDependency = require("./ModuleDependency");
6+
7+
function PrefetchDependency(request) {
8+
ModuleDependency.call(this, request);
9+
this.Class = PrefetchDependency;
10+
}
11+
module.exports = PrefetchDependency;
12+
13+
PrefetchDependency.prototype = Object.create(ModuleDependency.prototype);
14+
PrefetchDependency.prototype.type = "prefetch";

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": "0.10.0-beta10",
3+
"version": "0.10.0-beta11",
44
"author": "Tobias Koppers @sokra",
55
"description": "Packs CommonJs/AMD/Labeled 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, jade, coffee, css, less, ... and your custom stuff.",
66
"dependencies": {

test/browsertest/build.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ var library1 = cp.spawn("node", join(["../../bin/webpack.js", "--output-pathinfo
3434
bindOutput(library1);
3535
library1.on("exit", function(code) {
3636
if(code === 0) {
37-
// node ../../bin/webpack --output-pathinfo --colors --resolve-alias vm=vm-browserify --output-public-path js/ --module-bind json --module-bind css=style!css --module-bind less=style!css!less --module-bind coffee --module-bind jade ./lib/index js/web.js
37+
// node ../../bin/webpack --output-pathinfo --colors --resolve-alias vm=vm-browserify --output-public-path js/ --module-bind json --module-bind css=style!css --module-bind less=style!css!less --module-bind coffee --module-bind jade --prefetch ./less/stylesheet.less ./lib/index js/web.js
3838
var main = cp.spawn("node", join(["../../bin/webpack.js", "--output-pathinfo", "--colors", "--resolve-alias", "vm=vm-browserify", "--workers",
39-
"--output-public-path", "js/", "--module-bind", "json", "--module-bind", "css=style!css", "--module-bind", "less=style/url!file?postfix=.css&string!less", "--module-bind", "coffee", "--module-bind", "jade", "./lib/index", "js/web.js", "--progress"], extraArgs));
39+
"--output-public-path", "js/", "--module-bind", "json", "--module-bind", "css=style!css", "--module-bind", "less=style/url!file?postfix=.css&string!less", "--module-bind", "coffee", "--module-bind", "jade", "--prefetch", "./less/stylesheet.less", "./lib/index", "js/web.js", "--progress"], extraArgs));
4040
bindOutput(main);
4141
}
4242
});

0 commit comments

Comments
 (0)