From 34947ddd7d932b9303ba0014dcf9a96917128dae Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Wed, 19 Aug 2020 01:01:01 +0530 Subject: [PATCH 1/4] feat: add name flag --- packages/webpack-cli/README.md | 1 + packages/webpack-cli/lib/groups/BasicGroup.js | 3 +++ packages/webpack-cli/lib/utils/cli-flags.js | 7 +++++++ test/name/index.js | 1 + test/name/name.test.js | 10 ++++++++++ test/name/webpack.config.js | 5 +++++ 6 files changed, 27 insertions(+) create mode 100644 test/name/index.js create mode 100644 test/name/name.test.js create mode 100644 test/name/webpack.config.js diff --git a/packages/webpack-cli/README.md b/packages/webpack-cli/README.md index 637ac81ea74..f1bfe4fb9a3 100644 --- a/packages/webpack-cli/README.md +++ b/packages/webpack-cli/README.md @@ -44,6 +44,7 @@ yarn add webpack-cli --dev --color Enables colors on console --no-color Disable colors on console --env string Environment passed to the configuration when it is a function + --name string Name of the configuration. Used when loading multiple configurations --help Outputs list of supported flags -o, --output string Output location of the file generated by webpack -t, --target string Sets the build target diff --git a/packages/webpack-cli/lib/groups/BasicGroup.js b/packages/webpack-cli/lib/groups/BasicGroup.js index 028fd149520..588c3758726 100644 --- a/packages/webpack-cli/lib/groups/BasicGroup.js +++ b/packages/webpack-cli/lib/groups/BasicGroup.js @@ -33,6 +33,9 @@ class BasicGroup extends GroupHelper { if (arg === 'devtool') { options.devtool = args[arg]; } + if (arg === 'name') { + options.name = args[arg]; + } if (arg === 'entry') { options[arg] = this.resolveFilePath(args[arg], 'index.js'); if (options[arg].length === 0) { diff --git a/packages/webpack-cli/lib/utils/cli-flags.js b/packages/webpack-cli/lib/utils/cli-flags.js index 0e4afea8193..38eb7e33bcc 100644 --- a/packages/webpack-cli/lib/utils/cli-flags.js +++ b/packages/webpack-cli/lib/utils/cli-flags.js @@ -261,6 +261,13 @@ module.exports = { group: CONFIG_GROUP, description: 'Environment passed to the configuration when it is a function', }, + { + name: 'name', + usage: '--name', + type: String, + group: BASIC_GROUP, + description: 'Name of the configuration. Used when loading multiple configurations.', + }, ...flagsFromCore, /* { name: "analyze", diff --git a/test/name/index.js b/test/name/index.js new file mode 100644 index 00000000000..8b51892ab5d --- /dev/null +++ b/test/name/index.js @@ -0,0 +1 @@ +console.log("Tensa Zangetsu") diff --git a/test/name/name.test.js b/test/name/name.test.js new file mode 100644 index 00000000000..7321c6c99af --- /dev/null +++ b/test/name/name.test.js @@ -0,0 +1,10 @@ +'use strict'; +const { run } = require('../utils/test-utils'); + +describe('name flag', () => { + it('should set the flag in the config', () => { + const { stdout, stderr } = run(__dirname, ['--name', 'config-name'], false); + expect(stderr).toBeFalsy(); + expect(stdout).toContain("name: 'config-name'"); + }); +}); diff --git a/test/name/webpack.config.js b/test/name/webpack.config.js new file mode 100644 index 00000000000..385cb2ac035 --- /dev/null +++ b/test/name/webpack.config.js @@ -0,0 +1,5 @@ +const WebpackCLITestPlugin = require('../utils/webpack-cli-test-plugin'); + +module.exports = { + plugins: [new WebpackCLITestPlugin()], +}; From 851d30dc36b21164c7fc568d66743c301bbfb149 Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Thu, 20 Aug 2020 22:52:38 +0530 Subject: [PATCH 2/4] chore: add name in dup flag --- packages/webpack-cli/lib/utils/cli-flags.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/webpack-cli/lib/utils/cli-flags.js b/packages/webpack-cli/lib/utils/cli-flags.js index 38eb7e33bcc..8819663aef4 100644 --- a/packages/webpack-cli/lib/utils/cli-flags.js +++ b/packages/webpack-cli/lib/utils/cli-flags.js @@ -33,7 +33,7 @@ let flagsFromCore = : []; // duplicate flags -const duplicateFlags = ['entry', 'mode', 'stats', 'watch', 'target', 'devtool']; +const duplicateFlags = ['entry', 'mode', 'stats', 'watch', 'target', 'devtool', 'name']; flagsFromCore = flagsFromCore.filter((flag) => !duplicateFlags.includes(flag.name)); From e66b1496515b480805263134173e613a6628455c Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Thu, 20 Aug 2020 23:04:49 +0530 Subject: [PATCH 3/4] chore: refactor cli-flags --- packages/webpack-cli/lib/utils/cli-flags.js | 490 ++++++++++---------- 1 file changed, 244 insertions(+), 246 deletions(-) diff --git a/packages/webpack-cli/lib/utils/cli-flags.js b/packages/webpack-cli/lib/utils/cli-flags.js index 8819663aef4..9ad0ddaac30 100644 --- a/packages/webpack-cli/lib/utils/cli-flags.js +++ b/packages/webpack-cli/lib/utils/cli-flags.js @@ -8,6 +8,245 @@ const ADVANCED_GROUP = 'advanced'; const DISPLAY_GROUP = 'stats'; const ZERO_CONFIG_GROUP = 'zero-config'; +const groups = { + HELP_GROUP, + CONFIG_GROUP, + BASIC_GROUP, + OUTPUT_GROUP, + ADVANCED_GROUP, + DISPLAY_GROUP, + ZERO_CONFIG_GROUP, +}; + +const commands = [ + { + name: 'init', + alias: 'c', + type: String, + usage: 'init [scaffold]', + description: 'Initialize a new webpack configuration', + }, + { + name: 'migrate', + alias: 'm', + type: String, + usage: 'migrate', + description: 'Migrate a configuration to a new version', + }, + { + name: 'loader', + scope: 'external', + alias: 'l', + type: String, + usage: 'loader', + description: 'Scaffold a loader repository', + }, + { + name: 'plugin', + alias: 'p', + scope: 'external', + type: String, + usage: 'plugin', + description: 'Scaffold a plugin repository', + }, + { + name: 'info', + scope: 'external', + alias: 'i', + type: String, + usage: 'info [options]', + description: 'Outputs information about your system and dependencies', + flags: [ + { + name: 'output', + type: String, + group: OUTPUT_GROUP, + description: 'To get the output in specified format ( accept json or markdown )', + }, + ], + }, + { + name: 'serve', + alias: 's', + scope: 'external', + type: String, + usage: 'serve', + description: 'Run the webpack Dev Server', + }, +]; + +const core = [ + { + name: 'entry', + usage: '--entry | --entry --entry ', + type: String, + multiple: true, + defaultOption: true, + group: BASIC_GROUP, + description: 'The entry point(s) of your application e.g. ./src/main.js', + link: 'https://webpack.js.org/concepts/#entry', + }, + { + name: 'config', + usage: '--config ', + alias: 'c', + type: String, + defaultValue: null, + group: CONFIG_GROUP, + description: 'Provide path to a webpack configuration file e.g. ./webpack.config.js', + link: 'https://webpack.js.org/configuration/', + }, + { + name: 'color', + usage: '--color', + type: Boolean, + group: DISPLAY_GROUP, + negative: true, + defaultValue: true, + description: 'Enable/Disable colors on console', + }, + { + name: 'merge', + usage: '--merge ', + alias: 'm', + type: String, + group: CONFIG_GROUP, + description: 'Merge a configuration file using webpack-merge e.g. ./webpack.config.js', + link: 'https://github.com/survivejs/webpack-merge', + }, + { + name: 'progress', + usage: '--progress', + type: Boolean, + group: BASIC_GROUP, + description: 'Print compilation progress during build', + }, + { + name: 'help', + usage: '--help', + type: Boolean, + group: HELP_GROUP, + description: 'Outputs list of supported flags', + }, + { + name: 'output', + usage: '--output ', + alias: 'o', + group: OUTPUT_GROUP, + type: String, + description: 'Output location of the file generated by webpack e.g. ./dist/', + link: 'https://webpack.js.org/concepts/#output', + }, + { + name: 'target', + usage: '--target ', + alias: 't', + type: String, + group: ADVANCED_GROUP, + description: 'Sets the build target e.g. node', + link: 'https://webpack.js.org/configuration/target/#target', + }, + { + name: 'watch', + usage: '--watch', + type: Boolean, + alias: 'w', + group: BASIC_GROUP, + description: 'Watch for files changes', + link: 'https://webpack.js.org/configuration/watch/', + }, + { + name: 'hot', + usage: '--hot', + alias: 'h', + type: Boolean, + negative: true, + group: ADVANCED_GROUP, + description: 'Enables Hot Module Replacement', + link: 'https://webpack.js.org/concepts/hot-module-replacement/', + }, + { + name: 'devtool', + usage: '--devtool ', + type: String, + alias: 'd', + defaultValue: undefined, + group: BASIC_GROUP, + description: 'Determine source maps to use', + link: 'https://webpack.js.org/configuration/devtool/#devtool', + }, + { + name: 'prefetch', + usage: '--prefetch ', + type: String, + group: ADVANCED_GROUP, + description: 'Prefetch this request', + link: 'https://webpack.js.org/plugins/prefetch-plugin/', + }, + { + name: 'json', + usage: '--json', + type: Boolean, + alias: 'j', + description: 'Prints result as JSON', + group: DISPLAY_GROUP, + }, + { + name: 'mode', + usage: '--mode ', + type: String, + group: ZERO_CONFIG_GROUP, + defaultValue: 'production', + description: 'Defines the mode to pass to webpack', + link: 'https://webpack.js.org/concepts/#mode', + }, + { + name: 'version', + usage: '--version | --version ', + alias: 'v', + type: Boolean, + group: HELP_GROUP, + description: 'Get current version', + }, + { + name: 'stats', + usage: '--stats ', + type: [String, Boolean], + group: DISPLAY_GROUP, + negative: true, + description: 'It instructs webpack on how to treat the stats e.g. verbose', + link: 'https://webpack.js.org/configuration/stats/#stats', + }, + { + name: 'env', + usage: '--env', + type: String, + multiple: true, + group: CONFIG_GROUP, + description: 'Environment passed to the configuration when it is a function', + }, + { + name: 'name', + usage: '--name', + type: String, + group: BASIC_GROUP, + description: 'Name of the configuration. Used when loading multiple configurations.', + }, + /* { + name: "analyze", + type: Boolean, + group: BASIC_GROUP, + description: "analyze build for performance improvements" + }, */ + /* { + name: "interactive", + type: Boolean, + alias: "i", + description: "Use webpack interactively", + group: BASIC_GROUP + } */ +]; + // Extract all the flags being exported from core. A list of cli flags generated by core // can be found here https://github.com/webpack/webpack/blob/master/test/__snapshots__/Cli.test.js.snap let flagsFromCore = @@ -33,255 +272,14 @@ let flagsFromCore = : []; // duplicate flags -const duplicateFlags = ['entry', 'mode', 'stats', 'watch', 'target', 'devtool', 'name']; +const duplicateFlags = core.map((flag) => flag.name); +// remove duplicate flags flagsFromCore = flagsFromCore.filter((flag) => !duplicateFlags.includes(flag.name)); module.exports = { - groups: { - HELP_GROUP, - CONFIG_GROUP, - BASIC_GROUP, - OUTPUT_GROUP, - ADVANCED_GROUP, - DISPLAY_GROUP, - ZERO_CONFIG_GROUP, - }, - commands: [ - { - name: 'init', - alias: 'c', - type: String, - usage: 'init [scaffold]', - description: 'Initialize a new webpack configuration', - }, - { - name: 'migrate', - alias: 'm', - type: String, - usage: 'migrate', - description: 'Migrate a configuration to a new version', - }, - { - name: 'loader', - scope: 'external', - alias: 'l', - type: String, - usage: 'loader', - description: 'Scaffold a loader repository', - }, - { - name: 'plugin', - alias: 'p', - scope: 'external', - type: String, - usage: 'plugin', - description: 'Scaffold a plugin repository', - }, - { - name: 'info', - scope: 'external', - alias: 'i', - type: String, - usage: 'info [options]', - description: 'Outputs information about your system and dependencies', - flags: [ - { - name: 'output', - type: String, - group: OUTPUT_GROUP, - description: 'To get the output in specified format ( accept json or markdown )', - }, - ], - }, - { - name: 'serve', - alias: 's', - scope: 'external', - type: String, - usage: 'serve', - description: 'Run the webpack Dev Server', - }, - ], - core: [ - { - name: 'entry', - usage: '--entry | --entry --entry ', - type: String, - multiple: true, - defaultOption: true, - group: BASIC_GROUP, - description: 'The entry point(s) of your application e.g. ./src/main.js', - link: 'https://webpack.js.org/concepts/#entry', - }, - { - name: 'config', - usage: '--config ', - alias: 'c', - type: String, - defaultValue: null, - group: CONFIG_GROUP, - description: 'Provide path to a webpack configuration file e.g. ./webpack.config.js', - link: 'https://webpack.js.org/configuration/', - }, - { - name: 'config-name', - usage: '--config-name ', - type: String, - multiple: true, - group: CONFIG_GROUP, - description: 'Name of the configuration to use', - }, - { - name: 'color', - usage: '--color', - type: Boolean, - group: DISPLAY_GROUP, - negative: true, - defaultValue: true, - description: 'Enable/Disable colors on console', - }, - { - name: 'merge', - usage: '--merge ', - alias: 'm', - type: String, - group: CONFIG_GROUP, - description: 'Merge a configuration file using webpack-merge e.g. ./webpack.config.js', - link: 'https://github.com/survivejs/webpack-merge', - }, - { - name: 'progress', - usage: '--progress', - type: Boolean, - group: BASIC_GROUP, - description: 'Print compilation progress during build', - }, - { - name: 'help', - usage: '--help', - type: Boolean, - group: HELP_GROUP, - description: 'Outputs list of supported flags', - }, - { - name: 'output', - usage: '--output ', - alias: 'o', - group: OUTPUT_GROUP, - type: String, - description: 'Output location of the file generated by webpack e.g. ./dist/', - link: 'https://webpack.js.org/concepts/#output', - }, - { - name: 'target', - usage: '--target ', - alias: 't', - type: String, - group: ADVANCED_GROUP, - description: 'Sets the build target e.g. node', - link: 'https://webpack.js.org/configuration/target/#target', - }, - { - name: 'watch', - usage: '--watch', - type: Boolean, - alias: 'w', - group: BASIC_GROUP, - description: 'Watch for files changes', - link: 'https://webpack.js.org/configuration/watch/', - }, - { - name: 'hot', - usage: '--hot', - alias: 'h', - type: Boolean, - negative: true, - group: ADVANCED_GROUP, - description: 'Enables Hot Module Replacement', - link: 'https://webpack.js.org/concepts/hot-module-replacement/', - }, - { - name: 'devtool', - usage: '--devtool ', - type: String, - alias: 'd', - defaultValue: undefined, - group: BASIC_GROUP, - description: 'Determine source maps to use', - link: 'https://webpack.js.org/configuration/devtool/#devtool', - }, - { - name: 'prefetch', - usage: '--prefetch ', - type: String, - group: ADVANCED_GROUP, - description: 'Prefetch this request', - link: 'https://webpack.js.org/plugins/prefetch-plugin/', - }, - { - name: 'json', - usage: '--json', - type: Boolean, - alias: 'j', - description: 'Prints result as JSON', - group: DISPLAY_GROUP, - }, - { - name: 'mode', - usage: '--mode ', - type: String, - group: ZERO_CONFIG_GROUP, - defaultValue: 'production', - description: 'Defines the mode to pass to webpack', - link: 'https://webpack.js.org/concepts/#mode', - }, - { - name: 'version', - usage: '--version | --version ', - alias: 'v', - type: Boolean, - group: HELP_GROUP, - description: 'Get current version', - }, - { - name: 'stats', - usage: '--stats ', - type: [String, Boolean], - group: DISPLAY_GROUP, - negative: true, - description: 'It instructs webpack on how to treat the stats e.g. verbose', - link: 'https://webpack.js.org/configuration/stats/#stats', - }, - { - name: 'env', - usage: '--env', - type: String, - multiple: true, - group: CONFIG_GROUP, - description: 'Environment passed to the configuration when it is a function', - }, - { - name: 'name', - usage: '--name', - type: String, - group: BASIC_GROUP, - description: 'Name of the configuration. Used when loading multiple configurations.', - }, - ...flagsFromCore, - /* { - name: "analyze", - type: Boolean, - group: BASIC_GROUP, - description: "analyze build for performance improvements" - }, */ - /* { - name: "interactive", - type: Boolean, - alias: "i", - description: "Use webpack interactively", - group: BASIC_GROUP - } */ - ], + groups, + commands, + core: [...core, ...flagsFromCore], flagsFromCore, }; From 3516ecf25d12293f53d943775839b4f9910f49a3 Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Thu, 20 Aug 2020 23:15:20 +0530 Subject: [PATCH 4/4] chore: re add config-name flag --- packages/webpack-cli/lib/utils/cli-flags.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/webpack-cli/lib/utils/cli-flags.js b/packages/webpack-cli/lib/utils/cli-flags.js index 9ad0ddaac30..e7b59cee01a 100644 --- a/packages/webpack-cli/lib/utils/cli-flags.js +++ b/packages/webpack-cli/lib/utils/cli-flags.js @@ -232,6 +232,14 @@ const core = [ group: BASIC_GROUP, description: 'Name of the configuration. Used when loading multiple configurations.', }, + { + name: 'config-name', + usage: '--config-name ', + type: String, + multiple: true, + group: CONFIG_GROUP, + description: 'Name of the configuration to use', + }, /* { name: "analyze", type: Boolean,