From 7a5b33dcd5aa5a8ea37062ae93d1fc38cdb5464c Mon Sep 17 00:00:00 2001 From: Emanuele Date: Sun, 23 Feb 2020 05:54:59 +0000 Subject: [PATCH] feat(webpack-cli): added mode argument (#1253) --- .babelrc | 22 +++---- .gitignore | 1 + package.json | 10 +-- packages/webpack-cli/README.md | 1 + .../__tests__/ZeroConfigGroup.test.js | 29 +++++++-- .../webpack-cli/lib/groups/ZeroConfigGroup.js | 22 +++++-- packages/webpack-cli/lib/utils/cli-flags.js | 9 +++ .../help-single-arg.test.js.snap | 1 + test/mode/dev/dev.test.js | 51 +++++++++++++++ test/mode/dev/src/index.js | 1 + test/mode/prod/prod.test.js | 63 +++++++++++++++++++ test/mode/prod/src/index.js | 1 + yarn.lock | 14 ++--- 13 files changed, 193 insertions(+), 32 deletions(-) create mode 100644 test/mode/dev/dev.test.js create mode 100644 test/mode/dev/src/index.js create mode 100644 test/mode/prod/prod.test.js create mode 100644 test/mode/prod/src/index.js diff --git a/.babelrc b/.babelrc index e095cab0376..72009cef1f8 100644 --- a/.babelrc +++ b/.babelrc @@ -1,12 +1,12 @@ { - "presets": [[ - "@babel/preset-env", - { - "targets": { - "node": "current" - } - } - ], - "jest" - ] -} \ No newline at end of file + "presets": [ + [ + "@babel/preset-env", + { + "targets": { + "node": "current" + } + } + ] + ] +} diff --git a/.gitignore b/.gitignore index 4ca9a6c9c10..ad0e8a64be0 100644 --- a/.gitignore +++ b/.gitignore @@ -47,3 +47,4 @@ junit.xml #typescript source maps packages/**/*.map +*.tsbuildinfo diff --git a/package.json b/package.json index 1ceb3c7e0cf..ce0bb49ce58 100644 --- a/package.json +++ b/package.json @@ -133,12 +133,14 @@ "webpack": "5.x.x" }, "devDependencies": { - "@babel/preset-env": "^7.7.1", + "@babel/core": "^7.8.4", + "@babel/preset-env": "^7.8.4", "@commitlint/cli": "^8.2.0", "@commitlint/config-lerna-scopes": "^8.2.0", "@types/jest": "^24.0.22", "@typescript-eslint/eslint-plugin": "^2.17.0", "@typescript-eslint/parser": "^2.17.0", + "chalk": "^3.0.0", "commitlint": "^8.2.0", "commitlint-config-cz": "^0.12.1", "cz-customizable": "^6.2.0", @@ -149,7 +151,6 @@ "execa": "^3.2.0", "husky": "^3.0.9", "jest": "^25.1.0", - "jest-cli": "^25.1.0", "jest-junit": "^10.0.0", "jest-serializer-ansi": "^1.0.3", "lerna": "^3.20.2", @@ -157,11 +158,10 @@ "nyc": "^14.1.1", "prettier": "1.18.2", "readable-stream": "^3.5.0", - "ts-jest": "^24.1.0", + "ts-jest": "^25.2.1", "typedoc": "^0.15.0", "typescript": "^3.7.2", "webpack": "^5.0.0-beta.12", - "yeoman-test": "^2.1.0", - "chalk": "^3.0.0" + "yeoman-test": "^2.1.0" } } diff --git a/packages/webpack-cli/README.md b/packages/webpack-cli/README.md index 9fc919b5f91..8b9d965c6ee 100644 --- a/packages/webpack-cli/README.md +++ b/packages/webpack-cli/README.md @@ -54,6 +54,7 @@ Options --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 ``` diff --git a/packages/webpack-cli/__tests__/ZeroConfigGroup.test.js b/packages/webpack-cli/__tests__/ZeroConfigGroup.test.js index 2f2116ad5a8..6fd132ac3b8 100644 --- a/packages/webpack-cli/__tests__/ZeroConfigGroup.test.js +++ b/packages/webpack-cli/__tests__/ZeroConfigGroup.test.js @@ -13,14 +13,33 @@ describe('GroupHelper', function() { }); it('should load the prod zero config', () => { const group = new ZeroConfigGroup([ - [ - { - prod: true, - }, - ], + { + prod: true, + }, + ]); + + const result = group.run(); + expect(result.options.mode).toEqual('production'); + }); + it('should handle the mode option [production]', () => { + const group = new ZeroConfigGroup([ + { + mode: 'production', + }, ]); const result = group.run(); expect(result.options.mode).toEqual('production'); }); + + it('should handle the mode option [development]', () => { + const group = new ZeroConfigGroup([ + { + mode: 'development', + }, + ]); + + const result = group.run(); + expect(result.options.mode).toEqual('development'); + }); }); diff --git a/packages/webpack-cli/lib/groups/ZeroConfigGroup.js b/packages/webpack-cli/lib/groups/ZeroConfigGroup.js index c9c8929a8d9..ac65a713552 100644 --- a/packages/webpack-cli/lib/groups/ZeroConfigGroup.js +++ b/packages/webpack-cli/lib/groups/ZeroConfigGroup.js @@ -1,4 +1,5 @@ const GroupHelper = require('../utils/GroupHelper'); +const { logger } = require('@webpack-cli/logger'); const PRODUCTION = 'production'; const DEVELOPMENT = 'development'; @@ -17,12 +18,25 @@ class ZeroConfigGroup extends GroupHelper { getEnvFromOptionsAndMode() { if (process.env.NODE_ENV && (process.env.NODE_ENV === PRODUCTION || process.env.NODE_ENV === DEVELOPMENT)) { return process.env.NODE_ENV; - } else if (this.args.prod) { + } else { + if (this.args.mode && (this.args.dev || this.args.prod)) { + logger.warn( + `You provided both mode and ${ + this.args.prod ? '--prod' : '--dev' + } arguments. You should provide just one. "mode" will be used`, + ); + return this.args.mode; + } + if (this.args.mode) { + return this.args.mode; + } + if (this.args.prod) { + return PRODUCTION; + } else if (this.args.dev) { + return DEVELOPMENT; + } return PRODUCTION; - } else if (this.args.dev) { - return DEVELOPMENT; } - return PRODUCTION; } resolveZeroConfig() { diff --git a/packages/webpack-cli/lib/utils/cli-flags.js b/packages/webpack-cli/lib/utils/cli-flags.js index 433d8f9781f..b84c49b623a 100644 --- a/packages/webpack-cli/lib/utils/cli-flags.js +++ b/packages/webpack-cli/lib/utils/cli-flags.js @@ -283,6 +283,15 @@ module.exports = { description: 'Run production build', link: 'https://webpack.js.org/concepts/#mode', }, + { + name: 'mode', + usage: '--mode ', + type: String, + group: ZERO_CONFIG_GROUP, + description: 'Defines the mode to pass to webpack', + link: 'https://webpack.js.org/concepts/#mode', + acceptedValues: ["development", "production"] + }, { 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 3c8bb2e2c94..731eb62f5bf 100644 --- a/test/help/__snapshots__/help-single-arg.test.js.snap +++ b/test/help/__snapshots__/help-single-arg.test.js.snap @@ -44,6 +44,7 @@ Options --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 diff --git a/test/mode/dev/dev.test.js b/test/mode/dev/dev.test.js new file mode 100644 index 00000000000..5f74a5d2b4e --- /dev/null +++ b/test/mode/dev/dev.test.js @@ -0,0 +1,51 @@ +'use strict'; +const { run } = require('../../utils/test-utils'); +const { stat } = require('fs'); +const { resolve } = require('path'); +describe('mode flags', () => { + it('should load a development config when --dev is passed', done => { + const { stderr, stdout } = run(__dirname, ['--dev']); + 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 --mode=development is passed', done => { + const { stderr, stdout } = run(__dirname, ['--mode', 'development']); + 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 --mode=development and --dev are passed', done => { + const { stderr, stdout } = run(__dirname, ['--mode', 'development', '--dev']); + 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 --prod are passed', done => { + const { stderr, stdout } = run(__dirname, ['--mode', 'development', '--prod']); + 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/mode/dev/src/index.js b/test/mode/dev/src/index.js new file mode 100644 index 00000000000..0a899cfdc75 --- /dev/null +++ b/test/mode/dev/src/index.js @@ -0,0 +1 @@ +console.log('default'); diff --git a/test/mode/prod/prod.test.js b/test/mode/prod/prod.test.js new file mode 100644 index 00000000000..6d6482c2037 --- /dev/null +++ b/test/mode/prod/prod.test.js @@ -0,0 +1,63 @@ +'use strict'; +const { run } = require('../../utils/test-utils'); +const { stat } = require('fs'); +const { resolve } = require('path'); +describe('mode flags', () => { + it('should load a production config when --prod is passed', done => { + const { stderr, stdout } = run(__dirname, ['--prod']); + 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 production config when --mode=production is passed', done => { + const { stderr, stdout } = run(__dirname, ['--mode', 'production']); + 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 production config when --mode=production and --prod are passed', done => { + const { stderr, stdout } = run(__dirname, ['--mode', 'production', '--prod']); + 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 production config when --mode=production and --dev are passed', done => { + const { stderr, stdout } = run(__dirname, ['--mode', 'production', '--dev']); + 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 production config when passing --dev and --prod', done => { + const { stderr, stdout } = run(__dirname, ['--prod', '--dev']); + expect(stderr).toBeFalsy(); + 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/mode/prod/src/index.js b/test/mode/prod/src/index.js new file mode 100644 index 00000000000..0a899cfdc75 --- /dev/null +++ b/test/mode/prod/src/index.js @@ -0,0 +1 @@ +console.log('default'); diff --git a/yarn.lock b/yarn.lock index 26933e66c3c..079385703df 100644 --- a/yarn.lock +++ b/yarn.lock @@ -18,7 +18,7 @@ invariant "^2.2.4" semver "^5.5.0" -"@babel/core@^7.1.0", "@babel/core@^7.1.6", "@babel/core@^7.7.5": +"@babel/core@^7.1.0", "@babel/core@^7.1.6", "@babel/core@^7.7.5", "@babel/core@^7.8.4": version "7.8.4" resolved "https://registry.yarnpkg.com/@babel/core/-/core-7.8.4.tgz#d496799e5c12195b3602d0fddd77294e3e38e80e" integrity sha512-0LiLrB2PwrVI+a2/IEskBopDYSd8BCb3rOvH7D5tzoWd696TBEduBvuLVm4Nx6rltrLZqvI3MCalB2K2aVzQjA== @@ -12094,10 +12094,10 @@ trim-off-newlines@^1.0.0: resolved "https://registry.yarnpkg.com/trim-off-newlines/-/trim-off-newlines-1.0.1.tgz#9f9ba9d9efa8764c387698bcbfeb2c848f11adb3" integrity sha1-n5up2e+odkw4dpi8v+sshI8RrbM= -ts-jest@^24.1.0: - version "24.3.0" - resolved "https://registry.yarnpkg.com/ts-jest/-/ts-jest-24.3.0.tgz#b97814e3eab359ea840a1ac112deae68aa440869" - integrity sha512-Hb94C/+QRIgjVZlJyiWwouYUF+siNJHJHknyspaOcZ+OQAIdFG/UrdQVXw/0B8Z3No34xkUXZJpOTy9alOWdVQ== +ts-jest@^25.2.1: + version "25.2.1" + resolved "https://registry.yarnpkg.com/ts-jest/-/ts-jest-25.2.1.tgz#49bf05da26a8b7fbfbc36b4ae2fcdc2fef35c85d" + integrity sha512-TnntkEEjuXq/Gxpw7xToarmHbAafgCaAzOpnajnFC6jI7oo1trMzAHA04eWpc3MhV6+yvhE8uUBAmN+teRJh0A== dependencies: bs-logger "0.x" buffer-from "1.x" @@ -12108,7 +12108,7 @@ ts-jest@^24.1.0: mkdirp "0.x" resolve "1.x" semver "^5.5" - yargs-parser "10.x" + yargs-parser "^16.1.0" tslib@^1.8.1, tslib@^1.9.0: version "1.10.0" @@ -13050,7 +13050,7 @@ yallist@^4.0.0: resolved "https://registry.yarnpkg.com/yallist/-/yallist-4.0.0.tgz#9bb92790d9c0effec63be73519e11a35019a3a72" integrity sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A== -yargs-parser@10.x, yargs-parser@^10.0.0: +yargs-parser@^10.0.0: version "10.1.0" resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-10.1.0.tgz#7202265b89f7e9e9f2e5765e0fe735a905edbaa8" integrity sha512-VCIyR1wJoEBZUqk5PA+oOBF6ypbwh5aNB3I50guxAL/quggdfs4TtNHQrSazFA3fYZ+tEqfs0zIGlv0c/rgjbQ==