Catch errors while parsing DLL manifest

If the DLL manifest is blank or malformed, parsing it will throw
an error, and if left uncaught will kill the process if webpack is
in watch mode. Catching the error and adding it to the compilation's
errors will result in a failed compilation, but will keep the
process running while in watch mode.
This commit is contained in:
reduckted 2018-06-30 17:54:47 +10:00
parent b06cca2371
commit cb553050c3
8 changed files with 103 additions and 3 deletions

View File

@ -10,6 +10,7 @@ const DelegatedModuleFactoryPlugin = require("./DelegatedModuleFactoryPlugin");
const ExternalModuleFactoryPlugin = require("./ExternalModuleFactoryPlugin");
const DelegatedExportsDependency = require("./dependencies/DelegatedExportsDependency");
const NullFactory = require("./NullFactory");
const makePathsRelative = require("./util/identifier").makePathsRelative;
const validateOptions = require("schema-utils");
const schema = require("../schemas/plugins/DllReferencePlugin.json");
@ -43,9 +44,21 @@ class DllReferencePlugin {
params.compilationDependencies.add(manifest);
compiler.inputFileSystem.readFile(manifest, (err, result) => {
if (err) return callback(err);
params["dll reference " + manifest] = parseJson(
result.toString("utf-8")
);
// Catch errors parsing the manifest so that blank
// or malformed manifest files don't kill the process.
try {
params["dll reference " + manifest] = parseJson(
result.toString("utf-8")
);
} catch (e) {
// Store the error in the params so that it can
// be added as a compilation error later on.
const manifestPath = this.options.context
? makePathsRelative(this.options.context, manifest)
: manifest;
e.message = `Dll manifest ${manifestPath}\n${e.message}`;
params["dll reference parse error " + manifest] = e;
}
return callback();
});
} else {
@ -57,6 +70,12 @@ class DllReferencePlugin {
compiler.hooks.compile.tap("DllReferencePlugin", params => {
let manifest = this.options.manifest;
if (typeof manifest === "string") {
// If there was an error parsing the manifest
// file, exit now because the error will be added
// as a compilation error in the "compilation" hook.
if (params["dll reference parse error " + manifest]) {
return;
}
manifest = params["dll reference " + manifest];
}
const name = this.options.name || manifest.name;
@ -78,6 +97,21 @@ class DllReferencePlugin {
extensions: this.options.extensions
}).apply(normalModuleFactory);
});
compiler.hooks.compilation.tap(
"DllReferencePlugin",
(compilation, params) => {
let manifest = this.options.manifest;
if (typeof manifest === "string") {
// If there was an error parsing the manifest file, add the
// error as a compilation error to make the compilation fail.
let e = params["dll reference parse error " + manifest];
if (e) {
compilation.errors.push(e);
}
}
}
);
}
}

View File

@ -720,6 +720,29 @@ Child
[0] ./index.js 24 bytes {0} [built]"
`;
exports[`StatsTestCases should print correct stats for dll-reference-plugin-issue-7624 1`] = `
"Hash: 29b62432962bce4c54c0
Time: Xms
Built at: Thu Jan 01 1970 00:00:00 GMT
Asset Size Chunks Chunk Names
bundle.js 3.6 KiB 0 [emitted] main
Entrypoint main = bundle.js
[0] ./entry.js 29 bytes {0} [built]"
`;
exports[`StatsTestCases should print correct stats for dll-reference-plugin-issue-7624-error 1`] = `
"Hash: db465df0da5d95ebf88f
Time: Xms
Built at: Thu Jan 01 1970 00:00:00 GMT
Asset Size Chunks Chunk Names
bundle.js 3.6 KiB 0 main
Entrypoint main = bundle.js
[0] ./entry.js 29 bytes {0} [built]
ERROR in Dll manifest Xdir/dll-reference-plugin-issue-7624-error/blank-manifest.json
Unexpected end of JSON input while parsing near ''"
`;
exports[`StatsTestCases should print correct stats for exclude-with-loader 1`] = `
"Hash: 52eadc5de721f000106b
Time: Xms

View File

@ -0,0 +1 @@
// Intentionally left blank.

View File

@ -0,0 +1,15 @@
var webpack = require("../../../");
module.exports = {
mode: "production",
entry: "./entry.js",
output: {
filename: "bundle.js"
},
plugins: [
new webpack.DllReferencePlugin({
manifest: __dirname + "/blank-manifest.json",
name: "blank-manifest"
})
]
};

View File

@ -0,0 +1 @@
// Intentionally left blank.

View File

@ -0,0 +1,11 @@
{
"name": "foo",
"content": {
"./foo.js": {
"id": 0,
"buildMeta": {
"providedExports": true
}
}
}
}

View File

@ -0,0 +1,15 @@
var webpack = require("../../../");
module.exports = {
mode: "production",
entry: "./entry.js",
output: {
filename: "bundle.js"
},
plugins: [
new webpack.DllReferencePlugin({
manifest: __dirname + "/non-blank-manifest.json",
name: "non-blank-manifest"
})
]
};