fix named chunks, added multi entry plugin

This commit is contained in:
Tobias Koppers 2013-01-31 01:44:39 +01:00
parent ee01837d66
commit 55d1af8109
10 changed files with 181 additions and 11 deletions

View File

@ -241,15 +241,22 @@ module.exports = function(optimist, argv, convertOptions) {
}
}
if(argv._.length > 1) {
if(argv._.length > 0) {
ensureObject(options, "entry");
function addTo(name, entry) {
if(options.entry[name]) {
if(!Array.isArray(options.entry[name]))
options.entry[name] = [options.entry[name]];
options.entry[name].push(entry);
} else {
options.entry[name] = entry;
}
}
argv._.forEach(function(content) {
var i = content.indexOf("=");
if(i < 0) throw new Error("Each element must be <string>=<string>.");
else return options.entry[content.substr(0, i)] = content.substr(i+1);
if(i < 0) addTo("main", content);
else addTo(content.substr(0, i), content.substr(i+1))
});
} else if(argv._.length == 1) {
options.entry = argv._[0];
}
return options;

View File

@ -267,7 +267,7 @@ Compilation.prototype.seal = function seal(callback) {
Compilation.prototype.addChunk = function addChunk(name) {
if(name) {
if(Object.prototype.hasOwnProperty(this.namedChunks, name))
if(Object.prototype.hasOwnProperty.call(this.namedChunks, name))
return this.namedChunks[name];
}
var chunk = new Chunk(name);

29
lib/MultiEntryPlugin.js Normal file
View File

@ -0,0 +1,29 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
var MultiEntryDependency = require("./dependencies/MultiEntryDependency");
var SingleEntryDependency = require("./dependencies/SingleEntryDependency");
var MultiModuleFactory = require("./MultiModuleFactory");
function MultiEntryPlugin(context, entries, name) {
this.context = context;
this.entries = entries;
this.name = name;
}
module.exports = MultiEntryPlugin;
MultiEntryPlugin.prototype.apply = function(compiler) {
compiler.plugin("compilation", function(compilation, params) {
var multiModuleFactory = new MultiModuleFactory();
var normalModuleFactory = params.normalModuleFactory;
compilation.dependencyFactories.set(MultiEntryDependency, multiModuleFactory);
compilation.dependencyFactories.set(SingleEntryDependency, normalModuleFactory);
});
compiler.plugin("make", function(compilation, callback) {
compilation.addEntry(this.context, new MultiEntryDependency(this.entries.map(function(e) {
return new SingleEntryDependency(e);
}), this.name), this.name, callback);
}.bind(this));
};

69
lib/MultiModule.js Normal file
View File

@ -0,0 +1,69 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
var Module = require("./Module");
var RawSource = require("webpack-core/lib/RawSource");
function MultiModule(context, dependencies, name) {
Module.call(this);
this.context = context;
this.dependencies = dependencies;
this.name = name;
this.built = false;
this.cacheable = true;
}
module.exports = MultiModule;
MultiModule.prototype = Object.create(Module.prototype);
MultiModule.prototype.identifier = function() {
return "multi " + this.name;
};
MultiModule.prototype.readableIdentifier = function(requestShortener) {
return "multi " + this.name;
};
MultiModule.prototype.disconnect = function disconnect() {
this.built = false;
Module.prototype.disconnect.call(this);
};
MultiModule.prototype.build = function build(options, compilation, resolver, fs, callback) {
this.built = true;
return callback();
};
MultiModule.prototype.source = function(dependencyTemplates, outputOptions, requestShortener) {
var str = ["module.exports = "];
this.dependencies.forEach(function(dep, idx) {
if(dep.module) {
str.push("require(");
if(outputOptions.pathinfo)
str.push("/*! "+dep.request+" */");
str.push(""+dep.module.id);
str.push(")");
} else {
str.push("(function webpackMissingModule() { throw new Error(");
str.push(JSON.stringify("Cannot find module \"" + dep.request + "\""));
str.push("); }())");
}
str.push(";\n");
});
return new RawSource(str.join(""));
};
MultiModule.prototype.needRebuild = function needRebuild(fileTimestamps, contextTimestamps) {
return false;
};
MultiModule.prototype.size = function() {
return 16 + this.dependencies.length * 12;
};
MultiModule.prototype.updateHash = function(hash) {
hash.update("multi module");
hash.update(this.name || "");
Module.prototype.updateHash.call(this, hash);
};

18
lib/MultiModuleFactory.js Normal file
View File

@ -0,0 +1,18 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
var async = require("async");
var Tapable = require("tapable");
var MultiModule = require("./MultiModule");
function MultiModuleFactory() {
Tapable.call(this);
}
module.exports = MultiModuleFactory;
MultiModuleFactory.prototype = Object.create(Tapable.prototype);
MultiModuleFactory.prototype.create = function(context, dependency, callback) {
callback(null, new MultiModule(context, dependency.dependencies, dependency.name));
};

View File

@ -10,6 +10,7 @@ var EvalDevToolModulePlugin = require("./EvalDevToolModulePlugin");
var LibraryTemplatePlugin = require("./LibraryTemplatePlugin");
var SingleEntryPlugin = require("./SingleEntryPlugin");
var MultiEntryPlugin = require("./MultiEntryPlugin");
var CachePlugin = require("./CachePlugin");
var UglifyJsPlugin = require("./UglifyJsPlugin");
@ -59,11 +60,17 @@ WebpackOptionsApply.prototype.process = function(options, compiler) {
compiler.apply(new LibraryTemplatePlugin(options.output.library, options.output.libraryTarget));
if(options.devtool == "eval")
compiler.apply(new EvalDevToolModulePlugin());
if(typeof options.entry == "string") {
compiler.apply(new SingleEntryPlugin(options.context, options.entry, "main"));
function itemToPlugin(item, name) {
if(Array.isArray(item))
return new MultiEntryPlugin(options.context, item, name);
else
return new SingleEntryPlugin(options.context, item, name)
}
if(typeof options.entry == "string" || Array.isArray(options.entry)) {
compiler.apply(itemToPlugin(options.entry, "main"));
} else if(typeof options.entry == "object") {
Object.keys(options.entry).forEach(function(name) {
compiler.apply(new SingleEntryPlugin(options.context, options.entry[name], name));
compiler.apply(itemToPlugin(options.entry[name], name));
});
}
compiler.apply(

View File

@ -0,0 +1,16 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
var Dependency = require("../Dependency");
function MultiEntryDependency(dependencies, name) {
Dependency.call(this);
this.Class = MultiEntryDependency;
this.dependencies = dependencies;
this.name = name;
}
module.exports = MultiEntryDependency;
MultiEntryDependency.prototype = Object.create(Dependency.prototype);
MultiEntryDependency.prototype.type = "multi entry";

View File

@ -40,7 +40,7 @@ library1.on("exit", function(code) {
bindOutput(main);
}
});
// node ../../bin/webpack --output-pathinfo --colors --output-library library2 --output-public-path js/ --config library2config.js library2 js/library2.js
// node ../../bin/webpack --output-pathinfo --colors --output-library library2 --output-public-path js/ --config library2config.js library2 library2b js/library2.js
var library2 = cp.spawn("node", join(["../../bin/webpack.js", "--output-pathinfo", "--colors", "--output-library", "library2",
"--output-public-path", "js/", "--config", "library2config.js", "library2", "js/library2.js"], extraArgsNoWatch));
"--output-public-path", "js/", "--config", "library2config.js", "library2", "library2b", "js/library2.js"], extraArgsNoWatch));
bindOutput(library2);

View File

@ -240,6 +240,25 @@ describe("main", function() {
if(firstOne) done();
});
});
it("should handle named chunks", function(done) {
var sync = false;
require.ensure([], function(require) {
require("./empty?a");
require("./empty?b");
sync = true;
testLoad();
sync = false;
done();
}, "named-chunk");
function testLoad() {
require.ensure([], function(require) {
require("./empty?c");
require("./empty?d");
sync.should.be.ok;
}, "named-chunk");
}
});
});
describe("loaders", function() {

5
test/browsertest/node_modules/library2b.js generated vendored Normal file
View File

@ -0,0 +1,5 @@
describe("library2b", function() {
it("should load this library", function() {
true.should.be.ok;
});
});