Skip to content

Commit 55d1af8

Browse files
committed
fix named chunks, added multi entry plugin
1 parent ee01837 commit 55d1af8

File tree

10 files changed

+181
-11
lines changed

10 files changed

+181
-11
lines changed

bin/convert-argv.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,15 +241,22 @@ module.exports = function(optimist, argv, convertOptions) {
241241
}
242242
}
243243

244-
if(argv._.length > 1) {
244+
if(argv._.length > 0) {
245245
ensureObject(options, "entry");
246+
function addTo(name, entry) {
247+
if(options.entry[name]) {
248+
if(!Array.isArray(options.entry[name]))
249+
options.entry[name] = [options.entry[name]];
250+
options.entry[name].push(entry);
251+
} else {
252+
options.entry[name] = entry;
253+
}
254+
}
246255
argv._.forEach(function(content) {
247256
var i = content.indexOf("=");
248-
if(i < 0) throw new Error("Each element must be <string>=<string>.");
249-
else return options.entry[content.substr(0, i)] = content.substr(i+1);
257+
if(i < 0) addTo("main", content);
258+
else addTo(content.substr(0, i), content.substr(i+1))
250259
});
251-
} else if(argv._.length == 1) {
252-
options.entry = argv._[0];
253260
}
254261

255262
return options;

lib/Compilation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ Compilation.prototype.seal = function seal(callback) {
267267

268268
Compilation.prototype.addChunk = function addChunk(name) {
269269
if(name) {
270-
if(Object.prototype.hasOwnProperty(this.namedChunks, name))
270+
if(Object.prototype.hasOwnProperty.call(this.namedChunks, name))
271271
return this.namedChunks[name];
272272
}
273273
var chunk = new Chunk(name);

lib/MultiEntryPlugin.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
var MultiEntryDependency = require("./dependencies/MultiEntryDependency");
6+
var SingleEntryDependency = require("./dependencies/SingleEntryDependency");
7+
var MultiModuleFactory = require("./MultiModuleFactory");
8+
9+
function MultiEntryPlugin(context, entries, name) {
10+
this.context = context;
11+
this.entries = entries;
12+
this.name = name;
13+
}
14+
module.exports = MultiEntryPlugin;
15+
MultiEntryPlugin.prototype.apply = function(compiler) {
16+
compiler.plugin("compilation", function(compilation, params) {
17+
var multiModuleFactory = new MultiModuleFactory();
18+
var normalModuleFactory = params.normalModuleFactory;
19+
20+
compilation.dependencyFactories.set(MultiEntryDependency, multiModuleFactory);
21+
22+
compilation.dependencyFactories.set(SingleEntryDependency, normalModuleFactory);
23+
});
24+
compiler.plugin("make", function(compilation, callback) {
25+
compilation.addEntry(this.context, new MultiEntryDependency(this.entries.map(function(e) {
26+
return new SingleEntryDependency(e);
27+
}), this.name), this.name, callback);
28+
}.bind(this));
29+
};

lib/MultiModule.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
var Module = require("./Module");
6+
var RawSource = require("webpack-core/lib/RawSource");
7+
8+
function MultiModule(context, dependencies, name) {
9+
Module.call(this);
10+
this.context = context;
11+
this.dependencies = dependencies;
12+
this.name = name;
13+
this.built = false;
14+
this.cacheable = true;
15+
}
16+
module.exports = MultiModule;
17+
18+
MultiModule.prototype = Object.create(Module.prototype);
19+
20+
MultiModule.prototype.identifier = function() {
21+
return "multi " + this.name;
22+
};
23+
24+
MultiModule.prototype.readableIdentifier = function(requestShortener) {
25+
return "multi " + this.name;
26+
};
27+
28+
MultiModule.prototype.disconnect = function disconnect() {
29+
this.built = false;
30+
Module.prototype.disconnect.call(this);
31+
};
32+
33+
MultiModule.prototype.build = function build(options, compilation, resolver, fs, callback) {
34+
this.built = true;
35+
return callback();
36+
};
37+
38+
MultiModule.prototype.source = function(dependencyTemplates, outputOptions, requestShortener) {
39+
var str = ["module.exports = "];
40+
this.dependencies.forEach(function(dep, idx) {
41+
if(dep.module) {
42+
str.push("require(");
43+
if(outputOptions.pathinfo)
44+
str.push("/*! "+dep.request+" */");
45+
str.push(""+dep.module.id);
46+
str.push(")");
47+
} else {
48+
str.push("(function webpackMissingModule() { throw new Error(");
49+
str.push(JSON.stringify("Cannot find module \"" + dep.request + "\""));
50+
str.push("); }())");
51+
}
52+
str.push(";\n");
53+
});
54+
return new RawSource(str.join(""));
55+
};
56+
57+
MultiModule.prototype.needRebuild = function needRebuild(fileTimestamps, contextTimestamps) {
58+
return false;
59+
};
60+
61+
MultiModule.prototype.size = function() {
62+
return 16 + this.dependencies.length * 12;
63+
};
64+
65+
MultiModule.prototype.updateHash = function(hash) {
66+
hash.update("multi module");
67+
hash.update(this.name || "");
68+
Module.prototype.updateHash.call(this, hash);
69+
};

lib/MultiModuleFactory.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
var async = require("async");
6+
7+
var Tapable = require("tapable");
8+
var MultiModule = require("./MultiModule");
9+
10+
function MultiModuleFactory() {
11+
Tapable.call(this);
12+
}
13+
module.exports = MultiModuleFactory;
14+
15+
MultiModuleFactory.prototype = Object.create(Tapable.prototype);
16+
MultiModuleFactory.prototype.create = function(context, dependency, callback) {
17+
callback(null, new MultiModule(context, dependency.dependencies, dependency.name));
18+
};

lib/WebpackOptionsApply.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var EvalDevToolModulePlugin = require("./EvalDevToolModulePlugin");
1010
var LibraryTemplatePlugin = require("./LibraryTemplatePlugin");
1111

1212
var SingleEntryPlugin = require("./SingleEntryPlugin");
13+
var MultiEntryPlugin = require("./MultiEntryPlugin");
1314
var CachePlugin = require("./CachePlugin");
1415

1516
var UglifyJsPlugin = require("./UglifyJsPlugin");
@@ -59,11 +60,17 @@ WebpackOptionsApply.prototype.process = function(options, compiler) {
5960
compiler.apply(new LibraryTemplatePlugin(options.output.library, options.output.libraryTarget));
6061
if(options.devtool == "eval")
6162
compiler.apply(new EvalDevToolModulePlugin());
62-
if(typeof options.entry == "string") {
63-
compiler.apply(new SingleEntryPlugin(options.context, options.entry, "main"));
63+
function itemToPlugin(item, name) {
64+
if(Array.isArray(item))
65+
return new MultiEntryPlugin(options.context, item, name);
66+
else
67+
return new SingleEntryPlugin(options.context, item, name)
68+
}
69+
if(typeof options.entry == "string" || Array.isArray(options.entry)) {
70+
compiler.apply(itemToPlugin(options.entry, "main"));
6471
} else if(typeof options.entry == "object") {
6572
Object.keys(options.entry).forEach(function(name) {
66-
compiler.apply(new SingleEntryPlugin(options.context, options.entry[name], name));
73+
compiler.apply(itemToPlugin(options.entry[name], name));
6774
});
6875
}
6976
compiler.apply(
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
var Dependency = require("../Dependency");
6+
7+
function MultiEntryDependency(dependencies, name) {
8+
Dependency.call(this);
9+
this.Class = MultiEntryDependency;
10+
this.dependencies = dependencies;
11+
this.name = name;
12+
}
13+
module.exports = MultiEntryDependency;
14+
15+
MultiEntryDependency.prototype = Object.create(Dependency.prototype);
16+
MultiEntryDependency.prototype.type = "multi entry";

test/browsertest/build.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ library1.on("exit", function(code) {
4040
bindOutput(main);
4141
}
4242
});
43-
// node ../../bin/webpack --output-pathinfo --colors --output-library library2 --output-public-path js/ --config library2config.js library2 js/library2.js
43+
// node ../../bin/webpack --output-pathinfo --colors --output-library library2 --output-public-path js/ --config library2config.js library2 library2b js/library2.js
4444
var library2 = cp.spawn("node", join(["../../bin/webpack.js", "--output-pathinfo", "--colors", "--output-library", "library2",
45-
"--output-public-path", "js/", "--config", "library2config.js", "library2", "js/library2.js"], extraArgsNoWatch));
45+
"--output-public-path", "js/", "--config", "library2config.js", "library2", "library2b", "js/library2.js"], extraArgsNoWatch));
4646
bindOutput(library2);

test/browsertest/lib/index.web.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,25 @@ describe("main", function() {
240240
if(firstOne) done();
241241
});
242242
});
243+
244+
it("should handle named chunks", function(done) {
245+
var sync = false;
246+
require.ensure([], function(require) {
247+
require("./empty?a");
248+
require("./empty?b");
249+
sync = true;
250+
testLoad();
251+
sync = false;
252+
done();
253+
}, "named-chunk");
254+
function testLoad() {
255+
require.ensure([], function(require) {
256+
require("./empty?c");
257+
require("./empty?d");
258+
sync.should.be.ok;
259+
}, "named-chunk");
260+
}
261+
});
243262
});
244263

245264
describe("loaders", function() {

test/browsertest/node_modules/library2b.js

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)