Skip to content

Commit d66fb0d

Browse files
authored
Merge pull request webpack#4149 from timse/fail-early-on-non-absolute-context
fail if the given context is not absolute
2 parents be4f27c + b32054d commit d66fb0d

7 files changed

Lines changed: 147 additions & 47 deletions

‎lib/WebpackOptionsValidationError.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ class WebpackOptionsValidationError extends Error {
181181
return `${dataPath} should not be empty.`;
182182
else
183183
return `${dataPath} ${err.message}`;
184+
} else if(err.keyword === "absolutePath") {
185+
return `${dataPath}: ${err.message}`;
184186
} else {
185187
// eslint-disable-line no-fallthrough
186188
return `${dataPath} ${err.message} (${JSON.stringify(err, 0, 2)}).\n${getSchemaPartText(err.parentSchema)}`;

‎lib/validateSchema.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const ajv = new Ajv({
1111
verbose: true
1212
});
1313
require("ajv-keywords")(ajv, ["instanceof"]);
14+
require("../schemas/ajv.absolutePath")(ajv);
1415

1516
function validateSchema(schema, options) {
1617
if(Array.isArray(options)) {

‎schemas/ajv.absolutePath.js‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"use strict";
2+
3+
const getErrorFor = (shouldBeAbsolute, data, schema) => {
4+
const message = shouldBeAbsolute ?
5+
`The provided value ${JSON.stringify(data)} is not an absolute path!\n`
6+
: `A relative path is expected. However the provided value ${JSON.stringify(data)} is an absolute path!\n`;
7+
8+
return {
9+
keyword: "absolutePath",
10+
params: { absolutePath: data },
11+
message: message,
12+
parentSchema: schema,
13+
};
14+
};
15+
module.exports = (ajv) => ajv.addKeyword("absolutePath", {
16+
errors: true,
17+
type: "string",
18+
compile(expected, schema) {
19+
function callback(data) {
20+
const passes = expected === /^(?:[A-Z]:\\|\/)/.test(data);
21+
if(!passes) {
22+
callback.errors = [getErrorFor(expected, data, schema)];
23+
}
24+
return passes;
25+
}
26+
callback.errors = [];
27+
return callback;
28+
}
29+
});

‎schemas/webpackOptionsSchema.json‎

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,18 @@
158158
},
159159
{
160160
"instanceof": "RegExp"
161+
},
162+
{
163+
"items": {
164+
"type": "string",
165+
"absolutePath": true
166+
},
167+
"minItems": 1,
168+
"type": "array"
169+
},
170+
{
171+
"type": "string",
172+
"absolutePath": true
161173
}
162174
]
163175
},
@@ -244,7 +256,8 @@
244256
},
245257
"chunkFilename": {
246258
"description": "The filename of non-entry chunks as relative path inside the `output.path` directory.",
247-
"type": "string"
259+
"type": "string",
260+
"absolutePath": false
248261
},
249262
"crossOriginLoading": {
250263
"description": "This option enables cross-origin loading of chunks.",
@@ -302,7 +315,8 @@
302315
},
303316
"filename": {
304317
"description": "Specifies the name of each output file on disk. You must **not** specify an absolute path here! The `output.path` option determines the location on disk the files are written to, filename is used solely for naming the individual files.",
305-
"type": "string"
318+
"type": "string",
319+
"absolutePath": false
306320
},
307321
"hashDigest": {
308322
"minLength": 1,
@@ -318,15 +332,17 @@
318332
},
319333
"hotUpdateChunkFilename": {
320334
"description": "The filename of the Hot Update Chunks. They are inside the output.path directory.",
321-
"type": "string"
335+
"type": "string",
336+
"absolutePath": false
322337
},
323338
"hotUpdateFunction": {
324339
"description": "The JSONP function used by webpack for async loading of hot update chunks.",
325340
"type": "string"
326341
},
327342
"hotUpdateMainFilename": {
328343
"description": "The filename of the Hot Update Main File. It is inside the `output.path` directory.",
329-
"type": "string"
344+
"type": "string",
345+
"absolutePath": false
330346
},
331347
"jsonpFunction": {
332348
"description": "The JSONP function used by webpack for async loading of chunks.",
@@ -364,7 +380,8 @@
364380
},
365381
"path": {
366382
"description": "The output directory as **absolute path** (required).",
367-
"type": "string"
383+
"type": "string",
384+
"absolutePath": true
368385
},
369386
"pathinfo": {
370387
"description": "Include comments with information about the modules.",
@@ -376,7 +393,8 @@
376393
},
377394
"sourceMapFilename": {
378395
"description": "The filename of the SourceMaps for the JavaScript files. They are inside the `output.path` directory.",
379-
"type": "string"
396+
"type": "string",
397+
"absolutePath": false
380398
},
381399
"sourcePrefix": {
382400
"description": "Prefixes every line of the source in the bundle with this string.",
@@ -550,13 +568,22 @@
550568
]
551569
},
552570
"exclude": {
553-
"$ref": "#/definitions/ruleSet-condition"
571+
"allOf": [
572+
{"$ref": "#/definitions/ruleSet-condition"},
573+
{"absolutePath": true}
574+
]
554575
},
555576
"include": {
556-
"$ref": "#/definitions/ruleSet-condition"
577+
"allOf": [
578+
{"$ref": "#/definitions/ruleSet-condition"},
579+
{"absolutePath": true}
580+
]
557581
},
558582
"issuer": {
559-
"$ref": "#/definitions/ruleSet-condition"
583+
"allOf": [
584+
{"$ref": "#/definitions/ruleSet-condition"},
585+
{"absolutePath": true}
586+
]
560587
},
561588
"loader": {
562589
"anyOf": [
@@ -585,7 +612,10 @@
585612
"$ref": "#/definitions/ruleSet-query"
586613
},
587614
"resource": {
588-
"$ref": "#/definitions/ruleSet-condition"
615+
"allOf": [
616+
{"$ref": "#/definitions/ruleSet-condition"},
617+
{"absolutePath": true}
618+
]
589619
},
590620
"resourceQuery": {
591621
"$ref": "#/definitions/ruleSet-condition"
@@ -594,7 +624,10 @@
594624
"$ref": "#/definitions/ruleSet-rules"
595625
},
596626
"test": {
597-
"$ref": "#/definitions/ruleSet-condition"
627+
"allOf": [
628+
{"$ref": "#/definitions/ruleSet-condition"},
629+
{"absolutePath": true}
630+
]
598631
},
599632
"use": {
600633
"$ref": "#/definitions/ruleSet-use"
@@ -673,7 +706,8 @@
673706
},
674707
"context": {
675708
"description": "The base directory (absolute path!) for resolving the `entry` option. If `output.pathinfo` is set, the included pathinfo is shortened to this directory.",
676-
"type": "string"
709+
"type": "string",
710+
"absolutePath": true
677711
},
678712
"dependencies": {
679713
"description": "References to other configurations to depend on.",
@@ -816,15 +850,18 @@
816850
},
817851
"recordsInputPath": {
818852
"description": "Store compiler state to a json file.",
819-
"type": "string"
853+
"type": "string",
854+
"absolutePath": true
820855
},
821856
"recordsOutputPath": {
822857
"description": "Load compiler state from a json file.",
823-
"type": "string"
858+
"type": "string",
859+
"absolutePath": true
824860
},
825861
"recordsPath": {
826862
"description": "Store/Load compiler state from/to a json file. This will result in persistent ids of modules and chunks. An absolute path is expected. `recordsPath` is used for `recordsInputPath` and `recordsOutputPath` if they left undefined.",
827-
"type": "string"
863+
"type": "string",
864+
"absolutePath": true
828865
},
829866
"resolve": {
830867
"$ref": "#/definitions/resolve"

‎test/Compiler-caching.test.js‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* globals describe, it, before, after */
12
"use strict";
23

34
const should = require("should");
@@ -14,7 +15,7 @@ describe("Compiler (caching)", function() {
1415
new WebpackOptionsDefaulter().process(options);
1516
options.entry = entry;
1617
options.context = path.join(__dirname, "fixtures");
17-
options.output.path = "";
18+
options.output.path = "/";
1819
options.output.filename = "bundle.js";
1920
options.output.pathinfo = true;
2021
const logs = {
@@ -25,7 +26,9 @@ describe("Compiler (caching)", function() {
2526
const c = webpack(options);
2627
const files = {};
2728
c.outputFileSystem = {
28-
join: path.join.bind(path),
29+
join: function() {
30+
return [].join.call(arguments, "/").replace(/\/+/g, "/");
31+
},
2932
mkdirp: function(path, callback) {
3033
logs.mkdirp.push(path);
3134
callback();
@@ -188,12 +191,11 @@ describe("Compiler (caching)", function() {
188191
stats.assets[0].emitted.should.be.exactly(true);
189192

190193
helper.runAgain((stats, files, iteration) => {
191-
192194
// Cached the second run
193195
stats.assets[0].name.should.be.exactly("bundle.js");
194196
stats.assets[0].emitted.should.be.exactly(false);
195197

196-
files["bundle.js"].should.containEql("This is a");
198+
files["/bundle.js"].should.containEql("This is a");
197199

198200
const aContent = fs.readFileSync(tempFixture.aFilepath).toString().replace("This is a", "This is a MODIFIED");
199201

@@ -205,7 +207,7 @@ describe("Compiler (caching)", function() {
205207
stats.assets[0].name.should.be.exactly("bundle.js");
206208
stats.assets[0].emitted.should.be.exactly(true);
207209

208-
files["bundle.js"].should.containEql("This is a MODIFIED");
210+
files["/bundle.js"].should.containEql("This is a MODIFIED");
209211

210212
done();
211213
});

‎test/Compiler.test.js‎

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1+
/* globals describe, it */
12
var should = require("should");
23
var path = require("path");
34

4-
var NodeEnvironmentPlugin = require("../lib/node/NodeEnvironmentPlugin");
55
var webpack = require("../");
6-
var WebpackOptionsApply = require("../lib/WebpackOptionsApply");
76
var WebpackOptionsDefaulter = require("../lib/WebpackOptionsDefaulter");
87

98
describe("Compiler", function() {
@@ -12,7 +11,7 @@ describe("Compiler", function() {
1211
new WebpackOptionsDefaulter().process(options);
1312
options.entry = entry;
1413
options.context = path.join(__dirname, "fixtures");
15-
if(noOutputPath) options.output.path = "";
14+
if(noOutputPath) options.output.path = "/";
1615
options.output.pathinfo = true;
1716
var logs = {
1817
mkdirp: [],
@@ -22,7 +21,9 @@ describe("Compiler", function() {
2221
var c = webpack(options);
2322
var files = {};
2423
c.outputFileSystem = {
25-
join: path.join.bind(path),
24+
join: function() {
25+
return [].join.call(arguments, "/").replace(/\/+/g, "/");
26+
},
2627
mkdirp: function(path, callback) {
2728
logs.mkdirp.push(path);
2829
callback();
@@ -56,26 +57,25 @@ describe("Compiler", function() {
5657
});
5758
}
5859
it("should compile a single file to deep output", function(done) {
59-
var sep = path.sep;
60-
6160
compile("./c", {
6261
output: {
63-
path: 'what',
64-
filename: 'the' + sep + 'hell.js',
62+
path: "/what",
63+
filename: "the/hell.js",
6564
}
6665
}, function(stats, files) {
6766
stats.logs.mkdirp.should.eql([
68-
'what',
69-
'what' + sep + 'the',
67+
"/what",
68+
"/what/the",
7069
]);
7170
done();
7271
});
7372
});
73+
7474
it("should compile a single file", function(done) {
7575
compile("./c", {}, function(stats, files) {
76-
files.should.have.property("main.js").have.type("string");
77-
Object.keys(files).should.be.eql(["main.js"]);
78-
var bundle = files["main.js"];
76+
files.should.have.property("/main.js").have.type("string");
77+
Object.keys(files).should.be.eql(["/main.js"]);
78+
var bundle = files["/main.js"];
7979
bundle.should.containEql("function __webpack_require__(");
8080
bundle.should.containEql("__webpack_require__(/*! ./a */ 0);");
8181
bundle.should.containEql("./c.js");
@@ -89,11 +89,12 @@ describe("Compiler", function() {
8989
done();
9090
});
9191
});
92+
9293
it("should compile a complex file", function(done) {
9394
compile("./main1", {}, function(stats, files) {
94-
files.should.have.property("main.js").have.type("string");
95-
Object.keys(files).should.be.eql(["main.js"]);
96-
var bundle = files["main.js"];
95+
files.should.have.property("/main.js").have.type("string");
96+
Object.keys(files).should.be.eql(["/main.js"]);
97+
var bundle = files["/main.js"];
9798
bundle.should.containEql("function __webpack_require__(");
9899
bundle.should.containEql("__webpack_require__(/*! ./a */");
99100
bundle.should.containEql("./main1.js");
@@ -110,11 +111,12 @@ describe("Compiler", function() {
110111
done();
111112
});
112113
});
114+
113115
it("should compile a file with transitive dependencies", function(done) {
114116
compile("./abc", {}, function(stats, files) {
115-
files.should.have.property("main.js").have.type("string");
116-
Object.keys(files).should.be.eql(["main.js"]);
117-
var bundle = files["main.js"];
117+
files.should.have.property("/main.js").have.type("string");
118+
Object.keys(files).should.be.eql(["/main.js"]);
119+
var bundle = files["/main.js"];
118120
bundle.should.containEql("function __webpack_require__(");
119121
bundle.should.containEql("__webpack_require__(/*! ./a */");
120122
bundle.should.containEql("__webpack_require__(/*! ./b */");
@@ -133,14 +135,15 @@ describe("Compiler", function() {
133135
done();
134136
});
135137
});
138+
136139
it("should compile a file with multiple chunks", function(done) {
137140
compile("./chunks", {}, function(stats, files) {
138141
stats.chunks.length.should.be.eql(2);
139-
files.should.have.property("main.js").have.type("string");
140-
files.should.have.property("0.js").have.type("string");
141-
Object.keys(files).should.be.eql(["0.js", "main.js"]);
142-
var bundle = files["main.js"];
143-
var chunk = files["0.js"];
142+
files.should.have.property("/main.js").have.type("string");
143+
files.should.have.property("/0.js").have.type("string");
144+
Object.keys(files).should.be.eql(["/0.js", "/main.js"]);
145+
var bundle = files["/main.js"];
146+
var chunk = files["/0.js"];
144147
bundle.should.containEql("function __webpack_require__(");
145148
bundle.should.containEql("__webpack_require__(/*! ./b */");
146149
chunk.should.not.containEql("__webpack_require__(/* ./b */");

0 commit comments

Comments
 (0)