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 13, 2024
1 parent 513126d commit 4eec173
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 61 deletions.
4 changes: 2 additions & 2 deletions lib/MultiCompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ module.exports = class MultiCompiler {
new WebpackError(
`${
compiler.name
? `Compiler with name "${compiler.name}" doesn't use cache name. `
? `Compiler with name "${compiler.name}" doesn't use unique cache name. `
: ""
}Please set "cache.name" option.`
}Please set unique "cache.name" option.`
)
);
}
Expand Down
6 changes: 2 additions & 4 deletions lib/WebpackOptionsDefaulter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
"use strict";

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

/** @typedef {import("./config/normalization").WebpackOptions} WebpackOptions */
/** @typedef {import("./config/normalization").WebpackOptionsNormalized} WebpackOptionsNormalized */
Expand All @@ -19,7 +17,7 @@ class WebpackOptionsDefaulter {
* @returns {WebpackOptionsNormalized} normalized webpack options
*/
process(options) {
const normalizedOptions = getNormalizedWebpackCompilerOptions(options);
const normalizedOptions = getNormalizedWebpackOptions(options);
applyWebpackOptionsDefaults(normalizedOptions);
return normalizedOptions;
}
Expand Down
63 changes: 55 additions & 8 deletions lib/config/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ const {
getDefaultTarget
} = require("./target");

/** @typedef {import("../../declarations/WebpackOptions").CacheOptionsNormalized} CacheOptions */
/** @typedef {import("../../declarations/WebpackOptions").CacheOptions} CacheOptions */
/** @typedef {import("../../declarations/WebpackOptions").CacheOptionsNormalized} CacheOptionsNormalized */
/** @typedef {import("../../declarations/WebpackOptions").Context} Context */
/** @typedef {import("../../declarations/WebpackOptions").CssGeneratorOptions} CssGeneratorOptions */
/** @typedef {import("../../declarations/WebpackOptions").CssParserOptions} CssParserOptions */
Expand Down Expand Up @@ -60,7 +61,8 @@ const {
/** @typedef {import("../../declarations/WebpackOptions").RuleSetRules} RuleSetRules */
/** @typedef {import("../../declarations/WebpackOptions").SnapshotOptions} SnapshotOptions */
/** @typedef {import("../../declarations/WebpackOptions").Target} Target */
/** @typedef {import("../../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
/** @typedef {import("../../declarations/WebpackOptions").WebpackOptions} WebpackOptions */
/** @typedef {import("../../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptionsNormalized */
/** @typedef {import("../Compiler")} Compiler */
/** @typedef {import("../Module")} Module */
/** @typedef {import("./target").TargetProperties} TargetProperties */
Expand Down Expand Up @@ -138,7 +140,7 @@ const A = (obj, prop, factory) => {
};

/**
* @param {WebpackOptions} options options to be modified
* @param {WebpackOptionsNormalized} options options to be modified
* @returns {void}
*/
const applyWebpackOptionsBaseDefaults = options => {
Expand All @@ -147,7 +149,49 @@ const applyWebpackOptionsBaseDefaults = options => {
};

/**
* @param {WebpackOptions} options options to be modified
* @returns {(cache: CacheOptions, i: number) => CacheOptions} cache options
*/
const applyDefaultCacheNamesForMultiCompiler = () => {
/** @type {Set<string>} */
const cacheNames = new Set();
return (cache, i) => {
if (typeof cache === "boolean") return cache;
if (cache.type !== "filesystem") return { ...cache };
const name = cache.name ? cache.name : DEFAULT_CACHE_NAME;
if (i === 0) {
cacheNames.add(name);
return { ...cache };
}
if (cacheNames.has(name)) {
return { ...cache, name: `${name}__compiler${i + 1}__` };
}
cacheNames.add(cache.name);
return { ...cache };
};
};

/**
* @param {ReadonlyArray<WebpackOptions>} options input config
* @returns {ReadonlyArray<WebpackOptions>} normalized options
*/
const applyWebpackMultiCompilerOptions = options => {
/** @type {Array<WebpackOptions>} */
const resultedOptions = [];
const applyCacheOptions = applyDefaultCacheNamesForMultiCompiler();

for (let i = 0; i < options.length; i++) {
const config = options[i];
const cache = config.cache;
const newConfig = { ...config };
resultedOptions.push(newConfig);
newConfig.cache = applyCacheOptions(cache, i);
}

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

/**
* @param {WebpackOptionsNormalized} options options to be modified
* @returns {void}
*/
const applyWebpackOptionsDefaults = options => {
Expand Down Expand Up @@ -252,7 +296,9 @@ const applyWebpackOptionsDefaults = options => {
});

applyLoaderDefaults(
/** @type {NonNullable<WebpackOptions["loader"]>} */ (options.loader),
/** @type {NonNullable<WebpackOptionsNormalized["loader"]>} */ (
options.loader
),
{ targetProperties, environment: options.output.environment }
);

Expand All @@ -269,7 +315,7 @@ const applyWebpackOptionsDefaults = options => {

applyNodeDefaults(options.node, {
futureDefaults:
/** @type {NonNullable<WebpackOptions["experiments"]["futureDefaults"]>} */
/** @type {NonNullable<WebpackOptionsNormalized["experiments"]["futureDefaults"]>} */
(options.experiments.futureDefaults),
outputModule: options.output.module,
targetProperties
Expand All @@ -283,7 +329,7 @@ const applyWebpackOptionsDefaults = options => {
: false
);
applyPerformanceDefaults(
/** @type {NonNullable<WebpackOptions["performance"]>} */
/** @type {NonNullable<WebpackOptionsNormalized["performance"]>} */
(options.performance),
{
production
Expand Down Expand Up @@ -355,7 +401,7 @@ const applyExperimentsDefaults = (
};

/**
* @param {CacheOptions} cache options
* @param {CacheOptionsNormalized} cache options
* @param {Object} options options
* @param {string} options.name name
* @param {Mode} options.mode mode
Expand Down Expand Up @@ -1587,3 +1633,4 @@ const applyInfrastructureLoggingDefaults = infrastructureLogging => {
exports.defaultCacheName = DEFAULT_CACHE_NAME;
exports.applyWebpackOptionsBaseDefaults = applyWebpackOptionsBaseDefaults;
exports.applyWebpackOptionsDefaults = applyWebpackOptionsDefaults;
exports.applyWebpackMultiCompilerOptions = applyWebpackMultiCompilerOptions;
36 changes: 2 additions & 34 deletions lib/config/normalization.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@
"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 @@ -123,35 +120,11 @@ 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 getNormalizedWebpackCompilerOptions = config => {
const getNormalizedWebpackOptions = config => {
return {
amd: config.amd,
bail: config.bail,
Expand Down Expand Up @@ -592,9 +565,4 @@ const getNormalizedOptimizationRuntimeChunk = runtimeChunk => {
};
};

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

Expand All @@ -45,9 +43,8 @@ const getValidateSchema = memoize(() => require("./validateSchema"));
* @returns {MultiCompiler} a multi-compiler
*/
const createMultiCompiler = (childOptions, options) => {
const normalizedOptions =
getNormalizedWebpackMultiCompilerOptions(childOptions);
const compilers = normalizedOptions.map(options => createCompiler(options));
const appliedOptions = applyWebpackMultiCompilerOptions(childOptions);
const compilers = appliedOptions.map(options => createCompiler(options));
const compiler = new MultiCompiler(compilers, options);
for (const childCompiler of compilers) {
if (childCompiler.options.dependencies) {
Expand All @@ -65,7 +62,7 @@ const createMultiCompiler = (childOptions, options) => {
* @returns {Compiler} a compiler
*/
const createCompiler = rawOptions => {
const options = getNormalizedWebpackCompilerOptions(rawOptions);
const options = getNormalizedWebpackOptions(rawOptions);
applyWebpackOptionsBaseDefaults(options);
const compiler = new Compiler(
/** @type {string} */ (options.context),
Expand Down
15 changes: 12 additions & 3 deletions test/ConfigTestCases.template.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,21 @@ const describeCases = config => {
(options.experiments && options.experiments.outputModule
? ".mjs"
: ".js");
if (config.cache) {
if (
(config.cache && config.cache !== true) ||
(options.cache && options.cache !== true)
) {
options.cache = {
cacheDirectory,
name: `config-${idx}`,
...config.cache
name:
options.cache && options.cache !== true
? options.cache.name
: `config-${idx}`,
...config.cache,
...options.cache
};
}
if (config.cache) {
options.infrastructureLogging = {
debug: true,
console: createLogger(infraStructureLog)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = [
/Please set "cache\.name" option/,
/Compiler with name "3rd compiler" doesn't use cache name/
/Please set unique "cache\.name" option/,
/Compiler with name "3rd compiler" doesn't use unique cache name/
];

0 comments on commit 4eec173

Please sign in to comment.