Skip to content

Commit 41a1dfe

Browse files
committed
Merge branch 'master' into add/setimmediate
2 parents 4127d2e + cd21838 commit 41a1dfe

File tree

9 files changed

+112
-6
lines changed

9 files changed

+112
-6
lines changed

LICENSE

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(The MIT License)
22

3-
Copyright (c) 2012 - 2014 Tobias Koppers
3+
Copyright (c) 2012 - 2015 Tobias Koppers
44

55
Permission is hereby granted, free of charge, to any person obtaining
66
a copy of this software and associated documentation files (the
@@ -19,4 +19,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
1919
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
2020
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
2121
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22-
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

lib/Compiler.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
5+
var path = require("path");
56
var clone = require("clone");
67
var Tapable = require("tapable");
78

@@ -235,9 +236,9 @@ Compiler.prototype.emitAssets = function(compilation, callback) {
235236
if(queryStringIdx >= 0) {
236237
targetFile = targetFile.substr(0, queryStringIdx);
237238
}
238-
if(targetFile.indexOf("/") >= 0) {
239-
var idx = targetFile.lastIndexOf("/");
240-
var dir = targetFile.substr(0, idx);
239+
240+
if(targetFile.match(/\/|\\/)) {
241+
var dir = path.dirname(targetFile);
241242
this.outputFileSystem.mkdirp(this.outputFileSystem.join(outputPath, dir), writeOut.bind(this));
242243
} else writeOut.call(this);
243244
function writeOut(err) {

lib/EnvironmentPlugin.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Simen Brekken @simenbrekken
4+
*/
5+
var DefinePlugin = require("./DefinePlugin");
6+
7+
function EnvironmentPlugin(keys) {
8+
this.keys = Array.isArray(keys) ? keys : Array.prototype.slice.call(arguments);
9+
}
10+
module.exports = EnvironmentPlugin;
11+
12+
EnvironmentPlugin.prototype.apply = function(compiler) {
13+
compiler.apply(new DefinePlugin(this.keys.reduce(function(definitions, key) {
14+
var value = process.env[key];
15+
16+
if (value === undefined) {
17+
compiler.plugin("this-compilation", function(compilation) {
18+
var error = new Error(key + " environment variable is undefined.");
19+
error.name = "EnvVariableNotDefinedError";
20+
compilation.warnings.push(error);
21+
});
22+
}
23+
24+
definitions["process.env." + key] = value ? JSON.stringify(value) : "undefined";
25+
26+
return definitions;
27+
}, {})));
28+
};

lib/webpack.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ exportPlugins(exports, ".", [
7777
"UmdMainTemplatePlugin",
7878
"NoErrorsPlugin",
7979
"NewWatchingPlugin",
80+
"EnvPlugin"
8081
]);
8182
exportPlugins(exports.optimize = {}, "./optimize", [
8283
"AggressiveMergingPlugin",

test/Compiler.test.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ describe("Compiler", function() {
1212
options.entry = entry;
1313
options.context = path.join(__dirname, "fixtures");
1414
options.output.pathinfo = true;
15+
var logs = {
16+
mkdirp: [],
17+
writeFile: [],
18+
};
1519

1620
var c = new Compiler();
1721
c.options = new WebpackOptionsApply().process(options, c);
@@ -20,9 +24,11 @@ describe("Compiler", function() {
2024
c.outputFileSystem = {
2125
join: path.join.bind(path),
2226
mkdirp: function(path, callback) {
27+
logs.mkdirp.push(path);
2328
callback();
2429
},
2530
writeFile: function(name, content, callback) {
31+
logs.writeFile.push(name, content);
2632
files[name] = content.toString("utf-8");
2733
callback();
2834
}
@@ -44,9 +50,26 @@ describe("Compiler", function() {
4450
stats.errors[0].should.be.instanceOf(Error);
4551
throw stats.errors[0];
4652
}
53+
stats.logs = logs;
4754
callback(stats, files);
4855
});
4956
}
57+
it("should compile a single file to deep output", function(done) {
58+
var sep = path.sep;
59+
60+
compile("./c", {
61+
output: {
62+
path: 'what',
63+
filename: 'the' + sep + 'hell.js',
64+
}
65+
}, function(stats, files) {
66+
stats.logs.mkdirp.should.eql([
67+
'what',
68+
'what' + sep + 'the',
69+
]);
70+
done();
71+
});
72+
});
5073
it("should compile a single file", function(done) {
5174
compile("./c", {}, function(stats, files) {
5275
files.should.have.property("bundle.js").have.type("string");
@@ -135,4 +158,4 @@ describe("Compiler", function() {
135158
done();
136159
});
137160
});
138-
});
161+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module.exports = [
2+
[/(aaa)/, /module bbb/],
3+
[/(aaa)/, /module ccc/],
4+
[/(aaa)/, /module ddd/],
5+
[/(bbbccc)/, /module aaa/],
6+
[/(bbbccc)/, /module ddd/],
7+
[/(ddd)/, /module aaa/],
8+
[/(ddd)/, /module bbb/],
9+
[/(ddd)/, /module ccc/],
10+
[/(ddd)/, /module ddd/],
11+
];
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
it("should import a single process.env var", function() {
2+
if(process.env.AAA !== "aaa")
3+
require.include("aaa");
4+
});
5+
6+
it("should import multiple process.env vars", function() {
7+
if(process.env.BBB !== "bbb")
8+
require.include("bbb");
9+
if(process.env.CCC !== "ccc")
10+
require.include("ccc");
11+
});
12+
13+
it("should warn when a process.env variable is undefined", function() {
14+
if(process.env.DDD !== "ddd")
15+
require.include("ddd");
16+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = [
2+
[/(ddd)/, /DDD environment variable is undefined./]
3+
];
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var EnvironmentPlugin = require("../../../../lib/EnvironmentPlugin");
2+
process.env.AAA = "aaa";
3+
process.env.BBB = "bbb";
4+
process.env.CCC = "ccc";
5+
module.exports = [{
6+
name: "aaa",
7+
module: { unknownContextRegExp: /$^/, unknownContextCritical: false },
8+
plugins: [
9+
new EnvironmentPlugin("AAA")
10+
]
11+
}, {
12+
name: "bbbccc",
13+
module: { unknownContextRegExp: /$^/, unknownContextCritical: false },
14+
plugins: [
15+
new EnvironmentPlugin("BBB", "CCC")
16+
]
17+
}, {
18+
name: "ddd",
19+
module: { unknownContextRegExp: /$^/, unknownContextCritical: false },
20+
plugins: [
21+
new EnvironmentPlugin("DDD")
22+
]
23+
}];

0 commit comments

Comments
 (0)