Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages/webpack-cli/lib/utils/flag-defaults.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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]');
Comment thread
anshumanv marked this conversation as resolved.
Outdated
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);
});
Comment thread
anshumanv marked this conversation as resolved.
});
1 change: 1 addition & 0 deletions test/core-flags/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("Mizuhara Chizuru")
11 changes: 11 additions & 0 deletions test/core-flags/webpack.cache.config.js
Original file line number Diff line number Diff line change
@@ -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'])],
};