Skip to content

Commit

Permalink
refactor: 💡 remove defaults flag (#1543)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: the `defaults` options was removed without replacement
  • Loading branch information
anshumanv authored May 15, 2020
1 parent 6a8dd32 commit a905590
Show file tree
Hide file tree
Showing 10 changed files with 2 additions and 180 deletions.
1 change: 0 additions & 1 deletion packages/webpack-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ Options
--progress Print compilation progress during build
--silent Disable any output that webpack makes
--help Outputs list of supported flags
--defaults Allow webpack to set defaults aggresively
-o, --output string Output location of the file generated by webpack
--plugin string Load a given plugin
-g, --global string[] Declares and exposes a global variable
Expand Down
8 changes: 0 additions & 8 deletions packages/webpack-cli/lib/utils/cli-flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,6 @@ module.exports = {
group: HELP_GROUP,
description: 'Outputs list of supported flags',
},
{
name: 'defaults',
usage: '--defaults',
type: Boolean,
group: BASIC_GROUP,
description: 'Allow webpack to set defaults aggresively',
link: 'https://github.com/webpack-contrib/webpack-defaults',
},
{
name: 'output',
usage: '--output <path to output directory> e.g. ./dist/',
Expand Down
45 changes: 0 additions & 45 deletions packages/webpack-cli/lib/utils/zero-config.js

This file was deleted.

46 changes: 1 addition & 45 deletions packages/webpack-cli/lib/webpack-cli.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const { resolve, parse } = require('path');
const { existsSync } = require('fs');
const GroupHelper = require('./utils/GroupHelper');
const { Compiler } = require('./utils/Compiler');
const { groups, core } = require('./utils/cli-flags');
Expand Down Expand Up @@ -52,35 +50,6 @@ class WebpackCLI extends GroupHelper {
this.groupMap.set(groupName, [{ [opt.name]: val }]);
}
}
checkDefaults(options, outputOptions) {
if (Array.isArray(options)) {
return options.map((opt) => this.checkDefaults(opt, outputOptions));
}
if (options.entry && this.possibleFileNames.includes(options.entry)) {
const absFilename = parse(options.entry);
let tmpFileName = options.entry;
if (absFilename.ext !== '.js') {
tmpFileName += '.js';
}
const normalizedEntry = resolve(tmpFileName);
if (!existsSync(normalizedEntry)) {
const parsedPath = parse(normalizedEntry);
const possibleEntries = this.possibleFileNames
.map((f) => {
return resolve(parsedPath.dir, f);
})
.filter((e) => existsSync(e));

if (possibleEntries.length) {
options.entry = possibleEntries[0];
}
}
}
if (outputOptions.devtool) {
options.devtool = outputOptions.devtool;
}
return options;
}

/**
* Expose commander argParser
Expand Down Expand Up @@ -244,18 +213,6 @@ class WebpackCLI extends GroupHelper {
this._mergeOptionsToConfiguration(options);
}

/**
* Responsible for applying defaults, if necessary
* @private\
* @returns {void}
*/
_handForcedDefaults() {
if (this.outputConfiguration.defaults) {
const wrappedConfig = require('./utils/zero-config')(this.compilerConfiguration, this.outputConfiguration);
this.compilerConfiguration = this.checkDefaults(wrappedConfig.options, this.outputConfiguration);
}
}

/**
* It runs in a fancy order all the expected groups.
* Zero config and configuration goes first.
Expand All @@ -272,8 +229,7 @@ class WebpackCLI extends GroupHelper {
.then(() => this._handleGroupHelper(this.basicGroup))
.then(() => this._handleGroupHelper(this.advancedGroup))
.then(() => this._handleGroupHelper(this.statsGroup))
.then(() => this._handleGroupHelper(this.helpGroup))
.then(() => this._handForcedDefaults());
.then(() => this._handleGroupHelper(this.helpGroup));
}

async processArgs(args, cliOptions) {
Expand Down
14 changes: 1 addition & 13 deletions test/defaults/output-defaults.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { run } = require('../utils/test-utils');
describe('output flag defaults', () => {
it('should create default file for a given directory', (done) => {
const { stdout } = run(__dirname, ['--entry', './a.js', '--output', './binary'], false);
// Should print a warning about config fallback since we did not supply --defaults
// Should print a warning about config fallback
expect(stdout).toContain('option has not been set, webpack will fallback to');
stat(resolve(__dirname, './binary/main.js'), (err, stats) => {
expect(err).toBe(null);
Expand All @@ -29,16 +29,4 @@ describe('output flag defaults', () => {
const { stderr } = run(__dirname, ['--entry', './a.js', '--output'], false);
expect(stderr).toContain("error: option '-o, --output <value>' argument missing");
});

it('should not throw when --defaults flag is passed', (done) => {
const { stdout, stderr } = run(__dirname, ['--defaults'], false);
// When using --defaults it should not print warnings about config fallback
expect(stdout).not.toContain('option has not been set, webpack will fallback to');
expect(stderr).toBeFalsy();
stat(resolve(__dirname, './dist/main.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
done();
});
});
});
19 changes: 0 additions & 19 deletions test/defaults/with-config-and-entry/default-with-config.test.js

This file was deleted.

1 change: 0 additions & 1 deletion test/defaults/with-config-and-entry/index.js

This file was deleted.

9 changes: 0 additions & 9 deletions test/defaults/with-config-and-entry/webpack.config.js

This file was deleted.

This file was deleted.

22 changes: 0 additions & 22 deletions test/output/named-bundles/output-named-bundles.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,6 @@ describe('output flag named bundles', () => {
expect(stats.isFile()).toBe(true);
});

it('should not throw error on same bundle name for multiple entries with defaults', () => {
const { stderr } = run(__dirname, ['-c', resolve(__dirname, 'webpack.defaults.config.js'), '--defaults'], false);
expect(stderr).toBeFalsy();

let stats = statSync(resolve(__dirname, './dist/b.main.js'));
expect(stats.isFile()).toBe(true);
stats = statSync(resolve(__dirname, './dist/c.main.js'));
expect(stats.isFile()).toBe(true);
});

it('should successfully compile multiple entries', () => {
const { stderr } = run(__dirname, ['-c', resolve(__dirname, 'webpack.multiple.config.js')], false);
expect(stderr).toBeFalsy();
Expand All @@ -74,16 +64,4 @@ describe('output flag named bundles', () => {
const stats = statSync(resolve(__dirname, './bin/bundle.js'));
expect(stats.isFile()).toBe(true);
});

it('should output file in dist directory using default value with warning for empty output value', () => {
const { stderr } = run(__dirname, ['-c', resolve(__dirname, 'webpack.defaults.config.js'), '--defaults', '--output='], false);
expect(stderr).toContain(
"You provided an empty output value. Falling back to the output value of your webpack config file, or './dist/' if none was provided",
);

let stats = statSync(resolve(__dirname, './dist/b.main.js'));
expect(stats.isFile()).toBe(true);
stats = statSync(resolve(__dirname, './dist/c.main.js'));
expect(stats.isFile()).toBe(true);
});
});

0 comments on commit a905590

Please sign in to comment.