Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli(node): Add node flags to CLI #377

Merged
merged 6 commits into from
Apr 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why there's a return here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same reason as used here. If we don't exit, yargs will warn saying these flags are unknown and the normal cli flow will follow, with this return, the other file can strip those flags, spawn another webpack-cli process and work as intended.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests added. Let me know if they aren't enough. I've tried to add as many flags as possible.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay great, thanks!

}

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