From 6a45b54f988b742fac044a19218fbb913e06da10 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Thu, 27 Feb 2020 21:59:17 +0530 Subject: [PATCH] feat(webpack-cli): add no-mode flag --- packages/webpack-cli/README.md | 3 +- .../webpack-cli/lib/groups/ZeroConfigGroup.js | 20 ++++-- packages/webpack-cli/lib/utils/cli-flags.js | 8 +++ .../help-single-arg.test.js.snap | 45 ++++++------- test/no-mode/no-mode.test.js | 64 +++++++++++++++++++ test/no-mode/src/index.js | 1 + 6 files changed, 114 insertions(+), 27 deletions(-) create mode 100644 test/no-mode/no-mode.test.js create mode 100644 test/no-mode/src/index.js diff --git a/packages/webpack-cli/README.md b/packages/webpack-cli/README.md index 8b9d965c6ee..d0aa9d03a6c 100644 --- a/packages/webpack-cli/README.md +++ b/packages/webpack-cli/README.md @@ -36,7 +36,7 @@ Available Commands Options --entry string The entry point of your application. - -c, --config string Provide path to a webpack configuration file + -c, --config string Provide path to a webpack configuration file -m, --merge string Merge a configuration file using webpack-merge --progress Print compilation progress during build --silent Disable any output that webpack makes @@ -55,6 +55,7 @@ Options -d, --dev Run development build -p, --prod Run production build --mode string Defines the mode to pass to webpack + --no-mode Sets mode="none" which disables any default behavior --version Get current version --node-args string[] NodeJS flags ``` diff --git a/packages/webpack-cli/lib/groups/ZeroConfigGroup.js b/packages/webpack-cli/lib/groups/ZeroConfigGroup.js index ac65a713552..215f663f988 100644 --- a/packages/webpack-cli/lib/groups/ZeroConfigGroup.js +++ b/packages/webpack-cli/lib/groups/ZeroConfigGroup.js @@ -3,6 +3,7 @@ const { logger } = require('@webpack-cli/logger'); const PRODUCTION = 'production'; const DEVELOPMENT = 'development'; +const NONE = 'none'; /** * ZeroConfigGroup creates a zero configuration based on the environment */ @@ -19,13 +20,22 @@ class ZeroConfigGroup extends GroupHelper { if (process.env.NODE_ENV && (process.env.NODE_ENV === PRODUCTION || process.env.NODE_ENV === DEVELOPMENT)) { return process.env.NODE_ENV; } else { - if (this.args.mode && (this.args.dev || this.args.prod)) { + if ((this.args.mode || this.args.noMode) && (this.args.dev || this.args.prod)) { logger.warn( - `You provided both mode and ${ + `You provided both ${this.args.mode ? 'mode' : 'no-mode'} and ${ this.args.prod ? '--prod' : '--dev' - } arguments. You should provide just one. "mode" will be used`, + } arguments. You should provide just one. "${this.args.mode ? 'mode' : 'no-mode'}" will be used`, ); - return this.args.mode; + if (this.args.mode){ + return this.args.mode ; + } else { + return NONE ; + } + } + if (this.args.noMode && this.args.mode) { + logger.warn( + 'You Provided both mode and no-mode arguments. You Should Provide just one. "mode" will be used.' + ) } if (this.args.mode) { return this.args.mode; @@ -34,6 +44,8 @@ class ZeroConfigGroup extends GroupHelper { return PRODUCTION; } else if (this.args.dev) { return DEVELOPMENT; + } else if (this.args.noMode) { + return NONE; } return PRODUCTION; } diff --git a/packages/webpack-cli/lib/utils/cli-flags.js b/packages/webpack-cli/lib/utils/cli-flags.js index b84c49b623a..5264a2146e5 100644 --- a/packages/webpack-cli/lib/utils/cli-flags.js +++ b/packages/webpack-cli/lib/utils/cli-flags.js @@ -292,6 +292,14 @@ module.exports = { link: 'https://webpack.js.org/concepts/#mode', acceptedValues: ["development", "production"] }, + { + name: 'no-mode', + usage: '--no-mode', + type: Boolean, + group: ZERO_CONFIG_GROUP, + description: 'Sets mode="none" which disables any default behavior', + link: 'https://webpack.js.org/concepts/#mode', + }, { name: 'version', usage: '--version', diff --git a/test/help/__snapshots__/help-single-arg.test.js.snap b/test/help/__snapshots__/help-single-arg.test.js.snap index 731eb62f5bf..3ef3bcec597 100644 --- a/test/help/__snapshots__/help-single-arg.test.js.snap +++ b/test/help/__snapshots__/help-single-arg.test.js.snap @@ -25,28 +25,29 @@ Available Commands Options - --entry string The entry point of your application. - -c, --config string Provide path to a webpack configuration file - -m, --merge string Merge a configuration file using webpack-merge - --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 - -t, --target string Sets the build target - -w, --watch Watch for files changes - -h, --hot Enables Hot Module Replacement - -s, --sourcemap string Determine source maps to use - --prefetch string Prefetch this request - -j, --json Prints result as JSON - --standard Prints standard output - -d, --dev Run development build - -p, --prod Run production build - --mode string Defines the mode to pass to webpack - --version Get current version - --node-args string[] NodeJS flags + --entry string The entry point of your application. + -c, --config string Provide path to a webpack configuration file + -m, --merge string Merge a configuration file using webpack-merge + --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 + -t, --target string Sets the build target + -w, --watch Watch for files changes + -h, --hot Enables Hot Module Replacement + -s, --sourcemap string Determine source maps to use + --prefetch string Prefetch this request + -j, --json Prints result as JSON + --standard Prints standard output + -d, --dev Run development build + -p, --prod Run production build + --mode string Defines the mode to pass to webpack + --no-mode Sets mode="none" which disables any default behavior + --version Get current version + --node-args string[] NodeJS flags Made with ♥️ by the webpack team " `; diff --git a/test/no-mode/no-mode.test.js b/test/no-mode/no-mode.test.js new file mode 100644 index 00000000000..c043e9f4740 --- /dev/null +++ b/test/no-mode/no-mode.test.js @@ -0,0 +1,64 @@ +'use strict'; +const { run } = require('../utils/test-utils'); +const { stat } = require('fs'); +const { resolve } = require('path'); +describe('no-mode flag', () => { + it('should load a development config when --no-mode is passed', done => { + const { stderr, stdout } = run(__dirname, ['--no-mode']); + expect(stderr).toBeFalsy(); + expect(stdout).toBeTruthy(); + stat(resolve(__dirname, './bin/main.js'), (err, stats) => { + expect(err).toBe(null); + expect(stats.isFile()).toBe(true); + done(); + }); + }); + + it('should load a development config when --no-mode and --dev are passed', done => { + const { stderr, stdout } = run(__dirname, ['--no-mode', '--dev']); + expect(stderr).toContain('"no-mode" will be used'); + expect(stdout).toBeTruthy(); + + stat(resolve(__dirname, './bin/main.js'), (err, stats) => { + expect(err).toBe(null); + expect(stats.isFile()).toBe(true); + done(); + }); + }); + + it('should load a development config when --no-mode and --prod are passed', done => { + const { stderr, stdout } = run(__dirname, ['--no-mode', '--prod']); + expect(stderr).toContain('"no-mode" will be used'); + expect(stdout).toBeTruthy(); + + stat(resolve(__dirname, './bin/main.js'), (err, stats) => { + expect(err).toBe(null); + expect(stats.isFile()).toBe(true); + done(); + }); + }); + + it('should load a production config when --mode=production & --no-mode are passed', done => { + const { stderr, stdout } = run(__dirname, ['--mode', 'production', '--no-mode']); + expect(stderr).toContain('"mode" will be used'); + expect(stdout).toBeTruthy(); + + stat(resolve(__dirname, './bin/main.js'), (err, stats) => { + expect(err).toBe(null); + expect(stats.isFile()).toBe(true); + done(); + }); + }); + + it('should load a development config when --mode=development and --no-mode are passed', done => { + const { stderr, stdout } = run(__dirname, ['--mode', 'development', '--no-mode']); + expect(stderr).toContain('"mode" will be used'); + expect(stdout).toBeTruthy(); + + stat(resolve(__dirname, './bin/main.js'), (err, stats) => { + expect(err).toBe(null); + expect(stats.isFile()).toBe(true); + done(); + }); + }); +}); diff --git a/test/no-mode/src/index.js b/test/no-mode/src/index.js new file mode 100644 index 00000000000..a767aed6690 --- /dev/null +++ b/test/no-mode/src/index.js @@ -0,0 +1 @@ +console.log('Test'); \ No newline at end of file