Skip to content

Commit

Permalink
cli(node): Add node flags to CLI (#377)
Browse files Browse the repository at this point in the history
* feat: add support for node flags

* tests: Fix node-flags test

* misc: Fix test failing due to not-found webpack-cli

* misc: remove comment

* misc: refactor removing unecessary args

* tests: add more tests to prevent argument collision
  • Loading branch information
matheus1lva authored and evenstensberg committed Apr 30, 2018
1 parent 106c092 commit 05204f8
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 2 deletions.
47 changes: 47 additions & 0 deletions bin/process-node-flags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const spawn = require("cross-spawn");
const path = require("path");
/**
* Lookup for prefixed arguments (node.)
* place them before webpack cli arguments and spawns
* a different process using these V8/Node flags.
* By Doing that, user has the power to provide any node options, e.g --max-old-space-size=1024
* and these are going to be correctly used.
*
* @param {array} argv - Arguments input by the user directly to CLI
* @returns {Void} void
*/
module.exports = function(argv) {
const args = [path.join(__dirname, "webpack.js")];

argv.slice(2).forEach((arg) => {
if (arg.includes("node.")) {
args.unshift(arg.replace("node.", ""));
} else {
args.push(arg);
}
});

const webpackCliProcess = spawn(process.execPath, args, {
stdio: "inherit",
});

webpackCliProcess.on("exit", (code, signal) => {
process.on("exit", () => {
if (signal) {
process.kill(process.pid, signal);
} else {
process.exit(code);
}
});
});

/**
* Terminate children
* just in case the current one is terminated.
*/
process.on("SIGINT", () => {
webpackCliProcess.kill("SIGINT");
webpackCliProcess.kill("SIGTERM");
});

};
11 changes: 9 additions & 2 deletions bin/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@
"info"
];

const nodeFlags = process.argv.filter((arg) => arg.includes("node."));

if (nodeFlags.length) {
require("./process-node-flags")(process.argv);
return;
}

const NON_COMPILATION_CMD = process.argv.find(arg => {
if (arg === "serve") {
global.process.argv = global.process.argv.filter(a => a !== "serve");
Expand Down Expand Up @@ -261,8 +268,8 @@ For more information, see https://webpack.js.org/api/cli/.`);
*/
const stdout = argv.silent
? {
write: () => {}
} // eslint-disable-line
write: () => { }
} // eslint-disable-line
: process.stdout;

function ifArg(name, fn, init) {
Expand Down
1 change: 1 addition & 0 deletions test/binCases/node-flags/advanced/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "index";
9 changes: 9 additions & 0 deletions test/binCases/node-flags/advanced/stdin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"use strict";

module.exports = function testAssertions(code, stdout, stderr) {
expect(code).toBe(0);
expect(stdout).toEqual(expect.anything());
expect(stdout[5]).toContain("main.js");
expect(stdout[7]).toMatch(/index\.js.*\{0\}/);
expect(stderr).toHaveLength(0);
};
11 changes: 11 additions & 0 deletions test/binCases/node-flags/advanced/test.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--node.max_old_space_size=1024
--node.no-warnings
--node.turbo_jt
--display-used-exports
--display-entrypoints
--profile
--bail
--labeled-modules
--optimize-minimize
--mode production
./index.js
1 change: 1 addition & 0 deletions test/binCases/node-flags/only-node-flags/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "index";
9 changes: 9 additions & 0 deletions test/binCases/node-flags/only-node-flags/stdin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"use strict";

module.exports = function testAssertions(code, stdout, stderr) {
expect(code).toBe(0);
expect(stdout).toEqual(expect.anything());
expect(stdout[5]).toContain("main.js");
expect(stdout[7]).toMatch(/index\.js.*\{0\}/);
expect(stderr).toHaveLength(0);
};
3 changes: 3 additions & 0 deletions test/binCases/node-flags/only-node-flags/test.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--node.no-deprecation
--node.no-warnings
./index.js
1 change: 1 addition & 0 deletions test/binCases/node-flags/output/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "index";
9 changes: 9 additions & 0 deletions test/binCases/node-flags/output/stdin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"use strict";

module.exports = function testAssertions(code, stdout, stderr) {
expect(code).toBe(0);
expect(stdout).toEqual(expect.anything());
expect(stdout[5]).toContain("main.js");
expect(stdout[7]).toMatch(/index\.js.*\{0\}/);
expect(stderr).toHaveLength(0);
};
6 changes: 6 additions & 0 deletions test/binCases/node-flags/output/test.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
--node.no-deprecation
--node.no-warnings
--display-used-exports
--display-entrypoints
--mode production
./index.js

0 comments on commit 05204f8

Please sign in to comment.