Skip to content

Commit b8c47a7

Browse files
authored
Merge pull request webpack#7628 from reduckted/dllreferenceplugin-graceful-json-parse-failure
Catch errors while parsing DLL manifest
2 parents 699fe21 + 5b6f99b commit b8c47a7

File tree

8 files changed

+117
-3
lines changed

8 files changed

+117
-3
lines changed

lib/DllReferencePlugin.js

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const DelegatedModuleFactoryPlugin = require("./DelegatedModuleFactoryPlugin");
1010
const ExternalModuleFactoryPlugin = require("./ExternalModuleFactoryPlugin");
1111
const DelegatedExportsDependency = require("./dependencies/DelegatedExportsDependency");
1212
const NullFactory = require("./NullFactory");
13+
const makePathsRelative = require("./util/identifier").makePathsRelative;
14+
const WebpackError = require("./WebpackError");
1315

1416
const validateOptions = require("schema-utils");
1517
const schema = require("../schemas/plugins/DllReferencePlugin.json");
@@ -43,9 +45,23 @@ class DllReferencePlugin {
4345
params.compilationDependencies.add(manifest);
4446
compiler.inputFileSystem.readFile(manifest, (err, result) => {
4547
if (err) return callback(err);
46-
params["dll reference " + manifest] = parseJson(
47-
result.toString("utf-8")
48-
);
48+
// Catch errors parsing the manifest so that blank
49+
// or malformed manifest files don't kill the process.
50+
try {
51+
params["dll reference " + manifest] = parseJson(
52+
result.toString("utf-8")
53+
);
54+
} catch (e) {
55+
// Store the error in the params so that it can
56+
// be added as a compilation error later on.
57+
const manifestPath = makePathsRelative(
58+
compiler.options.context,
59+
manifest
60+
);
61+
params[
62+
"dll reference parse error " + manifest
63+
] = new DllManifestError(manifestPath, e.message);
64+
}
4965
return callback();
5066
});
5167
} else {
@@ -57,6 +73,12 @@ class DllReferencePlugin {
5773
compiler.hooks.compile.tap("DllReferencePlugin", params => {
5874
let manifest = this.options.manifest;
5975
if (typeof manifest === "string") {
76+
// If there was an error parsing the manifest
77+
// file, exit now because the error will be added
78+
// as a compilation error in the "compilation" hook.
79+
if (params["dll reference parse error " + manifest]) {
80+
return;
81+
}
6082
manifest = params["dll reference " + manifest];
6183
}
6284
const name = this.options.name || manifest.name;
@@ -78,6 +100,32 @@ class DllReferencePlugin {
78100
extensions: this.options.extensions
79101
}).apply(normalModuleFactory);
80102
});
103+
104+
compiler.hooks.compilation.tap(
105+
"DllReferencePlugin",
106+
(compilation, params) => {
107+
let manifest = this.options.manifest;
108+
if (typeof manifest === "string") {
109+
// If there was an error parsing the manifest file, add the
110+
// error as a compilation error to make the compilation fail.
111+
let e = params["dll reference parse error " + manifest];
112+
if (e) {
113+
compilation.errors.push(e);
114+
}
115+
}
116+
}
117+
);
118+
}
119+
}
120+
121+
class DllManifestError extends WebpackError {
122+
constructor(filename, message) {
123+
super();
124+
125+
this.name = "DllManifestError";
126+
this.message = `Dll manifest ${filename}\n${message}`;
127+
128+
Error.captureStackTrace(this, this.constructor);
81129
}
82130
}
83131

test/__snapshots__/StatsTestCases.test.js.snap

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,29 @@ Child
720720
[0] ./index.js 24 bytes {0} [built]"
721721
`;
722722

723+
exports[`StatsTestCases should print correct stats for dll-reference-plugin-issue-7624 1`] = `
724+
"Hash: 29b62432962bce4c54c0
725+
Time: Xms
726+
Built at: Thu Jan 01 1970 00:00:00 GMT
727+
Asset Size Chunks Chunk Names
728+
bundle.js 3.6 KiB 0 [emitted] main
729+
Entrypoint main = bundle.js
730+
[0] ./entry.js 29 bytes {0} [built]"
731+
`;
732+
733+
exports[`StatsTestCases should print correct stats for dll-reference-plugin-issue-7624-error 1`] = `
734+
"Hash: 701dcf62b26d0347b899
735+
Time: Xms
736+
Built at: Thu Jan 01 1970 00:00:00 GMT
737+
Asset Size Chunks Chunk Names
738+
bundle.js 3.6 KiB 0 main
739+
Entrypoint main = bundle.js
740+
[0] ./entry.js 29 bytes {0} [built]
741+
742+
ERROR in Dll manifest blank-manifest.json
743+
Unexpected end of JSON input while parsing near ''"
744+
`;
745+
723746
exports[`StatsTestCases should print correct stats for exclude-with-loader 1`] = `
724747
"Hash: 52eadc5de721f000106b
725748
Time: Xms

test/statsCases/dll-reference-plugin-issue-7624-error/blank-manifest.json

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// Intentionally left blank.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var webpack = require("../../../");
2+
3+
module.exports = {
4+
mode: "production",
5+
entry: "./entry.js",
6+
output: {
7+
filename: "bundle.js"
8+
},
9+
plugins: [
10+
new webpack.DllReferencePlugin({
11+
manifest: __dirname + "/blank-manifest.json",
12+
name: "blank-manifest"
13+
})
14+
]
15+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// Intentionally left blank.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "foo",
3+
"content": {
4+
"./foo.js": {
5+
"id": 0,
6+
"buildMeta": {
7+
"providedExports": true
8+
}
9+
}
10+
}
11+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var webpack = require("../../../");
2+
3+
module.exports = {
4+
mode: "production",
5+
entry: "./entry.js",
6+
output: {
7+
filename: "bundle.js"
8+
},
9+
plugins: [
10+
new webpack.DllReferencePlugin({
11+
manifest: __dirname + "/non-blank-manifest.json",
12+
name: "non-blank-manifest"
13+
})
14+
]
15+
};

0 commit comments

Comments
 (0)