From a9055902ea27c3b3ea66c334e6a8aa2f1848b6be Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Fri, 15 May 2020 16:04:05 +0530 Subject: [PATCH] =?UTF-8?q?refactor:=20=F0=9F=92=A1=20remove=20defaults=20?= =?UTF-8?q?flag=20(#1543)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit BREAKING CHANGE: the `defaults` options was removed without replacement --- packages/webpack-cli/README.md | 1 - packages/webpack-cli/lib/utils/cli-flags.js | 8 ---- packages/webpack-cli/lib/utils/zero-config.js | 45 ------------------ packages/webpack-cli/lib/webpack-cli.js | 46 +------------------ test/defaults/output-defaults.test.js | 14 +----- .../default-with-config.test.js | 19 -------- test/defaults/with-config-and-entry/index.js | 1 - .../with-config-and-entry/webpack.config.js | 9 ---- .../default-without-config.test.js | 17 ------- .../output-named-bundles.test.js | 22 --------- 10 files changed, 2 insertions(+), 180 deletions(-) delete mode 100644 packages/webpack-cli/lib/utils/zero-config.js delete mode 100644 test/defaults/with-config-and-entry/default-with-config.test.js delete mode 100644 test/defaults/with-config-and-entry/index.js delete mode 100644 test/defaults/with-config-and-entry/webpack.config.js delete mode 100644 test/defaults/without-config-and-entry/default-without-config.test.js diff --git a/packages/webpack-cli/README.md b/packages/webpack-cli/README.md index f9b11684d9e..c89fc5e9919 100644 --- a/packages/webpack-cli/README.md +++ b/packages/webpack-cli/README.md @@ -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 diff --git a/packages/webpack-cli/lib/utils/cli-flags.js b/packages/webpack-cli/lib/utils/cli-flags.js index 657897e5a08..56dbfa8bbd4 100644 --- a/packages/webpack-cli/lib/utils/cli-flags.js +++ b/packages/webpack-cli/lib/utils/cli-flags.js @@ -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 e.g. ./dist/', diff --git a/packages/webpack-cli/lib/utils/zero-config.js b/packages/webpack-cli/lib/utils/zero-config.js deleted file mode 100644 index d6b0e794893..00000000000 --- a/packages/webpack-cli/lib/utils/zero-config.js +++ /dev/null @@ -1,45 +0,0 @@ -function getEnvFromOptionsAndMode(mode, optionsObject) { - const { dev, prod } = optionsObject; - const NODE_ENV = process.env.NODE_ENV; - if (NODE_ENV && (NODE_ENV === 'production' || NODE_ENV === 'development')) { - return NODE_ENV; - } else if (prod) { - return 'production'; - } else if (dev) { - return 'development'; - } else if (mode) { - return mode; - } - return 'production'; -} - -function getConfigurations(options, outputOptions) { - const merge = require('webpack-merge'); - const { mode } = options; - const defaultConfigType = getEnvFromOptionsAndMode(mode, outputOptions); - const defaultConfig = require(`./${defaultConfigType}-config`)(options, outputOptions); - const newConfig = merge(defaultConfig, options); - newConfig.mode = defaultConfigType; - - const isEntryObject = newConfig.entry && newConfig.entry instanceof Object; - const isOutputDefined = newConfig.output && newConfig.output.filename; - const isConflictingOutput = isEntryObject && isOutputDefined && newConfig.output.filename === 'main.js'; - if (isConflictingOutput) { - newConfig.output.filename = '[name].main.js'; - } - return newConfig; -} - -module.exports = function setDefaultConfigBasedOnEnvironment(options, outputOptions) { - let newOptions; - if (Array.isArray(options)) { - newOptions = options.map((arrayOptions) => getConfigurations(arrayOptions, outputOptions)); - return { - options: newOptions, - }; - } - newOptions = getConfigurations(options, outputOptions); - return { - options: newOptions, - }; -}; diff --git a/packages/webpack-cli/lib/webpack-cli.js b/packages/webpack-cli/lib/webpack-cli.js index 0ad86885d86..ae8e18683bc 100644 --- a/packages/webpack-cli/lib/webpack-cli.js +++ b/packages/webpack-cli/lib/webpack-cli.js @@ -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'); @@ -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 @@ -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. @@ -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) { diff --git a/test/defaults/output-defaults.test.js b/test/defaults/output-defaults.test.js index 8e04eb511ee..cceb480b8cf 100644 --- a/test/defaults/output-defaults.test.js +++ b/test/defaults/output-defaults.test.js @@ -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); @@ -29,16 +29,4 @@ describe('output flag defaults', () => { const { stderr } = run(__dirname, ['--entry', './a.js', '--output'], false); expect(stderr).toContain("error: option '-o, --output ' 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(); - }); - }); }); diff --git a/test/defaults/with-config-and-entry/default-with-config.test.js b/test/defaults/with-config-and-entry/default-with-config.test.js deleted file mode 100644 index 6a26b7d0089..00000000000 --- a/test/defaults/with-config-and-entry/default-with-config.test.js +++ /dev/null @@ -1,19 +0,0 @@ -'use strict'; -const { stat } = require('fs'); -const { resolve } = require('path'); -const { run } = require('../../utils/test-utils'); - -describe('output flag defaults with config', () => { - it.skip('should use default entry if config entry file is not present', (done) => { - const { stdout, stderr } = run(__dirname, ['--defaults'], false); - // Should use the output dir specified in the config - expect(stdout).toContain('./index.js'); - // Should not throw because of unknown entry in config since it will pickup the default entry - expect(stderr).toBeFalsy(); - stat(resolve(__dirname, './binary/a.bundle.js'), (err, stats) => { - expect(err).toBe(null); - expect(stats.isFile()).toBe(true); - done(); - }); - }); -}); diff --git a/test/defaults/with-config-and-entry/index.js b/test/defaults/with-config-and-entry/index.js deleted file mode 100644 index 0dd284f3d81..00000000000 --- a/test/defaults/with-config-and-entry/index.js +++ /dev/null @@ -1 +0,0 @@ -console.log('Kamado Tanjiro'); diff --git a/test/defaults/with-config-and-entry/webpack.config.js b/test/defaults/with-config-and-entry/webpack.config.js deleted file mode 100644 index e74e9d7d4a2..00000000000 --- a/test/defaults/with-config-and-entry/webpack.config.js +++ /dev/null @@ -1,9 +0,0 @@ -const { resolve } = require('path'); - -module.exports = { - entry: './unknown.js', - output: { - path: resolve(__dirname, 'binary'), - filename: 'a.bundle.js', - }, -}; diff --git a/test/defaults/without-config-and-entry/default-without-config.test.js b/test/defaults/without-config-and-entry/default-without-config.test.js deleted file mode 100644 index c0455fc7d7a..00000000000 --- a/test/defaults/without-config-and-entry/default-without-config.test.js +++ /dev/null @@ -1,17 +0,0 @@ -'use strict'; -const { stat } = require('fs'); -const { resolve } = require('path'); -const { run } = require('../../utils/test-utils'); - -describe('output flag defaults without config', () => { - it('should throw if the entry file is not present', (done) => { - const { stdout } = run(__dirname, ['--defaults'], false); - - expect(stdout).toContain("Error: Can't resolve './index.js' in"); - stat(resolve(__dirname, './dist/main.js'), (err, stats) => { - expect(err).toBeTruthy(); - expect(stats).toBe(undefined); - done(); - }); - }); -}); diff --git a/test/output/named-bundles/output-named-bundles.test.js b/test/output/named-bundles/output-named-bundles.test.js index e0070ba91db..03657f1f6ca 100644 --- a/test/output/named-bundles/output-named-bundles.test.js +++ b/test/output/named-bundles/output-named-bundles.test.js @@ -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(); @@ -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); - }); });