Skip to content

Commit

Permalink
add default cache names in multicompiler mode
Browse files Browse the repository at this point in the history
  • Loading branch information
vankop committed Apr 12, 2024
1 parent d00aa38 commit e39095e
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 18 deletions.
20 changes: 12 additions & 8 deletions lib/MultiCompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,26 +115,30 @@ module.exports = class MultiCompiler {

_validateCompilersOptions() {
if (this.compilers.length < 2) return;
const cacheNames = new Set();
/**
* @param {Compiler} compiler compiler
* @param {WebpackError} warning warning
*/
const addWarning = (compiler, warning) => {
compiler.hooks.thisCompilation.tap("MultiCompiler", compilation => {
compilation.warnings.push(warning);
});
};
for (const compiler of this.compilers) {
if (compiler.options.cache && "name" in compiler.options.cache) {
const cacheName = compiler.options.cache && compiler.options.cache.name;
if (cacheNames.has(cacheName)) {
const name = compiler.options.cache.name;
const warn = /__compiler(\d+)__$/.test(name);
if (warn) {
addWarning(
compiler,
new WebpackError(
`Compiler cache with name ${JSON.stringify(
cacheName
)} is already defined. Please set unique "cache.name" option.`
`${
compiler.name
? `Compiler with name "${compiler.name}" doesn't use cache name. `
: ""
}Please set "cache.name" option.`
)
);
} else {
cacheNames.add(cacheName);
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions lib/WebpackOptionsDefaulter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"use strict";

const { applyWebpackOptionsDefaults } = require("./config/defaults");
const { getNormalizedWebpackOptions } = require("./config/normalization");
const {
getNormalizedWebpackCompilerOptions
} = require("./config/normalization");

/** @typedef {import("./config/normalization").WebpackOptions} WebpackOptions */
/** @typedef {import("./config/normalization").WebpackOptionsNormalized} WebpackOptionsNormalized */
Expand All @@ -17,7 +19,7 @@ class WebpackOptionsDefaulter {
* @returns {WebpackOptionsNormalized} normalized webpack options
*/
process(options) {
const normalizedOptions = getNormalizedWebpackOptions(options);
const normalizedOptions = getNormalizedWebpackCompilerOptions(options);
applyWebpackOptionsDefaults(normalizedOptions);
return normalizedOptions;
}
Expand Down
4 changes: 3 additions & 1 deletion lib/config/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const {
/** @typedef {import("./target").TargetProperties} TargetProperties */

const NODE_MODULES_REGEXP = /[\\/]node_modules[\\/]/i;
const DEFAULT_CACHE_NAME = "default";

/**
* Sets a constant default value when undefined
Expand Down Expand Up @@ -201,7 +202,7 @@ const applyWebpackOptionsDefaults = options => {
development ? { type: /** @type {"memory"} */ ("memory") } : false
);
applyCacheDefaults(options.cache, {
name: name || "default",
name: name || DEFAULT_CACHE_NAME,
mode: mode || "production",
development,
cacheUnaffected: options.experiments.cacheUnaffected
Expand Down Expand Up @@ -1583,5 +1584,6 @@ const applyInfrastructureLoggingDefaults = infrastructureLogging => {
D(infrastructureLogging, "appendOnly", !tty);
};

exports.defaultCacheName = DEFAULT_CACHE_NAME;
exports.applyWebpackOptionsBaseDefaults = applyWebpackOptionsBaseDefaults;
exports.applyWebpackOptionsDefaults = applyWebpackOptionsDefaults;
36 changes: 34 additions & 2 deletions lib/config/normalization.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
"use strict";

const util = require("util");
const { defaultCacheName } = require("./defaults");

/** @typedef {import("../../declarations/WebpackOptions").CacheOptionsNormalized} CacheOptions */
/** @typedef {import("../../declarations/WebpackOptions").EntryDescriptionNormalized} EntryDescriptionNormalized */
/** @typedef {import("../../declarations/WebpackOptions").EntryStatic} EntryStatic */
/** @typedef {import("../../declarations/WebpackOptions").EntryStaticNormalized} EntryStaticNormalized */
/** @typedef {import("../../declarations/WebpackOptions").Externals} Externals */
/** @typedef {import("../../declarations/WebpackOptions").FileCacheOptions} FileCacheOptions */
/** @typedef {import("../../declarations/WebpackOptions").LibraryName} LibraryName */
/** @typedef {import("../../declarations/WebpackOptions").LibraryOptions} LibraryOptions */
/** @typedef {import("../../declarations/WebpackOptions").MemoryCacheOptions} MemoryCacheOptions */
/** @typedef {import("../../declarations/WebpackOptions").ModuleOptionsNormalized} ModuleOptionsNormalized */
/** @typedef {import("../../declarations/WebpackOptions").OptimizationRuntimeChunk} OptimizationRuntimeChunk */
/** @typedef {import("../../declarations/WebpackOptions").OptimizationRuntimeChunkNormalized} OptimizationRuntimeChunkNormalized */
Expand Down Expand Up @@ -120,11 +123,35 @@ const keyedNestedConfig = (value, fn, customKeys) => {
return result;
};

/**
* @param {ReadonlyArray<WebpackOptions>} options input config
* @returns {ReadonlyArray<WebpackOptions>} normalized options
*/
const getNormalizedWebpackMultiCompilerOptions = options => {
/** @type {Array<WebpackOptions>} */
const resultedOptions = [];

for (let i = 0; i < options.length; i++) {
const config = options[i];
resultedOptions.push({
...config,
cache: optionalNestedConfig(config.cache, cache => {
if (typeof cache === "boolean") return cache;
if (i === 0 || cache.type !== "filesystem" || "name" in cache)
return { ...cache };
return { ...cache, name: `${defaultCacheName}__compiler${i + 1}__` };
})
});
}

return /** @type {ReadonlyArray<WebpackOptions>} */ (resultedOptions);
};

/**
* @param {WebpackOptions} config input config
* @returns {WebpackOptionsNormalized} normalized options
*/
const getNormalizedWebpackOptions = config => {
const getNormalizedWebpackCompilerOptions = config => {
return {
amd: config.amd,
bail: config.bail,
Expand Down Expand Up @@ -565,4 +592,9 @@ const getNormalizedOptimizationRuntimeChunk = runtimeChunk => {
};
};

exports.getNormalizedWebpackOptions = getNormalizedWebpackOptions;
exports.getNormalizedWebpackMultiCompilerOptions =
getNormalizedWebpackMultiCompilerOptions;
exports.getNormalizedWebpackCompilerOptions =
getNormalizedWebpackCompilerOptions;
// for backward compatibility
exports.getNormalizedWebpackOptions = getNormalizedWebpackCompilerOptions;
11 changes: 8 additions & 3 deletions lib/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ const {
applyWebpackOptionsDefaults,
applyWebpackOptionsBaseDefaults
} = require("./config/defaults");
const { getNormalizedWebpackOptions } = require("./config/normalization");
const {
getNormalizedWebpackCompilerOptions,
getNormalizedWebpackMultiCompilerOptions
} = require("./config/normalization");
const NodeEnvironmentPlugin = require("./node/NodeEnvironmentPlugin");
const memoize = require("./util/memoize");

Expand All @@ -42,7 +45,9 @@ const getValidateSchema = memoize(() => require("./validateSchema"));
* @returns {MultiCompiler} a multi-compiler
*/
const createMultiCompiler = (childOptions, options) => {
const compilers = childOptions.map(options => createCompiler(options));
const normalizedOptions =
getNormalizedWebpackMultiCompilerOptions(childOptions);
const compilers = normalizedOptions.map(options => createCompiler(options));
const compiler = new MultiCompiler(compilers, options);
for (const childCompiler of compilers) {
if (childCompiler.options.dependencies) {
Expand All @@ -60,7 +65,7 @@ const createMultiCompiler = (childOptions, options) => {
* @returns {Compiler} a compiler
*/
const createCompiler = rawOptions => {
const options = getNormalizedWebpackOptions(rawOptions);
const options = getNormalizedWebpackCompilerOptions(rawOptions);
applyWebpackOptionsBaseDefaults(options);
const compiler = new Compiler(
/** @type {string} */ (options.context),
Expand Down
22 changes: 20 additions & 2 deletions test/__snapshots__/StatsTestCases.basictest.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1819,10 +1819,28 @@ webpack x.x.x compiled successfully in X ms
1 asset
1 module
webpack x.x.x compiled successfully in X ms"
`;

WARNING in Compiler cache with name \\"name1\\" is already defined. Please set unique \\"cache.name\\" option.
exports[`StatsTestCases should print correct stats for multicompiler-mode-cache-warning 1`] = `
"1 asset
1 module
webpack x.x.x compiled successfully in X ms
webpack x.x.x compiled with 1 warning in X ms"
1 asset
1 module
WARNING in Please set \\"cache.name\\" option.
webpack x.x.x compiled with 1 warning in X ms
3rd compiler:
1 asset
1 module
WARNING in Compiler with name \\"3rd compiler\\" doesn't use cache name. Please set \\"cache.name\\" option.
3rd compiler (webpack x.x.x) compiled with 1 warning in X ms"
`;

exports[`StatsTestCases should print correct stats for named-chunk-groups 1`] = `
Expand Down
Empty file.
30 changes: 30 additions & 0 deletions test/statsCases/multicompiler-mode-cache-warning/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"use strict";

/** @type {import("../../../").Configuration} */
module.exports = [
{
mode: "production",
entry: "./index",
cache: {
type: "filesystem"
},
stats: { preset: "minimal" }
},
{
mode: "production",
entry: "./index",
cache: {
type: "filesystem"
},
stats: { preset: "minimal" }
},
{
name: "3rd compiler",
mode: "production",
entry: "./index",
cache: {
type: "filesystem"
},
stats: { preset: "minimal" }
}
];

0 comments on commit e39095e

Please sign in to comment.