added provide plugin and option

This commit is contained in:
Tobias Koppers 2013-02-10 20:37:30 +01:00
parent 34424b36af
commit 05152cee7f
4 changed files with 47 additions and 13 deletions

18
lib/ProvidePlugin.js Normal file
View File

@ -0,0 +1,18 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
var ModuleParserHelpers = require("./ModuleParserHelpers");
function ProvidePlugin(name, request) {
this.name = name;
this.request = request;
}
module.exports = ProvidePlugin;
ProvidePlugin.prototype.apply = function(compiler) {
var name = this.name;
var request = this.request;
compiler.parser.plugin("expression " + name, function(expr) {
return ModuleParserHelpers.addParsedVariable(this, name, "require(" + JSON.stringify(request) + ")");
});
};

View File

@ -19,6 +19,7 @@ var ConsolePlugin = require("./ConsolePlugin");
var APIPlugin = require("./APIPlugin");
var ConstPlugin = require("./ConstPlugin");
var CompatibilityPlugin = require("./CompatibilityPlugin");
var ProvidePlugin = require("./ProvidePlugin");
var CommonJsPlugin = require("./dependencies/CommonJsPlugin");
var AMDPlugin = require("./dependencies/AMDPlugin");
@ -102,12 +103,18 @@ WebpackOptionsApply.prototype.process = function(options, compiler) {
if(options.cache === undefined ? options.watch : options.cache)
compiler.apply(new CachePlugin(typeof options.cache == "object" ? options.cache : null));
if(typeof options.provide === "object") {
for(var name in options.provide) {
compiler.apply(new ProvidePlugin(name, options.provide[name]));
}
}
compiler.applyPlugins("after-plugins", compiler);
compiler.resolvers.normal.apply(
new ModuleAliasPlugin(options.resolve.alias),
makeRootPlugin(options.resolve.root),
makeRootPlugin("module", options.resolve.root),
new ModulesInDirectoriesPlugin("module", options.resolve.modulesDirectories),
makeRootPlugin(options.resolve.fallback),
makeRootPlugin("module", options.resolve.fallback),
new ModuleAsFilePlugin("module"),
new ModuleAsDirectoryPlugin("module"),
new DirectoryDescriptionFilePlugin("package.json", ["webpack", "browserify", "web", ["jam", "main"], "main"]),
@ -116,18 +123,18 @@ WebpackOptionsApply.prototype.process = function(options, compiler) {
);
compiler.resolvers.context.apply(
new ModuleAliasPlugin(options.resolve.alias),
makeRootPlugin(options.resolve.root),
makeRootPlugin("module", options.resolve.root),
new ModulesInDirectoriesPlugin("module", options.resolve.modulesDirectories),
makeRootPlugin(options.resolve.fallback),
makeRootPlugin("module", options.resolve.fallback),
new ModuleAsFilePlugin("module"),
new ModuleAsDirectoryPlugin("module"),
new DirectoryResultPlugin()
);
compiler.resolvers.loader.apply(
new ModuleAliasPlugin(options.resolveLoader.alias),
makeRootPlugin(options.resolveLoader.root),
makeRootPlugin("loader-module", options.resolveLoader.root),
new ModulesInDirectoriesPlugin("loader-module", options.resolveLoader.modulesDirectories),
makeRootPlugin(options.resolveLoader.fallback),
makeRootPlugin("loader-module", options.resolveLoader.fallback),
new ModuleTemplatesPlugin("loader-module", options.resolveLoader.moduleTemplates, "module"),
new ModuleAsFilePlugin("module"),
new ModuleAsDirectoryPlugin("module"),
@ -139,9 +146,9 @@ WebpackOptionsApply.prototype.process = function(options, compiler) {
return options;
}
function makeRootPlugin(root) {
function makeRootPlugin(name, root) {
if(typeof root === "string")
return new ModulesInRootPlugin("module", root);
return new ModulesInRootPlugin(name, root);
else if(Array.isArray(root)) {
return function() {
root.forEach(function(root) {

View File

@ -14,6 +14,9 @@ module.exports = {
amd: {
fromOptions: true
},
provide: {
s3: "submodule3"
},
plugins: [
function() {
this.plugin("normal-module-factory", function(nmf) {

View File

@ -24,28 +24,28 @@ describe("library2", function() {
});
sameTick1 = false;
});
it("should load stuff with require.ensure asynchron", function() {
should.exist(tickExtra);
tickExtra.should.be.eql(false);
});
it("should load not include stuff from parent, remove empty chunks and apply a post loader", function() {
should.exist(tickEmpty);
tickEmpty.should.be.eql(true);
extraValue.should.be.eql("Lib2 extra2 with post loader");
});
it("should merge chunks if maxChunks specified", function() {
should.exist(tickEmpty);
tickEmpty.should.be.eql(true);
testValue.should.be.eql("test module");
});
it("should load require.amd from options", function() {
require.amd.should.have.property("fromOptions").be.eql(true);
});
it("should run empty AMD require", function() {
var emptyRequire = false;
require([], function() {
@ -53,5 +53,11 @@ describe("library2", function() {
});
emptyRequire.should.be.eql(true);
});
it("should provide free variables", function() {
s3().should.be.eql("submodule3");
});
});
exports.ok = true;
// it should not fail if comment in last line