Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: assign cache value for default configs #2013

Merged
merged 6 commits into from Nov 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 7 additions & 3 deletions packages/webpack-cli/lib/groups/resolveConfig.js
Expand Up @@ -126,21 +126,21 @@ const resolveConfigFiles = async (args) => {
const defaultConfig = configFiles.find((p) => p.path.includes(mode) || p.path.includes(modeAlias[mode]));

if (defaultConfig) {
opts = await finalize(defaultConfig, args);
opts = await finalize(defaultConfig, args, true);
return;
}

const foundConfig = configFiles.pop();

opts = await finalize(foundConfig, args);
opts = await finalize(foundConfig, args, true);

return;
}
};

// Given config data, determines the type of config and
// returns final config
const finalize = async (moduleObj, args) => {
const finalize = async (moduleObj, args, isDefaultConfig = false) => {
const { env, configName } = args;
const newOptionsObject = {
outputOptions: {},
Expand All @@ -151,6 +151,10 @@ const finalize = async (moduleObj, args) => {
return newOptionsObject;
}

if (isDefaultConfig) {
newOptionsObject.outputOptions.defaultConfig = moduleObj.path;
}

const config = moduleObj.config;

const isMultiCompilerMode = Array.isArray(config);
Expand Down
14 changes: 8 additions & 6 deletions packages/webpack-cli/lib/utils/flag-defaults.js
@@ -1,23 +1,25 @@
const cacheDefaults = (finalConfig, parsedArgs) => {
const cacheDefaults = (finalConfig, parsedArgs, outputOptions) => {
// eslint-disable-next-line no-prototype-builtins
const hasCache = finalConfig.hasOwnProperty('cache');
let cacheConfig = {};
if (hasCache && parsedArgs.config) {
if (hasCache && (parsedArgs.config || (outputOptions && outputOptions.defaultConfig))) {
if (finalConfig.cache && finalConfig.cache.type === 'filesystem') {
cacheConfig.buildDependencies = {
config: parsedArgs.config,
config: parsedArgs.config || [outputOptions.defaultConfig],
};
} else {
cacheConfig = finalConfig.cache;
}
return { cache: cacheConfig };
}
return cacheConfig;
};

const assignFlagDefaults = (compilerConfig, parsedArgs) => {
const assignFlagDefaults = (compilerConfig, parsedArgs, outputOptions) => {
if (Array.isArray(compilerConfig)) {
return compilerConfig.map((config) => cacheDefaults(config, parsedArgs));
return compilerConfig.map((config) => cacheDefaults(config, parsedArgs, outputOptions));
}
return cacheDefaults(compilerConfig, parsedArgs);
return cacheDefaults(compilerConfig, parsedArgs, outputOptions);
};

module.exports = assignFlagDefaults;
2 changes: 1 addition & 1 deletion packages/webpack-cli/lib/webpack-cli.js
Expand Up @@ -45,7 +45,7 @@ class WebpackCLI {
// Merge the core flag config with the compilerConfiguration
coreCliHelper.processArguments(coreCliArgs, this.compilerConfiguration, coreConfig);
// Assign some defaults to core flags
const configWithDefaults = assignFlagDefaults(this.compilerConfiguration, parsedArgs);
const configWithDefaults = assignFlagDefaults(this.compilerConfiguration, parsedArgs, this.outputConfiguration);
this._mergeOptionsToConfiguration(configWithDefaults);
}

Expand Down
11 changes: 11 additions & 0 deletions test/core-flags/cache-flags.test.js
Expand Up @@ -136,6 +136,17 @@ describe('cache related flags from core', () => {
expect(exitCode).toEqual(0);
});

it('should assign cache build dependencies with default config', () => {
// TODO: Fix on windows
if (isWindows) return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid this here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

already skipped for windows, some problems in all tests 😞

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, let's focus on it late

const { stderr, stdout, exitCode } = run(__dirname, ['--cache-type', 'filesystem']);
expect(stderr).toBeFalsy();
expect(stdout).toContain('buildDependencies');
expect(stdout).toContain(`'${path.join(__dirname, './webpack.config.js')}'`);
expect(stdout).toContain("type: 'filesystem'");
expect(exitCode).toEqual(0);
});

it('should assign cache build dependencies with merged configs', () => {
// TODO: Fix on windows
if (isWindows) return;
Expand Down