Skip to content

Commit d2e3c74

Browse files
authored
fix: assign cache value for default configs (#2013)
1 parent 19fa662 commit d2e3c74

File tree

4 files changed

+27
-10
lines changed

4 files changed

+27
-10
lines changed

packages/webpack-cli/lib/groups/resolveConfig.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,21 +126,21 @@ const resolveConfigFiles = async (args) => {
126126
const defaultConfig = configFiles.find((p) => p.path.includes(mode) || p.path.includes(modeAlias[mode]));
127127

128128
if (defaultConfig) {
129-
opts = await finalize(defaultConfig, args);
129+
opts = await finalize(defaultConfig, args, true);
130130
return;
131131
}
132132

133133
const foundConfig = configFiles.pop();
134134

135-
opts = await finalize(foundConfig, args);
135+
opts = await finalize(foundConfig, args, true);
136136

137137
return;
138138
}
139139
};
140140

141141
// Given config data, determines the type of config and
142142
// returns final config
143-
const finalize = async (moduleObj, args) => {
143+
const finalize = async (moduleObj, args, isDefaultConfig = false) => {
144144
const { env, configName } = args;
145145
const newOptionsObject = {
146146
outputOptions: {},
@@ -151,6 +151,10 @@ const finalize = async (moduleObj, args) => {
151151
return newOptionsObject;
152152
}
153153

154+
if (isDefaultConfig) {
155+
newOptionsObject.outputOptions.defaultConfig = moduleObj.path;
156+
}
157+
154158
const config = moduleObj.config;
155159

156160
const isMultiCompilerMode = Array.isArray(config);
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
1-
const cacheDefaults = (finalConfig, parsedArgs) => {
1+
const cacheDefaults = (finalConfig, parsedArgs, outputOptions) => {
22
// eslint-disable-next-line no-prototype-builtins
33
const hasCache = finalConfig.hasOwnProperty('cache');
44
let cacheConfig = {};
5-
if (hasCache && parsedArgs.config) {
5+
if (hasCache && (parsedArgs.config || (outputOptions && outputOptions.defaultConfig))) {
66
if (finalConfig.cache && finalConfig.cache.type === 'filesystem') {
77
cacheConfig.buildDependencies = {
8-
config: parsedArgs.config,
8+
config: parsedArgs.config || [outputOptions.defaultConfig],
99
};
10+
} else {
11+
cacheConfig = finalConfig.cache;
1012
}
1113
return { cache: cacheConfig };
1214
}
1315
return cacheConfig;
1416
};
1517

16-
const assignFlagDefaults = (compilerConfig, parsedArgs) => {
18+
const assignFlagDefaults = (compilerConfig, parsedArgs, outputOptions) => {
1719
if (Array.isArray(compilerConfig)) {
18-
return compilerConfig.map((config) => cacheDefaults(config, parsedArgs));
20+
return compilerConfig.map((config) => cacheDefaults(config, parsedArgs, outputOptions));
1921
}
20-
return cacheDefaults(compilerConfig, parsedArgs);
22+
return cacheDefaults(compilerConfig, parsedArgs, outputOptions);
2123
};
2224

2325
module.exports = assignFlagDefaults;

packages/webpack-cli/lib/webpack-cli.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class WebpackCLI {
4545
// Merge the core flag config with the compilerConfiguration
4646
coreCliHelper.processArguments(coreCliArgs, this.compilerConfiguration, coreConfig);
4747
// Assign some defaults to core flags
48-
const configWithDefaults = assignFlagDefaults(this.compilerConfiguration, parsedArgs);
48+
const configWithDefaults = assignFlagDefaults(this.compilerConfiguration, parsedArgs, this.outputConfiguration);
4949
this._mergeOptionsToConfiguration(configWithDefaults);
5050
}
5151

test/core-flags/cache-flags.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,17 @@ describe('cache related flags from core', () => {
136136
expect(exitCode).toEqual(0);
137137
});
138138

139+
it('should assign cache build dependencies with default config', () => {
140+
// TODO: Fix on windows
141+
if (isWindows) return;
142+
const { stderr, stdout, exitCode } = run(__dirname, ['--cache-type', 'filesystem']);
143+
expect(stderr).toBeFalsy();
144+
expect(stdout).toContain('buildDependencies');
145+
expect(stdout).toContain(`'${path.join(__dirname, './webpack.config.js')}'`);
146+
expect(stdout).toContain("type: 'filesystem'");
147+
expect(exitCode).toEqual(0);
148+
});
149+
139150
it('should assign cache build dependencies with merged configs', () => {
140151
// TODO: Fix on windows
141152
if (isWindows) return;

0 commit comments

Comments
 (0)