Skip to content

Commit 7e90f11

Browse files
authored
feat: assign config paths in build dependencies in cache config (#1900)
1 parent 71e89b4 commit 7e90f11

File tree

8 files changed

+132
-19
lines changed

8 files changed

+132
-19
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const cacheDefaults = (finalConfig, parsedArgs) => {
2+
// eslint-disable-next-line no-prototype-builtins
3+
const hasCache = finalConfig.hasOwnProperty('cache');
4+
let cacheConfig = {};
5+
if (hasCache && parsedArgs.config) {
6+
if (finalConfig.cache && finalConfig.cache.type === 'filesystem') {
7+
cacheConfig.buildDependencies = {
8+
config: parsedArgs.config,
9+
};
10+
}
11+
return { cache: cacheConfig };
12+
}
13+
return cacheConfig;
14+
};
15+
16+
const assignFlagDefaults = (compilerConfig, parsedArgs) => {
17+
if (Array.isArray(compilerConfig)) {
18+
return compilerConfig.map((config) => cacheDefaults(config, parsedArgs));
19+
}
20+
return cacheDefaults(compilerConfig, parsedArgs);
21+
};
22+
23+
module.exports = assignFlagDefaults;

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const { groups, core } = require('./utils/cli-flags');
55
const argParser = require('./utils/arg-parser');
66
const { outputStrategy } = require('./utils/merge-strategies');
77
const { toKebabCase } = require('./utils/helpers');
8+
const assignFlagDefaults = require('./utils/flag-defaults');
89

910
// CLI arg resolvers
1011
const handleConfigResolution = require('./groups/ConfigGroup');
@@ -44,19 +45,20 @@ class WebpackCLI extends GroupHelper {
4445
* @private\
4546
* @returns {void}
4647
*/
47-
_handleCoreFlags() {
48-
if (!this.groupMap.has('core')) {
49-
return;
48+
_handleCoreFlags(parsedArgs) {
49+
if (this.groupMap.has('core')) {
50+
const coreFlags = this.groupMap.get('core');
51+
52+
// convert all the flags from map to single object
53+
const coreConfig = coreFlags.reduce((allFlag, curFlag) => ({ ...allFlag, ...curFlag }), {});
54+
const coreCliHelper = require('webpack').cli;
55+
const coreCliArgs = coreCliHelper.getArguments();
56+
// Merge the core flag config with the compilerConfiguration
57+
coreCliHelper.processArguments(coreCliArgs, this.compilerConfiguration, coreConfig);
58+
// Assign some defaults to core flags
5059
}
51-
const coreFlags = this.groupMap.get('core');
52-
53-
// convert all the flags from map to single object
54-
const coreConfig = coreFlags.reduce((allFlag, curFlag) => ({ ...allFlag, ...curFlag }), {});
55-
56-
const coreCliHelper = require('webpack').cli;
57-
const coreCliArgs = coreCliHelper.getArguments();
58-
// Merge the core flag config with the compilerConfiguration
59-
coreCliHelper.processArguments(coreCliArgs, this.compilerConfiguration, coreConfig);
60+
const configWithDefaults = assignFlagDefaults(this.compilerConfiguration, parsedArgs);
61+
this._mergeOptionsToConfiguration(configWithDefaults);
6062
}
6163

6264
async _baseResolver(cb, parsedArgs, strategy) {
@@ -193,7 +195,7 @@ class WebpackCLI extends GroupHelper {
193195
.then(() => this._baseResolver(handleConfigResolution, parsedArgs))
194196
.then(() => this._baseResolver(resolveMode, parsedArgs))
195197
.then(() => this._baseResolver(resolveOutput, parsedArgs, outputStrategy))
196-
.then(() => this._handleCoreFlags())
198+
.then(() => this._handleCoreFlags(parsedArgs))
197199
.then(() => this._baseResolver(basicResolver, parsedArgs))
198200
.then(() => this._baseResolver(resolveAdvanced, parsedArgs))
199201
.then(() => this._baseResolver(resolveStats, parsedArgs))

test/cache/cache.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ const { run, isWebpack5 } = require('../utils/test-utils');
44

55
describe('cache related tests', () => {
66
it('should log warning in case of single compiler', () => {
7-
let { stderr, stdout } = run(__dirname, ['-c', 'webpack.config.js'], false);
7+
let { stderr, stdout } = run(__dirname, ['-c', './webpack.config.js'], false);
88
// run 2nd compilation
9-
({ stderr, stdout } = run(__dirname, ['-c', 'webpack.config.js'], false));
9+
({ stderr, stdout } = run(__dirname, ['-c', './webpack.config.js'], false));
1010

1111
if (isWebpack5) {
1212
expect(stderr).toContain('starting to restore cache content');

test/cache/webpack.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const path = require('path');
33
module.exports = {
44
cache: {
55
type: 'filesystem',
6+
name: 'cache-config-tests',
67
buildDependencies: {
78
config: [__filename],
89
},

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

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
'use strict';
22

3-
const { run } = require('../utils/test-utils');
4-
const { existsSync } = require('fs');
3+
const { run, isWindows } = require('../utils/test-utils');
4+
const { existsSync, writeFileSync, unlinkSync } = require('fs');
55
const { resolve } = require('path');
66

77
describe('cache related flags from core', () => {
88
it('should be successful with --cache ', () => {
99
const { stderr, stdout } = run(__dirname, ['--cache']);
10-
1110
expect(stderr).toBeFalsy();
1211
expect(stdout).toContain(`type: 'memory'`);
1312
});
@@ -69,4 +68,79 @@ describe('cache related flags from core', () => {
6968
expect(stderr).toBeFalsy();
7069
expect(stdout).toContain(`version: '1.1.3'`);
7170
});
71+
72+
it('should assign cache build dependencies correctly when cache type is filesystem', () => {
73+
// TODO: Fix on windows
74+
if (isWindows) return;
75+
const { stderr, stdout } = run(__dirname, ['--cache-type', 'filesystem', '-c', './webpack.config.js']);
76+
expect(stderr).toBeFalsy();
77+
expect(stdout).toContain('buildDependencies');
78+
expect(stdout).toContain("config: [ './webpack.config.js' ]");
79+
expect(stdout).not.toContain('[cached] 1 module');
80+
// Run again to check for cache
81+
const newRun = run(__dirname, ['--cache-type', 'filesystem', '-c', './webpack.config.js']);
82+
expect(newRun.stdout).toContain('[cached] 1 module');
83+
expect(newRun.stderr).toBeFalsy();
84+
expect(newRun.exitCode).toEqual(0);
85+
});
86+
87+
it('should assign cache build dependencies correctly when cache type is filesystem in config', () => {
88+
// TODO: Fix on windows
89+
if (isWindows) return;
90+
const { stderr, stdout } = run(__dirname, ['-c', './webpack.cache.config.js']);
91+
expect(stderr).toBeFalsy();
92+
expect(stdout).toContain('buildDependencies');
93+
expect(stdout).toContain("config: [ './webpack.cache.config.js' ]");
94+
expect(stdout).toContain("type: 'filesystem'");
95+
// Run again to check for cache
96+
const newRun = run(__dirname, ['-c', './webpack.cache.config.js']);
97+
expect(newRun.stdout).toContain('[cached] 1 module');
98+
expect(newRun.stderr).toBeFalsy();
99+
expect(newRun.exitCode).toEqual(0);
100+
});
101+
102+
it('should assign cache build dependencies with multiple configs', () => {
103+
// TODO: Fix on windows
104+
if (isWindows) return;
105+
const { stderr, stdout, exitCode } = run(__dirname, ['-c', './webpack.cache.config.js', '-c', './webpack.config.js']);
106+
expect(stderr).toBeFalsy();
107+
expect(stdout).toContain('buildDependencies');
108+
expect(stdout).toContain("config: [ './webpack.cache.config.js', './webpack.config.js' ]");
109+
expect(stdout).toContain("type: 'filesystem'");
110+
expect(exitCode).toEqual(0);
111+
});
112+
113+
it('should assign cache build dependencies with merged configs', () => {
114+
// TODO: Fix on windows
115+
if (isWindows) return;
116+
const { stderr, stdout, exitCode } = run(__dirname, ['-c', './webpack.cache.config.js', '-c', './webpack.config.js', '--merge']);
117+
expect(stderr).toBeFalsy();
118+
expect(stdout).toContain('buildDependencies');
119+
expect(stdout).toContain("config: [ './webpack.cache.config.js', './webpack.config.js' ]");
120+
expect(stdout).toContain("type: 'filesystem'");
121+
expect(exitCode).toEqual(0);
122+
});
123+
124+
it('should invalidate cache when config changes', () => {
125+
// TODO: Fix on windows
126+
if (isWindows) return;
127+
// Creating a temporary webpack config
128+
writeFileSync(resolve(__dirname, './webpack.test.config.js'), 'module.exports = {mode: "development"}');
129+
const { stderr, stdout } = run(__dirname, ['--cache-type', 'filesystem', '-c', './webpack.test.config.js']);
130+
expect(stderr).toBeFalsy();
131+
// modules should not be cached on first run
132+
expect(stdout).not.toContain('[cached] 1 module');
133+
134+
// Running again should use the cache
135+
const newRun = run(__dirname, ['--cache-type', 'filesystem', '-c', './webpack.test.config.js']);
136+
expect(newRun.stdout).toContain('[cached] 1 module');
137+
138+
// Change config to invalidate cache
139+
writeFileSync(resolve(__dirname, './webpack.test.config.js'), 'module.exports = {mode: "production"}');
140+
141+
const newRun2 = run(__dirname, ['--cache-type', 'filesystem', '-c', './webpack.test.config.js']);
142+
unlinkSync(resolve(__dirname, './webpack.test.config.js'));
143+
expect(newRun2).not.toContain('[cached] 1 module');
144+
expect(newRun2.exitCode).toEqual(0);
145+
});
72146
});

test/core-flags/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("Mizuhara Chizuru")
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const WebpackCLITestPlugin = require('../utils/webpack-cli-test-plugin');
2+
3+
module.exports = {
4+
entry: './src/main.js',
5+
mode: 'development',
6+
cache: {
7+
type: 'filesystem',
8+
name: 'config-cache',
9+
},
10+
name: 'compiler-cache',
11+
plugins: [new WebpackCLITestPlugin(['cache'])],
12+
};

test/core-flags/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ module.exports = {
44
entry: './src/main.js',
55
mode: 'development',
66
name: 'compiler',
7-
plugins: [new WebpackCLITestPlugin(['module', 'entry', 'resolve', 'resolveLoader'])],
7+
plugins: [new WebpackCLITestPlugin(['module', 'entry', 'resolve', 'resolveLoader', 'cache'])],
88
};

0 commit comments

Comments
 (0)