Skip to content

Commit

Permalink
Merge pull request #6398 from addaleax/no-binding
Browse files Browse the repository at this point in the history
Avoid relying on Node’s internals
  • Loading branch information
sokra committed Feb 1, 2018
2 parents c7cbc35 + 8da8b93 commit 9323ee6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
10 changes: 10 additions & 0 deletions lib/EnvironmentPlugin.js
Expand Up @@ -7,6 +7,9 @@

const DefinePlugin = require("./DefinePlugin");

const needsEnvVarFix = ["8", "9"].indexOf(process.versions.node.split(".")[0]) >= 0 &&
process.platform === "win32";

class EnvironmentPlugin {
constructor(keys) {
if(Array.isArray(keys)) {
Expand All @@ -23,6 +26,13 @@ class EnvironmentPlugin {

apply(compiler) {
const definitions = this.keys.reduce((defs, key) => {
// TODO remove once the fix has made its way into Node 8.
// Work around https://github.com/nodejs/node/pull/18463,
// affecting Node 8 & 9 by performing an OS-level
// operation that always succeeds before reading
// environment variables:
if(needsEnvVarFix) require("os").cpus();

const value = process.env[key] !== undefined ? process.env[key] : this.defaultValues[key];

if(value === undefined) {
Expand Down
4 changes: 3 additions & 1 deletion lib/node/NodeTargetPlugin.js
Expand Up @@ -6,9 +6,11 @@

const ExternalsPlugin = require("../ExternalsPlugin");

const builtins = require("module").builtinModules || Object.keys(process.binding("natives"));

class NodeTargetPlugin {
apply(compiler) {
new ExternalsPlugin("commonjs", Object.keys(process.binding("natives"))).apply(compiler);
new ExternalsPlugin("commonjs", builtins).apply(compiler);
}
}

Expand Down

0 comments on commit 9323ee6

Please sign in to comment.