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

feat: assign config paths in build dependencies in cache config #1900

Merged
merged 10 commits into from Oct 10, 2020
19 changes: 19 additions & 0 deletions packages/webpack-cli/lib/utils/flag-defaults.js
@@ -0,0 +1,19 @@
const assignFlagDefaults = (compilerConfig, parsedArgs) => {
const finalConfig = { ...compilerConfig };
// eslint-disable-next-line no-prototype-builtins
const hasCache = finalConfig.hasOwnProperty('cache');
if (hasCache && parsedArgs.config) {
if (finalConfig.cache && finalConfig.cache.type === 'filesystem') {
finalConfig.cache = {
...finalConfig.cache,
buildDependencies: {
config: parsedArgs.config,
},
};
}
return { cache: finalConfig.cache };
}
return {};
};

module.exports = assignFlagDefaults;
28 changes: 15 additions & 13 deletions packages/webpack-cli/lib/webpack-cli.js
Expand Up @@ -5,6 +5,7 @@ const { groups, core } = require('./utils/cli-flags');
const argParser = require('./utils/arg-parser');
const { outputStrategy } = require('./utils/merge-strategies');
const { toKebabCase } = require('./utils/helpers');
const assignFlagDefaults = require('./utils/flag-defaults');

// CLI arg resolvers
const handleConfigResolution = require('./groups/ConfigGroup');
Expand Down Expand Up @@ -44,19 +45,20 @@ class WebpackCLI extends GroupHelper {
* @private\
* @returns {void}
*/
_handleCoreFlags() {
if (!this.groupMap.has('core')) {
return;
_handleCoreFlags(parsedArgs) {
if (this.groupMap.has('core')) {
const coreFlags = this.groupMap.get('core');

// convert all the flags from map to single object
const coreConfig = coreFlags.reduce((allFlag, curFlag) => ({ ...allFlag, ...curFlag }), {});
const coreCliHelper = require('webpack').cli;
const coreCliArgs = coreCliHelper.getArguments();
// Merge the core flag config with the compilerConfiguration
coreCliHelper.processArguments(coreCliArgs, this.compilerConfiguration, coreConfig);
// Assign some defaults to core flags
}
const coreFlags = this.groupMap.get('core');

// convert all the flags from map to single object
const coreConfig = coreFlags.reduce((allFlag, curFlag) => ({ ...allFlag, ...curFlag }), {});

const coreCliHelper = require('webpack').cli;
const coreCliArgs = coreCliHelper.getArguments();
// Merge the core flag config with the compilerConfiguration
coreCliHelper.processArguments(coreCliArgs, this.compilerConfiguration, coreConfig);
const configWithDefaults = assignFlagDefaults(this.compilerConfiguration, parsedArgs);
this._mergeOptionsToConfiguration(configWithDefaults);
}

async _baseResolver(cb, parsedArgs, strategy) {
Expand Down Expand Up @@ -193,7 +195,7 @@ class WebpackCLI extends GroupHelper {
.then(() => this._baseResolver(handleConfigResolution, parsedArgs))
.then(() => this._baseResolver(resolveMode, parsedArgs))
.then(() => this._baseResolver(resolveOutput, parsedArgs, outputStrategy))
.then(() => this._handleCoreFlags())
.then(() => this._handleCoreFlags(parsedArgs))
.then(() => this._baseResolver(basicResolver, parsedArgs))
.then(() => this._baseResolver(resolveAdvanced, parsedArgs))
.then(() => this._baseResolver(resolveStats, parsedArgs))
Expand Down
48 changes: 46 additions & 2 deletions test/core-flags/cache-flags.test.js
@@ -1,13 +1,12 @@
'use strict';

const { run } = require('../utils/test-utils');
const { existsSync } = require('fs');
const { existsSync, writeFileSync, unlinkSync } = require('fs');
const { resolve } = require('path');

describe('cache related flags from core', () => {
it('should be successful with --cache ', () => {
const { stderr, stdout } = run(__dirname, ['--cache']);

expect(stderr).toBeFalsy();
expect(stdout).toContain(`type: 'memory'`);
});
Expand Down Expand Up @@ -69,4 +68,49 @@ describe('cache related flags from core', () => {
expect(stderr).toBeFalsy();
expect(stdout).toContain(`version: '1.1.3'`);
});

it('should assign cache build dependencies correctly when cache type is filesystem', () => {
const { stderr, stdout } = run(__dirname, ['--cache-type', 'filesystem', '-c', './webpack.config.js']);
expect(stderr).toBeFalsy();
expect(stdout).toContain('buildDependencies: { config: [Array]');
expect(stdout).not.toContain('[cached] 1 module');
// Run again to check for cache
const newRun = run(__dirname, ['--cache-type', 'filesystem', '-c', './webpack.config.js']);
expect(newRun.stdout).toContain('[cached] 1 module');
expect(newRun.stderr).toBeFalsy();
expect(newRun.exitCode).toEqual(0);
});

it('should assign cache build dependencies correctly when cache type is filesystem in config', () => {
const { stderr, stdout } = run(__dirname, ['-c', './webpack.cache.config.js']);
expect(stderr).toBeFalsy();
expect(stdout).toContain('buildDependencies: { config: [Array]');
anshumanv marked this conversation as resolved.
Show resolved Hide resolved
expect(stdout).not.toContain('[cached] 1 module');
// Run again to check for cache
const newRun = run(__dirname, ['-c', './webpack.cache.config.js']);
expect(newRun.stdout).toContain('[cached] 1 module');
expect(newRun.stderr).toBeFalsy();
expect(newRun.exitCode).toEqual(0);
});

it('should invalidate cache when config changes', () => {
// Creating a temporary webpack config
writeFileSync(resolve(__dirname, './webpack.test.config.js'), 'module.exports = {mode: "none"}');
const { stderr, stdout } = run(__dirname, ['--cache-type', 'filesystem', '-c', './webpack.test.config.js']);
expect(stderr).toBeFalsy();
// modules should not be cached on first run
expect(stdout).not.toContain('[cached] 1 module');

// Running again should use the cache
const newRun = run(__dirname, ['--cache-type', 'filesystem', '-c', './webpack.test.config.js']);
expect(newRun.stdout).toContain('[cached] 1 module');

// Change config to invalidate cache
writeFileSync(resolve(__dirname, './webpack.test.config.js'), 'module.exports = {mode: "production"}');

const newRun2 = run(__dirname, ['--cache-type', 'filesystem', '-c', './webpack.test.config.js']);
unlinkSync(resolve(__dirname, './webpack.test.config.js'));
expect(newRun2).not.toContain('[cached] 1 module');
expect(newRun2.exitCode).toEqual(0);
});
anshumanv marked this conversation as resolved.
Show resolved Hide resolved
});
1 change: 1 addition & 0 deletions test/core-flags/src/index.js
@@ -0,0 +1 @@
console.log("Mizuhara Chizuru")
11 changes: 11 additions & 0 deletions test/core-flags/webpack.cache.config.js
@@ -0,0 +1,11 @@
const WebpackCLITestPlugin = require('../utils/webpack-cli-test-plugin');

module.exports = {
entry: './src/main.js',
mode: 'development',
cache: {
type: 'filesystem',
},
name: 'compiler',
plugins: [new WebpackCLITestPlugin(['module', 'entry', 'resolve', 'resolveLoader'])],
};