Skip to content

Commit 05152ce

Browse files
committed
added provide plugin and option
1 parent 34424b3 commit 05152ce

File tree

4 files changed

+47
-13
lines changed

4 files changed

+47
-13
lines changed

lib/ProvidePlugin.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 ModuleParserHelpers = require("./ModuleParserHelpers");
6+
7+
function ProvidePlugin(name, request) {
8+
this.name = name;
9+
this.request = request;
10+
}
11+
module.exports = ProvidePlugin;
12+
ProvidePlugin.prototype.apply = function(compiler) {
13+
var name = this.name;
14+
var request = this.request;
15+
compiler.parser.plugin("expression " + name, function(expr) {
16+
return ModuleParserHelpers.addParsedVariable(this, name, "require(" + JSON.stringify(request) + ")");
17+
});
18+
};

lib/WebpackOptionsApply.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var ConsolePlugin = require("./ConsolePlugin");
1919
var APIPlugin = require("./APIPlugin");
2020
var ConstPlugin = require("./ConstPlugin");
2121
var CompatibilityPlugin = require("./CompatibilityPlugin");
22+
var ProvidePlugin = require("./ProvidePlugin");
2223

2324
var CommonJsPlugin = require("./dependencies/CommonJsPlugin");
2425
var AMDPlugin = require("./dependencies/AMDPlugin");
@@ -102,12 +103,18 @@ WebpackOptionsApply.prototype.process = function(options, compiler) {
102103
if(options.cache === undefined ? options.watch : options.cache)
103104
compiler.apply(new CachePlugin(typeof options.cache == "object" ? options.cache : null));
104105

106+
if(typeof options.provide === "object") {
107+
for(var name in options.provide) {
108+
compiler.apply(new ProvidePlugin(name, options.provide[name]));
109+
}
110+
}
111+
105112
compiler.applyPlugins("after-plugins", compiler);
106113
compiler.resolvers.normal.apply(
107114
new ModuleAliasPlugin(options.resolve.alias),
108-
makeRootPlugin(options.resolve.root),
115+
makeRootPlugin("module", options.resolve.root),
109116
new ModulesInDirectoriesPlugin("module", options.resolve.modulesDirectories),
110-
makeRootPlugin(options.resolve.fallback),
117+
makeRootPlugin("module", options.resolve.fallback),
111118
new ModuleAsFilePlugin("module"),
112119
new ModuleAsDirectoryPlugin("module"),
113120
new DirectoryDescriptionFilePlugin("package.json", ["webpack", "browserify", "web", ["jam", "main"], "main"]),
@@ -116,18 +123,18 @@ WebpackOptionsApply.prototype.process = function(options, compiler) {
116123
);
117124
compiler.resolvers.context.apply(
118125
new ModuleAliasPlugin(options.resolve.alias),
119-
makeRootPlugin(options.resolve.root),
126+
makeRootPlugin("module", options.resolve.root),
120127
new ModulesInDirectoriesPlugin("module", options.resolve.modulesDirectories),
121-
makeRootPlugin(options.resolve.fallback),
128+
makeRootPlugin("module", options.resolve.fallback),
122129
new ModuleAsFilePlugin("module"),
123130
new ModuleAsDirectoryPlugin("module"),
124131
new DirectoryResultPlugin()
125132
);
126133
compiler.resolvers.loader.apply(
127134
new ModuleAliasPlugin(options.resolveLoader.alias),
128-
makeRootPlugin(options.resolveLoader.root),
135+
makeRootPlugin("loader-module", options.resolveLoader.root),
129136
new ModulesInDirectoriesPlugin("loader-module", options.resolveLoader.modulesDirectories),
130-
makeRootPlugin(options.resolveLoader.fallback),
137+
makeRootPlugin("loader-module", options.resolveLoader.fallback),
131138
new ModuleTemplatesPlugin("loader-module", options.resolveLoader.moduleTemplates, "module"),
132139
new ModuleAsFilePlugin("module"),
133140
new ModuleAsDirectoryPlugin("module"),
@@ -139,9 +146,9 @@ WebpackOptionsApply.prototype.process = function(options, compiler) {
139146
return options;
140147
}
141148

142-
function makeRootPlugin(root) {
149+
function makeRootPlugin(name, root) {
143150
if(typeof root === "string")
144-
return new ModulesInRootPlugin("module", root);
151+
return new ModulesInRootPlugin(name, root);
145152
else if(Array.isArray(root)) {
146153
return function() {
147154
root.forEach(function(root) {

test/browsertest/library2config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ module.exports = {
1414
amd: {
1515
fromOptions: true
1616
},
17+
provide: {
18+
s3: "submodule3"
19+
},
1720
plugins: [
1821
function() {
1922
this.plugin("normal-module-factory", function(nmf) {

test/browsertest/node_modules/library2/lib/main.js

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

0 commit comments

Comments
 (0)