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 3a17599
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 16 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;
37 changes: 35 additions & 2 deletions lib/config/normalization.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
"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").MemoryCacheOptions} MemoryCacheOptions */
/** @typedef {import("../../declarations/WebpackOptions").FileCacheOptions} FileCacheOptions */
/** @typedef {import("../../declarations/WebpackOptions").LibraryName} LibraryName */
/** @typedef {import("../../declarations/WebpackOptions").LibraryOptions} LibraryOptions */
/** @typedef {import("../../declarations/WebpackOptions").ModuleOptionsNormalized} ModuleOptionsNormalized */
Expand All @@ -22,6 +25,7 @@ const util = require("util");
/** @typedef {import("../../declarations/WebpackOptions").WebpackOptions} WebpackOptions */
/** @typedef {import("../../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptionsNormalized */
/** @typedef {import("../Entrypoint")} Entrypoint */
/** @typedef {import("../MultiCompiler").MultiCompilerOptions} MultiCompilerOptions */

const handledDeprecatedNoEmitOnErrors = util.deprecate(
/**
Expand Down Expand Up @@ -120,11 +124,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 +593,9 @@ const getNormalizedOptimizationRuntimeChunk = runtimeChunk => {
};
};

exports.getNormalizedWebpackOptions = getNormalizedWebpackOptions;
exports.getNormalizedWebpackMultiCompilerOptions =
getNormalizedWebpackMultiCompilerOptions;
exports.getNormalizedWebpackCompilerOptions =
getNormalizedWebpackCompilerOptions;
// for backward compatability
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

0 comments on commit 3a17599

Please sign in to comment.