Skip to content

Commit

Permalink
Merge pull request #7628 from reduckted/dllreferenceplugin-graceful-j…
Browse files Browse the repository at this point in the history
…son-parse-failure

Catch errors while parsing DLL manifest
  • Loading branch information
sokra committed Jul 23, 2018
2 parents 699fe21 + 5b6f99b commit b8c47a7
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 3 deletions.
54 changes: 51 additions & 3 deletions lib/DllReferencePlugin.js
Expand Up @@ -10,6 +10,8 @@ const DelegatedModuleFactoryPlugin = require("./DelegatedModuleFactoryPlugin");
const ExternalModuleFactoryPlugin = require("./ExternalModuleFactoryPlugin");
const DelegatedExportsDependency = require("./dependencies/DelegatedExportsDependency");
const NullFactory = require("./NullFactory");
const makePathsRelative = require("./util/identifier").makePathsRelative;
const WebpackError = require("./WebpackError");

const validateOptions = require("schema-utils");
const schema = require("../schemas/plugins/DllReferencePlugin.json");
Expand Down Expand Up @@ -43,9 +45,23 @@ 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 = makePathsRelative(
compiler.options.context,
manifest
);
params[
"dll reference parse error " + manifest
] = new DllManifestError(manifestPath, e.message);
}
return callback();
});
} else {
Expand All @@ -57,6 +73,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;
Expand All @@ -78,6 +100,32 @@ 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);
}
}
}
);
}
}

class DllManifestError extends WebpackError {
constructor(filename, message) {
super();

this.name = "DllManifestError";
this.message = `Dll manifest ${filename}\n${message}`;

Error.captureStackTrace(this, this.constructor);
}
}

Expand Down
23 changes: 23 additions & 0 deletions test/__snapshots__/StatsTestCases.test.js.snap
Expand Up @@ -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: 701dcf62b26d0347b899
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 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
Expand Down
Empty file.
@@ -0,0 +1 @@
// Intentionally left blank.
@@ -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"
})
]
};
1 change: 1 addition & 0 deletions test/statsCases/dll-reference-plugin-issue-7624/entry.js
@@ -0,0 +1 @@
// Intentionally left blank.
@@ -0,0 +1,11 @@
{
"name": "foo",
"content": {
"./foo.js": {
"id": 0,
"buildMeta": {
"providedExports": true
}
}
}
}
15 changes: 15 additions & 0 deletions test/statsCases/dll-reference-plugin-issue-7624/webpack.config.js
@@ -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"
})
]
};

0 comments on commit b8c47a7

Please sign in to comment.