From 1724ddb9067d5c5ba2654d4e5473ee9de5610825 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Thu, 17 Sep 2020 19:26:39 +0530 Subject: [PATCH] feat: allow multiple targets (#1799) * feat: allow multiple targets Co-authored-by: Anshuman Verma Co-authored-by: Rishabh Chawla --- .../__snapshots__/parseArgs.test.ts.snap | 3 +-- packages/serve/__tests__/parseArgs.test.ts | 2 +- packages/webpack-cli/lib/utils/cli-flags.js | 3 ++- test/target/flag-test/target-flag.test.js | 20 +++++++++++++++++-- 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/packages/serve/__tests__/__snapshots__/parseArgs.test.ts.snap b/packages/serve/__tests__/__snapshots__/parseArgs.test.ts.snap index a3a3f3ad9a2..e885c4c568c 100644 --- a/packages/serve/__tests__/__snapshots__/parseArgs.test.ts.snap +++ b/packages/serve/__tests__/__snapshots__/parseArgs.test.ts.snap @@ -42,8 +42,7 @@ Object { "webpackArgs": Object { "color": true, "config": Array [], - "mode": "production", - "target": "node", + "mode": "development", }, } `; diff --git a/packages/serve/__tests__/parseArgs.test.ts b/packages/serve/__tests__/parseArgs.test.ts index 976d2bfd81d..742d56b0ad3 100644 --- a/packages/serve/__tests__/parseArgs.test.ts +++ b/packages/serve/__tests__/parseArgs.test.ts @@ -27,7 +27,7 @@ describe('parseArgs', () => { }); it('parses webpack and dev server args', () => { - const args = parseArgs(cli, ['--bonjour', '--target=node', '--port', '8080']); + const args = parseArgs(cli, ['--bonjour', '--mode=development', '--port', '8080']); expect(args).toMatchSnapshot(); expect(errorMock.mock.calls.length).toEqual(0); expect(processExitSpy.mock.calls.length).toEqual(0); diff --git a/packages/webpack-cli/lib/utils/cli-flags.js b/packages/webpack-cli/lib/utils/cli-flags.js index c336e1f2728..b82eca34b01 100644 --- a/packages/webpack-cli/lib/utils/cli-flags.js +++ b/packages/webpack-cli/lib/utils/cli-flags.js @@ -144,6 +144,7 @@ const core = [ alias: 't', type: String, group: ADVANCED_GROUP, + multiple: cli !== undefined, description: 'Sets the build target e.g. node', link: 'https://webpack.js.org/configuration/target/#target', }, @@ -259,7 +260,7 @@ const core = [ // 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 = - typeof cli !== 'undefined' + cli !== undefined ? Object.entries(cli.getArguments()).map(([flag, meta]) => { if (meta.simpleType === 'string') { meta.type = String; diff --git a/test/target/flag-test/target-flag.test.js b/test/target/flag-test/target-flag.test.js index 899bf401f98..2e85a918786 100644 --- a/test/target/flag-test/target-flag.test.js +++ b/test/target/flag-test/target-flag.test.js @@ -10,7 +10,11 @@ describe('--target flag', () => { it(`should accept ${val} with --target flag`, (done) => { const { stdout, stderr } = run(__dirname, ['--target', `${val}`]); expect(stderr).toBeFalsy(); - expect(stdout).toContain(`target: '${val}'`); + if (isWebpack5) { + expect(stdout).toContain(`target: [ '${val}' ]`); + } else { + expect(stdout).toContain(`target: '${val}'`); + } stat(resolve(__dirname, 'bin/main.js'), (err, stats) => { expect(err).toBe(null); @@ -22,7 +26,11 @@ describe('--target flag', () => { it(`should accept ${val} with -t alias`, (done) => { const { stdout, stderr } = run(__dirname, ['-t', `${val}`]); expect(stderr).toBeFalsy(); - expect(stdout).toContain(`target: '${val}'`); + if (isWebpack5) { + expect(stdout).toContain(`target: [ '${val}' ]`); + } else { + expect(stdout).toContain(`target: '${val}'`); + } stat(resolve(__dirname, 'bin/main.js'), (err, stats) => { expect(err).toBe(null); @@ -40,4 +48,12 @@ describe('--target flag', () => { expect(stderr).toContain('Invalid configuration object'); } }); + + if (isWebpack5) { + it('should allow multiple targets', () => { + const { stderr, stdout } = run(__dirname, ['--target', 'node', '--target', 'async-node']); + expect(stderr).toBeFalsy(); + expect(stdout).toContain(`target: [ 'node', 'async-node' ]`); + }); + } });