From 0bf9a2e0d9fa648166fd6c2ab8e88d1d605287aa Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Sat, 8 Aug 2020 18:42:27 +0530 Subject: [PATCH 1/9] fix: remove eeca --- packages/webpack-cli/bin/cli.js | 10 +++++++++- packages/webpack-cli/lib/bootstrap.js | 2 ++ packages/webpack-cli/lib/runner.js | 2 ++ test/node/bin/main.bundle.js | 1 + 4 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 test/node/bin/main.bundle.js diff --git a/packages/webpack-cli/bin/cli.js b/packages/webpack-cli/bin/cli.js index 34faad08445..e9afaea4007 100755 --- a/packages/webpack-cli/bin/cli.js +++ b/packages/webpack-cli/bin/cli.js @@ -5,6 +5,7 @@ require('v8-compile-cache'); const importLocal = require('import-local'); const parseNodeArgs = require('../lib/utils/parse-node-args'); const runner = require('../lib/runner'); +const { performance } = require('perf_hooks'); // Prefer the local installation of webpack-cli if (importLocal(__filename)) { @@ -15,4 +16,11 @@ process.title = 'webpack'; const [, , ...rawArgs] = process.argv; const { cliArgs, nodeArgs } = parseNodeArgs(rawArgs); -runner(nodeArgs, cliArgs); +const x = performance.now(); +process.execArgv.push(...nodeArgs); +console.log({ nodeArgs }); +// runner(nodeArgs, cliArgs); +require('../lib/bootstrap'); +console.log(process); + +console.log(performance.now() - x, 'time takes'); diff --git a/packages/webpack-cli/lib/bootstrap.js b/packages/webpack-cli/lib/bootstrap.js index b7bbc6ac2d4..619e3d2d13d 100644 --- a/packages/webpack-cli/lib/bootstrap.js +++ b/packages/webpack-cli/lib/bootstrap.js @@ -7,6 +7,8 @@ const argParser = require('./utils/arg-parser'); require('./utils/process-log'); process.title = 'webpack-cli'; +console.log(process); + const isCommandUsed = (commands) => commands.find((cmd) => { return process.argv.includes(cmd.name) || process.argv.includes(cmd.alias); diff --git a/packages/webpack-cli/lib/runner.js b/packages/webpack-cli/lib/runner.js index 50295954e04..f282d81b4dc 100644 --- a/packages/webpack-cli/lib/runner.js +++ b/packages/webpack-cli/lib/runner.js @@ -2,6 +2,8 @@ const execa = require('execa'); const cliPath = require.resolve('./bootstrap.js'); function runner(nodeArgs, cliArgs) { + console.log({ nodeArgs, cliPath, cliArgs }); + console.log(['node', ...nodeArgs, cliPath, ...cliArgs].join(' ')); execa('node', [...nodeArgs, cliPath, ...cliArgs], { stdio: 'inherit' }).catch((e) => { process.exit(e.exitCode); }); diff --git a/test/node/bin/main.bundle.js b/test/node/bin/main.bundle.js new file mode 100644 index 00000000000..6970480f6e2 --- /dev/null +++ b/test/node/bin/main.bundle.js @@ -0,0 +1 @@ +!function(e){var t={};function r(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)r.d(n,o,function(t){return e[t]}.bind(null,o));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=0)}([function(e,t){e.exports="a.js"}]); \ No newline at end of file From 9bf58c04da7c27f1d296a432177a5d32ebe898ae Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Mon, 10 Aug 2020 10:26:43 +0530 Subject: [PATCH 2/9] fix: rm execa --- .../__tests__/cli-executer.test.js | 12 ++++---- packages/webpack-cli/bin/cli.js | 15 ++++------ packages/webpack-cli/lib/bootstrap.js | 29 ++++++++++--------- packages/webpack-cli/lib/runner.js | 12 -------- .../webpack-cli/lib/utils/cli-executer.js | 4 +-- 5 files changed, 28 insertions(+), 44 deletions(-) delete mode 100644 packages/webpack-cli/lib/runner.js diff --git a/packages/webpack-cli/__tests__/cli-executer.test.js b/packages/webpack-cli/__tests__/cli-executer.test.js index fdd12f9de71..3510fab08e7 100644 --- a/packages/webpack-cli/__tests__/cli-executer.test.js +++ b/packages/webpack-cli/__tests__/cli-executer.test.js @@ -1,8 +1,8 @@ -jest.mock('../lib/runner'); +jest.mock('../lib/bootstrap'); jest.mock('enquirer'); -const runner = require('../lib/runner'); -runner.mockImplementation(() => {}); +const runCLI = require('../lib/bootstrap'); +runCLI.mockImplementation(() => {}); describe('CLI Executer', () => { let cliExecuter = null; @@ -47,9 +47,9 @@ describe('CLI Executer', () => { it('runs enquirer options then runs webpack', async () => { await cliExecuter(); - // ensure that the webpack runner is called - expect(runner.mock.calls.length).toEqual(1); - expect(runner.mock.calls[0]).toEqual([[], ['--config', 'test1', '--entry', 'test2', '--progress']]); + // ensure that the webpack runCLI is called + expect(runCLI.mock.calls.length).toEqual(1); + expect(runCLI.mock.calls[0]).toEqual([[], ['--config', 'test1', '--entry', 'test2', '--progress']]); // check that webpack options are actually being displayed that // the user can select from diff --git a/packages/webpack-cli/bin/cli.js b/packages/webpack-cli/bin/cli.js index e9afaea4007..0e750fd1400 100755 --- a/packages/webpack-cli/bin/cli.js +++ b/packages/webpack-cli/bin/cli.js @@ -4,8 +4,7 @@ require('v8-compile-cache'); const importLocal = require('import-local'); const parseNodeArgs = require('../lib/utils/parse-node-args'); -const runner = require('../lib/runner'); -const { performance } = require('perf_hooks'); +const runCLI = require('../lib/bootstrap'); // Prefer the local installation of webpack-cli if (importLocal(__filename)) { @@ -14,13 +13,9 @@ if (importLocal(__filename)) { process.title = 'webpack'; const [, , ...rawArgs] = process.argv; -const { cliArgs, nodeArgs } = parseNodeArgs(rawArgs); -const x = performance.now(); -process.execArgv.push(...nodeArgs); -console.log({ nodeArgs }); -// runner(nodeArgs, cliArgs); -require('../lib/bootstrap'); -console.log(process); +// figure out how to inject node args at runtime +// eslint-disable-next-line no-unused-vars +const { cliArgs, nodeArgs } = parseNodeArgs(rawArgs); -console.log(performance.now() - x, 'time takes'); +runCLI(cliArgs); diff --git a/packages/webpack-cli/lib/bootstrap.js b/packages/webpack-cli/lib/bootstrap.js index 619e3d2d13d..e146fde0881 100644 --- a/packages/webpack-cli/lib/bootstrap.js +++ b/packages/webpack-cli/lib/bootstrap.js @@ -7,22 +7,25 @@ const argParser = require('./utils/arg-parser'); require('./utils/process-log'); process.title = 'webpack-cli'; -console.log(process); +// Create a new instance of the CLI object +const cli = new WebpackCLI(); -const isCommandUsed = (commands) => +const isCommandUsed = (args) => commands.find((cmd) => { - return process.argv.includes(cmd.name) || process.argv.includes(cmd.alias); + return args.includes(cmd.name) || args.includes(cmd.alias); }); -async function runCLI(cli, commandIsUsed) { +async function runCLI(cliArgs) { let args; + + const commandIsUsed = isCommandUsed(cliArgs); const runVersion = () => { - cli.runVersion(process.argv, commandIsUsed); + cli.runVersion(cliArgs, commandIsUsed); }; - const parsedArgs = argParser(core, process.argv, false, process.title, cli.runHelp, runVersion, commands); + const parsedArgs = argParser(core, cliArgs, false, process.title, cli.runHelp, runVersion, commands); if (parsedArgs.unknownArgs.includes('help')) { - cli.runHelp(process.argv); + cli.runHelp(cliArgs); process.exit(0); } @@ -79,10 +82,10 @@ async function runCLI(cli, commandIsUsed) { } else if (err.name === 'ALREADY_SET') { const argsMap = {}; const keysToDelete = []; - process.argv.forEach((arg, idx) => { + cliArgs.forEach((arg, idx) => { const oldMapValue = argsMap[arg]; argsMap[arg] = { - value: process.argv[idx], + value: cliArgs[idx], pos: idx, }; // Swap idx of overridden value @@ -94,8 +97,8 @@ async function runCLI(cli, commandIsUsed) { // Filter out the value for the overridden key const newArgKeys = Object.keys(argsMap).filter((arg) => !keysToDelete.includes(argsMap[arg].pos)); // eslint-disable-next-line require-atomic-updates - process.argv = newArgKeys; - args = argParser('', core, process.argv); + cliArgs = newArgKeys; + args = argParser('', core, cliArgs); await cli.run(args.opts, core); process.stdout.write('\n'); logger.warn('Duplicate flags found, defaulting to last set value'); @@ -106,6 +109,4 @@ async function runCLI(cli, commandIsUsed) { } } -const commandIsUsed = isCommandUsed(commands); -const cli = new WebpackCLI(); -runCLI(cli, commandIsUsed); +module.exports = runCLI; diff --git a/packages/webpack-cli/lib/runner.js b/packages/webpack-cli/lib/runner.js deleted file mode 100644 index f282d81b4dc..00000000000 --- a/packages/webpack-cli/lib/runner.js +++ /dev/null @@ -1,12 +0,0 @@ -const execa = require('execa'); -const cliPath = require.resolve('./bootstrap.js'); - -function runner(nodeArgs, cliArgs) { - console.log({ nodeArgs, cliPath, cliArgs }); - console.log(['node', ...nodeArgs, cliPath, ...cliArgs].join(' ')); - execa('node', [...nodeArgs, cliPath, ...cliArgs], { stdio: 'inherit' }).catch((e) => { - process.exit(e.exitCode); - }); -} - -module.exports = runner; diff --git a/packages/webpack-cli/lib/utils/cli-executer.js b/packages/webpack-cli/lib/utils/cli-executer.js index bddb21d8cdb..2634a0ea130 100644 --- a/packages/webpack-cli/lib/utils/cli-executer.js +++ b/packages/webpack-cli/lib/utils/cli-executer.js @@ -1,8 +1,8 @@ const { MultiSelect, Input } = require('enquirer'); const { cyan } = require('colorette'); -const runner = require('../runner'); const logger = require('./logger'); const cliArgs = require('./cli-flags').core; +const runCLI = require('../bootstrap'); async function prompter() { const args = []; @@ -56,7 +56,7 @@ async function run() { const args = await prompter(); process.stdout.write('\n'); logger.info('Executing CLI\n'); - runner([], args); + runCLI(args); } catch (err) { logger.error(`Action Interrupted, use ${cyan('webpack-cli help')} to see possible options.`); } From 8ca5dc85f839fa4fd2b4791eb5243f86c6603222 Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Mon, 10 Aug 2020 17:56:42 +0530 Subject: [PATCH 3/9] fix: rm execa --- test/node/bin/main.bundle.js | 1 - 1 file changed, 1 deletion(-) delete mode 100644 test/node/bin/main.bundle.js diff --git a/test/node/bin/main.bundle.js b/test/node/bin/main.bundle.js deleted file mode 100644 index 6970480f6e2..00000000000 --- a/test/node/bin/main.bundle.js +++ /dev/null @@ -1 +0,0 @@ -!function(e){var t={};function r(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)r.d(n,o,function(t){return e[t]}.bind(null,o));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=0)}([function(e,t){e.exports="a.js"}]); \ No newline at end of file From ac10533d1f0654a1a0f4e0ebe23ec92d1286f996 Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Tue, 11 Aug 2020 21:34:57 +0530 Subject: [PATCH 4/9] fix: supply args only --- packages/webpack-cli/lib/bootstrap.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/webpack-cli/lib/bootstrap.js b/packages/webpack-cli/lib/bootstrap.js index e146fde0881..349d17f727c 100644 --- a/packages/webpack-cli/lib/bootstrap.js +++ b/packages/webpack-cli/lib/bootstrap.js @@ -22,7 +22,7 @@ async function runCLI(cliArgs) { const runVersion = () => { cli.runVersion(cliArgs, commandIsUsed); }; - const parsedArgs = argParser(core, cliArgs, false, process.title, cli.runHelp, runVersion, commands); + const parsedArgs = argParser(core, cliArgs, true, process.title, cli.runHelp, runVersion, commands); if (parsedArgs.unknownArgs.includes('help')) { cli.runHelp(cliArgs); From 009bbcef39a7065f8f9fd18aff7f77c4ec4b070b Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Tue, 11 Aug 2020 21:59:35 +0530 Subject: [PATCH 5/9] fix: do not slice args in help and version --- packages/webpack-cli/lib/webpack-cli.js | 4 ++-- test/node/node.test.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/webpack-cli/lib/webpack-cli.js b/packages/webpack-cli/lib/webpack-cli.js index aab0e10f6ab..a6f6909acf9 100644 --- a/packages/webpack-cli/lib/webpack-cli.js +++ b/packages/webpack-cli/lib/webpack-cli.js @@ -281,7 +281,7 @@ class WebpackCLI extends GroupHelper { const subject = allNames.filter((name) => { return args.includes(name); })[0]; - const invalidArgs = hasUnknownArgs(args.slice(2), ...allNames); + const invalidArgs = hasUnknownArgs(args, ...allNames); const isCommand = commands.includes(subject); options.enabled = !args.includes('--no-color'); return new HelpGroup().outputHelp(isCommand, subject, invalidArgs); @@ -291,7 +291,7 @@ class WebpackCLI extends GroupHelper { const HelpGroup = require('./groups/HelpGroup'); const { commands, allNames, hasUnknownArgs } = require('./utils/unknown-args'); const commandsUsed = args.filter((val) => commands.includes(val)); - const invalidArgs = hasUnknownArgs(args.slice(2), ...allNames); + const invalidArgs = hasUnknownArgs(args, ...allNames); options.enabled = !args.includes('--no-color'); return new HelpGroup().outputVersion(externalPkg, commandsUsed, invalidArgs); } diff --git a/test/node/node.test.js b/test/node/node.test.js index a449f4eee15..026fb326c2c 100644 --- a/test/node/node.test.js +++ b/test/node/node.test.js @@ -4,7 +4,7 @@ const { resolve } = require('path'); const { run } = require('../utils/test-utils'); const parseNodeArgs = require('../../packages/webpack-cli/lib/utils/parse-node-args'); -describe('node flags', () => { +describe.skip('node flags', () => { it('parseNodeArgs helper must work correctly', () => { [ { From 90d350228452b379c23ec2b851d500909b5df015 Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Tue, 11 Aug 2020 22:39:01 +0530 Subject: [PATCH 6/9] fix: spawn new process for executor --- packages/webpack-cli/__tests__/cli-executer.test.js | 10 +++++----- packages/webpack-cli/lib/bootstrap.js | 2 +- packages/webpack-cli/lib/runner.js | 10 ++++++++++ packages/webpack-cli/lib/utils/cli-executer.js | 4 ++-- 4 files changed, 18 insertions(+), 8 deletions(-) create mode 100644 packages/webpack-cli/lib/runner.js diff --git a/packages/webpack-cli/__tests__/cli-executer.test.js b/packages/webpack-cli/__tests__/cli-executer.test.js index 3510fab08e7..a67b63bc293 100644 --- a/packages/webpack-cli/__tests__/cli-executer.test.js +++ b/packages/webpack-cli/__tests__/cli-executer.test.js @@ -1,8 +1,8 @@ -jest.mock('../lib/bootstrap'); +jest.mock('../lib/runner'); jest.mock('enquirer'); -const runCLI = require('../lib/bootstrap'); -runCLI.mockImplementation(() => {}); +const runner = require('../lib/runner'); +runner.mockImplementation(() => {}); describe('CLI Executer', () => { let cliExecuter = null; @@ -48,8 +48,8 @@ describe('CLI Executer', () => { await cliExecuter(); // ensure that the webpack runCLI is called - expect(runCLI.mock.calls.length).toEqual(1); - expect(runCLI.mock.calls[0]).toEqual([[], ['--config', 'test1', '--entry', 'test2', '--progress']]); + expect(runner.mock.calls.length).toEqual(1); + expect(runner.mock.calls[0]).toEqual([[], ['--config', 'test1', '--entry', 'test2', '--progress']]); // check that webpack options are actually being displayed that // the user can select from diff --git a/packages/webpack-cli/lib/bootstrap.js b/packages/webpack-cli/lib/bootstrap.js index 349d17f727c..b95fb54c629 100644 --- a/packages/webpack-cli/lib/bootstrap.js +++ b/packages/webpack-cli/lib/bootstrap.js @@ -60,7 +60,7 @@ async function runCLI(cliArgs) { parsedArgs.unknownArgs.forEach((unknown) => { logger.warn(`Unknown argument: ${unknown}`); }); - cliExecuter(); + await cliExecuter(); return; } const parsedArgsOpts = parsedArgs.opts; diff --git a/packages/webpack-cli/lib/runner.js b/packages/webpack-cli/lib/runner.js new file mode 100644 index 00000000000..50295954e04 --- /dev/null +++ b/packages/webpack-cli/lib/runner.js @@ -0,0 +1,10 @@ +const execa = require('execa'); +const cliPath = require.resolve('./bootstrap.js'); + +function runner(nodeArgs, cliArgs) { + execa('node', [...nodeArgs, cliPath, ...cliArgs], { stdio: 'inherit' }).catch((e) => { + process.exit(e.exitCode); + }); +} + +module.exports = runner; diff --git a/packages/webpack-cli/lib/utils/cli-executer.js b/packages/webpack-cli/lib/utils/cli-executer.js index 2634a0ea130..fbb2ef6895c 100644 --- a/packages/webpack-cli/lib/utils/cli-executer.js +++ b/packages/webpack-cli/lib/utils/cli-executer.js @@ -2,7 +2,7 @@ const { MultiSelect, Input } = require('enquirer'); const { cyan } = require('colorette'); const logger = require('./logger'); const cliArgs = require('./cli-flags').core; -const runCLI = require('../bootstrap'); +const runner = require('../runner'); async function prompter() { const args = []; @@ -56,7 +56,7 @@ async function run() { const args = await prompter(); process.stdout.write('\n'); logger.info('Executing CLI\n'); - runCLI(args); + await runner([], args); } catch (err) { logger.error(`Action Interrupted, use ${cyan('webpack-cli help')} to see possible options.`); } From 3c9bd1b888c6ee089e160f2451f39bd3d7e16f5c Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Thu, 13 Aug 2020 21:00:37 +0530 Subject: [PATCH 7/9] fix: remove node-args, fix tests, remove stale helpers --- packages/webpack-cli/bin/cli.js | 7 +- .../webpack-cli/lib/utils/parse-node-args.js | 27 -------- test/node/node.test.js | 69 ++++--------------- test/utils/test-utils.js | 10 +-- 4 files changed, 21 insertions(+), 92 deletions(-) delete mode 100644 packages/webpack-cli/lib/utils/parse-node-args.js diff --git a/packages/webpack-cli/bin/cli.js b/packages/webpack-cli/bin/cli.js index 0e750fd1400..960fc1fad39 100755 --- a/packages/webpack-cli/bin/cli.js +++ b/packages/webpack-cli/bin/cli.js @@ -3,7 +3,6 @@ 'use strict'; require('v8-compile-cache'); const importLocal = require('import-local'); -const parseNodeArgs = require('../lib/utils/parse-node-args'); const runCLI = require('../lib/bootstrap'); // Prefer the local installation of webpack-cli @@ -14,8 +13,4 @@ process.title = 'webpack'; const [, , ...rawArgs] = process.argv; -// figure out how to inject node args at runtime -// eslint-disable-next-line no-unused-vars -const { cliArgs, nodeArgs } = parseNodeArgs(rawArgs); - -runCLI(cliArgs); +runCLI(rawArgs); diff --git a/packages/webpack-cli/lib/utils/parse-node-args.js b/packages/webpack-cli/lib/utils/parse-node-args.js deleted file mode 100644 index 9ea49b8d6a9..00000000000 --- a/packages/webpack-cli/lib/utils/parse-node-args.js +++ /dev/null @@ -1,27 +0,0 @@ -/** - * Parse cli args and split these to args for node js and the rest - * - * @param {string[]} rawArgs raw cli args - * @returns {{cliArgs: string[], nodeArgs: string[]}} cli and nodejs args - */ -module.exports = (rawArgs) => { - const cliArgs = []; - const nodeArgs = []; - let isNodeArg = false; - - for (const value of rawArgs) { - if (value === '--node-args') { - isNodeArg = true; - } else if (value.startsWith('--node-args=')) { - const [, argValue] = value.match(/^--node-args="?(.+?)"?$/); - nodeArgs.push(argValue); - } else if (isNodeArg) { - isNodeArg = false; - nodeArgs.push(...value.split(' ')); - } else { - cliArgs.push(value); - } - } - - return { cliArgs, nodeArgs }; -}; diff --git a/test/node/node.test.js b/test/node/node.test.js index 026fb326c2c..ac76a0e7a53 100644 --- a/test/node/node.test.js +++ b/test/node/node.test.js @@ -2,57 +2,16 @@ const { stat } = require('fs'); const { resolve } = require('path'); const { run } = require('../utils/test-utils'); -const parseNodeArgs = require('../../packages/webpack-cli/lib/utils/parse-node-args'); -describe.skip('node flags', () => { - it('parseNodeArgs helper must work correctly', () => { - [ - { - rawArgs: ['--foo', '--bar', '--baz=quux'], - expectedCliArgs: ['--foo', '--bar', '--baz=quux'], - expectedNodeArgs: [], - }, - { - rawArgs: ['--foo', '--bar', '--baz=quux', '--node-args', '--name1=value1', '--node-args', '--name2 value2'], - expectedCliArgs: ['--foo', '--bar', '--baz=quux'], - expectedNodeArgs: ['--name1=value1', '--name2', 'value2'], - }, - { - rawArgs: [ - '--node-args', - '--name1=value1', - '--node-args', - '--name2="value2"', - '--node-args', - '--name3 value3', - '--node-args', - '-k v', - ], - expectedCliArgs: [], - expectedNodeArgs: ['--name1=value1', '--name2="value2"', '--name3', 'value3', '-k', 'v'], - }, - ].map(({ rawArgs, expectedNodeArgs, expectedCliArgs }) => { - const { nodeArgs, cliArgs } = parseNodeArgs(rawArgs); - expect(nodeArgs).toEqual(expectedNodeArgs); - expect(cliArgs).toEqual(expectedCliArgs); - }); - }); - - it('is able to pass the options flags to node js', (done) => { - const { stdout } = run( - __dirname, - [ - '--node-args', - `--require=${resolve(__dirname, 'bootstrap.js')}`, - '--node-args', - `-r ${resolve(__dirname, 'bootstrap2.js')}`, - '--output', - './bin/[name].bundle.js', - ], - false, - ); +describe('node flags', () => { + it('is able to pass the options flags to node js', async (done) => { + const { stdout, stderr } = await run(__dirname, ['--output', './bin/[name].bundle.js'], false, [ + `--require=${resolve(__dirname, 'bootstrap.js')}`, + `--require=${resolve(__dirname, 'bootstrap2.js')}`, + ]); expect(stdout).toContain('---from bootstrap.js---'); expect(stdout).toContain('---from bootstrap2.js---'); + expect(stderr).toBeFalsy(); stat(resolve(__dirname, './bin/main.bundle.js'), (err, stats) => { expect(err).toBe(null); expect(stats.isFile()).toBe(true); @@ -60,20 +19,20 @@ describe.skip('node flags', () => { }); }); - it('throws an error on supplying unknown flags', () => { - const { stderr } = run(__dirname, ['--node-args', '--unknown']); + it('throws an error on supplying unknown flags', async () => { + const { stderr } = await run(__dirname, [], false, ['--unknown']); expect(stderr).toContain('bad option'); }); - it('throws an error if no values were supplied with --max-old-space-size', () => { - const { stderr, stdout } = run(__dirname, ['--node-args', '--max-old-space-size']); + it('throws an error if no values were supplied with --max-old-space-size', async () => { + const { stderr, stdout } = await run(__dirname, [], false, ['--max-old-space-size']); expect(stderr).toContain('missing value for flag --max-old-space-size'); expect(stdout).toBeFalsy(); }); - it('throws an error if an illegal value was supplied with --max-old-space-size', () => { - const { stderr, stdout } = run(__dirname, ['--node-args', '--max-old-space-size=1024a']); - expect(stderr).toContain('illegal value for flag --max-old-space-size'); + it('throws an error if an illegal value was supplied with --max-old-space-size', async () => { + const { stderr, stdout } = await run(__dirname, [], true, ['--max_old_space_size=1024a']); + expect(stderr).toContain('Error: illegal value for flag --max_old_space_size=1024a of type size_t'); expect(stdout).toBeFalsy(); }); }); diff --git a/test/utils/test-utils.js b/test/utils/test-utils.js index 80faa787a27..8d96d872333 100644 --- a/test/utils/test-utils.js +++ b/test/utils/test-utils.js @@ -2,7 +2,7 @@ const path = require('path'); const fs = require('fs'); const execa = require('execa'); -const { sync: spawnSync } = execa; +const { sync: spawnSync, node: execaNode } = execa; const { Writable } = require('readable-stream'); const concat = require('concat-stream'); @@ -15,16 +15,18 @@ const ENABLE_LOG_COMPILATION = process.env.ENABLE_PIPE || false; * @param {String} testCase The path to folder that contains the webpack.config.js * @param {Array} args Array of arguments to pass to webpack * @param {Boolean} setOutput Boolean that decides if a default output path will be set or not - * @returns {Object} The webpack output + * @returns {Object} The webpack output or Promise when nodeOptions are present */ -function run(testCase, args = [], setOutput = true) { +function run(testCase, args = [], setOutput = true, nodeArgs = []) { const cwd = path.resolve(testCase); const outputPath = path.resolve(testCase, 'bin'); + const processExecutor = nodeArgs.length ? execaNode : spawnSync; const argsWithOutput = setOutput ? args.concat('--output', outputPath) : args; - const result = spawnSync(WEBPACK_PATH, argsWithOutput, { + const result = processExecutor(WEBPACK_PATH, argsWithOutput, { cwd, reject: false, + nodeOptions: nodeArgs, stdio: ENABLE_LOG_COMPILATION ? 'inherit' : 'pipe', }); From 8ae3642eb905a6ed97e7126cf659a21743a35480 Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Thu, 13 Aug 2020 21:11:49 +0530 Subject: [PATCH 8/9] chore: cleanup node-args references --- packages/webpack-cli/README.md | 1 - packages/webpack-cli/lib/utils/cli-flags.js | 8 -------- 2 files changed, 9 deletions(-) diff --git a/packages/webpack-cli/README.md b/packages/webpack-cli/README.md index 673d751e578..4751e251fec 100644 --- a/packages/webpack-cli/README.md +++ b/packages/webpack-cli/README.md @@ -54,7 +54,6 @@ yarn add webpack-cli --dev -j, --json Prints result as JSON --mode string Defines the mode to pass to webpack -v, --version Get current version - --node-args string[] NodeJS flags --stats string It instructs webpack on how to treat the stats --no-stats Disables stats output ``` diff --git a/packages/webpack-cli/lib/utils/cli-flags.js b/packages/webpack-cli/lib/utils/cli-flags.js index 7477a0b5da7..b11a1debb08 100644 --- a/packages/webpack-cli/lib/utils/cli-flags.js +++ b/packages/webpack-cli/lib/utils/cli-flags.js @@ -236,14 +236,6 @@ module.exports = { group: HELP_GROUP, description: 'Get current version', }, - { - name: 'node-args', - usage: '--node-args "--max-old-space-size=1024"', - type: String, - multiple: true, - group: BASIC_GROUP, - description: 'NodeJS flags', - }, { name: 'stats', usage: '--stats ', From 786932d09abb23e7c1183409e286cd4569bee441 Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Thu, 13 Aug 2020 23:36:08 +0530 Subject: [PATCH 9/9] chore: add investigation comment --- test/node/node.test.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/node/node.test.js b/test/node/node.test.js index ac76a0e7a53..63114299a8e 100644 --- a/test/node/node.test.js +++ b/test/node/node.test.js @@ -3,6 +3,9 @@ const { stat } = require('fs'); const { resolve } = require('path'); const { run } = require('../utils/test-utils'); +// TODO - We pass node args to `nodeOptions` in execa, +// passing via NODE_OPTIONS= in env in execa +// throws different error from what we manually see describe('node flags', () => { it('is able to pass the options flags to node js', async (done) => { const { stdout, stderr } = await run(__dirname, ['--output', './bin/[name].bundle.js'], false, [