Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const {
} = require("./lib/html-tags");
const prettyError = require("./lib/errors.js");
const chunkSorter = require("./lib/chunksorter.js");
const validateChunkNames = require("./lib/validators");
const { AsyncSeriesWaterfallHook } = require("tapable");

/** @typedef {import("./typings").HtmlTagObject} HtmlTagObject */
Expand Down Expand Up @@ -1195,6 +1196,34 @@ class HtmlWebpackPlugin {
return result;
}

/**
* @param {(string | null | undefined)[]} allChunkNames - all compilation chunk names
* @returns {Error[]} validation errors
*/
validateOptions(allChunkNames) {
const validationErrors = [];
if (this.options.chunks !== "all") {
const chunksErr = validateChunkNames(
allChunkNames,
this.options.chunks,
"chunks",
);
if (chunksErr) {
validationErrors.push(chunksErr);
}
}
const excludeChunksErr = validateChunkNames(
allChunkNames,
this.options.excludeChunks,
"excludeChunks",
);
if (excludeChunksErr) {
validationErrors.push(excludeChunksErr);
}

return validationErrors;
}

/**
* Replace [contenthash] in filename
*
Expand Down Expand Up @@ -1261,6 +1290,15 @@ class HtmlWebpackPlugin {
) {
// Get all entry point names for this html file
const entryNames = Array.from(compilation.entrypoints.keys());

// Get all chunk names
const allChunkNames = Array.from(compilation.chunks).map((c) => c.name);

const validationErrors = this.validateOptions(allChunkNames);
if (validationErrors.length) {
compilation.errors.push(...validationErrors);
}

const filteredEntryNames = this.filterEntryChunks(
entryNames,
this.options.chunks,
Expand Down
22 changes: 22 additions & 0 deletions lib/validators.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Validates that all expected chunks are part of the compilation result
*
* @param {(string | null | undefined)[]} allChunkNames - all compilation chunk names
* @param {string[]} expectedChunkNames - expected chunk names
* @param {string} label
* @returns {Error | undefined} validation errors
*/
module.exports = function (allChunkNames, expectedChunkNames, label) {
if (Array.isArray(expectedChunkNames)) {
const missingChunks = expectedChunkNames.filter(
(chunkName) => !allChunkNames.includes(chunkName),
);

if (missingChunks.length) {
const chunksStr = missingChunks.join(", ");
return new Error(
`HtmlWebpackPlugin: The following chunks provided in the '${label}' option were not found: ${chunksStr}`,
);
}
}
};
54 changes: 54 additions & 0 deletions spec/basic.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3048,6 +3048,60 @@ describe("HtmlWebpackPlugin", () => {
);
});

it("throws an error if a specified chunk in the chunks option is not found", (done) => {
testHtmlPlugin(
{
mode: "production",
entry: {
app: path.join(__dirname, "fixtures/index.js"),
},
output: {
path: OUTPUT_DIR,
filename: "[name]_bundle.js",
},
optimization: {
emitOnErrors: true,
},
plugins: [
new HtmlWebpackPlugin({
chunks: ["app", "non_existent_chunk"],
}),
],
},
[],
null,
done,
true,
);
});

it("throws an error if a specified chunk in the excludeChunks option is not found", (done) => {
testHtmlPlugin(
{
mode: "production",
entry: {
app: path.join(__dirname, "fixtures/index.js"),
},
output: {
path: OUTPUT_DIR,
filename: "[name]_bundle.js",
},
optimization: {
emitOnErrors: true,
},
plugins: [
new HtmlWebpackPlugin({
excludeChunks: ["non_existent_chunk"],
}),
],
},
[],
null,
done,
true,
);
});

it("should add the webpack compilation object as a property of the templateParam object", (done) => {
testHtmlPlugin(
{
Expand Down