2013-01-31 01:49:25 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
2017-05-07 02:41:26 +08:00
|
|
|
"use strict";
|
2018-07-03 16:24:29 +08:00
|
|
|
|
2017-05-07 02:41:26 +08:00
|
|
|
const AliasPlugin = require("enhanced-resolve/lib/AliasPlugin");
|
|
|
|
const nodeLibsBrowser = require("node-libs-browser");
|
2018-07-03 16:24:29 +08:00
|
|
|
const {
|
|
|
|
addParsedVariableToModule,
|
|
|
|
requireFileAsExpression
|
2018-07-03 16:25:08 +08:00
|
|
|
} = require("../JavascriptParserHelpers");
|
2013-01-31 01:49:25 +08:00
|
|
|
|
2017-05-07 02:41:26 +08:00
|
|
|
module.exports = class NodeSourcePlugin {
|
|
|
|
constructor(options) {
|
|
|
|
this.options = options;
|
2014-03-19 05:34:35 +08:00
|
|
|
}
|
2017-05-07 02:41:26 +08:00
|
|
|
apply(compiler) {
|
2017-05-07 04:16:50 +08:00
|
|
|
const options = this.options;
|
2018-05-29 20:50:40 +08:00
|
|
|
if (options === false) {
|
2018-02-25 09:00:20 +08:00
|
|
|
// allow single kill switch to turn off this plugin
|
2017-06-02 07:47:43 +08:00
|
|
|
return;
|
2018-05-29 20:50:40 +08:00
|
|
|
}
|
2016-11-12 01:42:00 +08:00
|
|
|
|
2017-11-08 18:32:05 +08:00
|
|
|
const getPathToModule = (module, type) => {
|
2018-02-25 09:00:20 +08:00
|
|
|
if (type === true || (type === undefined && nodeLibsBrowser[module])) {
|
2018-05-29 20:50:40 +08:00
|
|
|
if (!nodeLibsBrowser[module]) {
|
2018-02-25 09:00:20 +08:00
|
|
|
throw new Error(
|
|
|
|
`No browser version for node.js core module ${module} available`
|
|
|
|
);
|
2018-05-29 20:50:40 +08:00
|
|
|
}
|
2017-05-07 02:41:26 +08:00
|
|
|
return nodeLibsBrowser[module];
|
2018-02-25 09:00:20 +08:00
|
|
|
} else if (type === "mock") {
|
2017-05-07 02:41:26 +08:00
|
|
|
return require.resolve(`node-libs-browser/mock/${module}`);
|
2018-02-25 09:00:20 +08:00
|
|
|
} else if (type === "empty") {
|
2017-05-07 02:41:26 +08:00
|
|
|
return require.resolve("node-libs-browser/mock/empty");
|
2018-05-29 20:50:40 +08:00
|
|
|
} else {
|
|
|
|
return module;
|
|
|
|
}
|
2017-11-08 18:32:05 +08:00
|
|
|
};
|
2016-11-23 00:58:24 +08:00
|
|
|
|
2017-11-08 18:32:05 +08:00
|
|
|
const addExpression = (parser, name, module, type, suffix) => {
|
2017-05-07 02:41:26 +08:00
|
|
|
suffix = suffix || "";
|
2017-12-19 21:35:30 +08:00
|
|
|
parser.hooks.expression.for(name).tap("NodeSourcePlugin", () => {
|
2018-02-25 09:00:20 +08:00
|
|
|
if (
|
|
|
|
parser.state.module &&
|
|
|
|
parser.state.module.resource === getPathToModule(module, type)
|
|
|
|
)
|
|
|
|
return;
|
2018-07-03 16:24:29 +08:00
|
|
|
const mockModule = requireFileAsExpression(
|
2018-02-25 09:00:20 +08:00
|
|
|
parser.state.module.context,
|
|
|
|
getPathToModule(module, type)
|
|
|
|
);
|
2018-07-03 16:24:29 +08:00
|
|
|
return addParsedVariableToModule(parser, name, mockModule + suffix);
|
2017-05-07 02:41:26 +08:00
|
|
|
});
|
2017-11-08 18:32:05 +08:00
|
|
|
};
|
2016-09-14 18:04:42 +08:00
|
|
|
|
2018-02-25 09:00:20 +08:00
|
|
|
compiler.hooks.compilation.tap(
|
|
|
|
"NodeSourcePlugin",
|
|
|
|
(compilation, { normalModuleFactory }) => {
|
|
|
|
const handler = (parser, parserOptions) => {
|
|
|
|
if (parserOptions.node === false) return;
|
2016-09-14 18:04:42 +08:00
|
|
|
|
2018-02-25 09:00:20 +08:00
|
|
|
let localOptions = options;
|
2018-05-29 20:50:40 +08:00
|
|
|
if (parserOptions.node) {
|
2018-02-25 09:00:20 +08:00
|
|
|
localOptions = Object.assign({}, localOptions, parserOptions.node);
|
2018-05-29 20:50:40 +08:00
|
|
|
}
|
2018-02-25 09:00:20 +08:00
|
|
|
if (localOptions.global) {
|
|
|
|
parser.hooks.expression
|
|
|
|
.for("global")
|
|
|
|
.tap("NodeSourcePlugin", () => {
|
2018-07-03 16:24:29 +08:00
|
|
|
const retrieveGlobalModule = requireFileAsExpression(
|
2018-02-25 09:00:20 +08:00
|
|
|
parser.state.module.context,
|
2018-07-09 20:48:28 +08:00
|
|
|
require.resolve("../../buildin/global")
|
2018-02-25 09:00:20 +08:00
|
|
|
);
|
2018-07-03 16:24:29 +08:00
|
|
|
return addParsedVariableToModule(
|
2018-02-25 09:00:20 +08:00
|
|
|
parser,
|
|
|
|
"global",
|
|
|
|
retrieveGlobalModule
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (localOptions.process) {
|
|
|
|
const processType = localOptions.process;
|
|
|
|
addExpression(parser, "process", "process", processType);
|
|
|
|
}
|
|
|
|
if (localOptions.console) {
|
|
|
|
const consoleType = localOptions.console;
|
|
|
|
addExpression(parser, "console", "console", consoleType);
|
|
|
|
}
|
|
|
|
const bufferType = localOptions.Buffer;
|
|
|
|
if (bufferType) {
|
|
|
|
addExpression(parser, "Buffer", "buffer", bufferType, ".Buffer");
|
|
|
|
}
|
|
|
|
if (localOptions.setImmediate) {
|
|
|
|
const setImmediateType = localOptions.setImmediate;
|
|
|
|
addExpression(
|
|
|
|
parser,
|
|
|
|
"setImmediate",
|
|
|
|
"timers",
|
|
|
|
setImmediateType,
|
|
|
|
".setImmediate"
|
|
|
|
);
|
|
|
|
addExpression(
|
|
|
|
parser,
|
|
|
|
"clearImmediate",
|
|
|
|
"timers",
|
|
|
|
setImmediateType,
|
|
|
|
".clearImmediate"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
normalModuleFactory.hooks.parser
|
|
|
|
.for("javascript/auto")
|
|
|
|
.tap("NodeSourcePlugin", handler);
|
|
|
|
normalModuleFactory.hooks.parser
|
|
|
|
.for("javascript/dynamic")
|
|
|
|
.tap("NodeSourcePlugin", handler);
|
|
|
|
normalModuleFactory.hooks.parser
|
|
|
|
.for("javascript/esm")
|
|
|
|
.tap("NodeSourcePlugin", handler);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
compiler.hooks.afterResolvers.tap("NodeSourcePlugin", compiler => {
|
|
|
|
for (const lib of Object.keys(nodeLibsBrowser)) {
|
|
|
|
if (options[lib] !== false) {
|
|
|
|
compiler.resolverFactory.hooks.resolver
|
|
|
|
.for("normal")
|
|
|
|
.tap("NodeSourcePlugin", resolver => {
|
|
|
|
new AliasPlugin(
|
|
|
|
"described-resolve",
|
|
|
|
{
|
|
|
|
name: lib,
|
|
|
|
onlyModule: true,
|
|
|
|
alias: getPathToModule(lib, options[lib])
|
|
|
|
},
|
|
|
|
"resolve"
|
|
|
|
).apply(resolver);
|
|
|
|
});
|
2017-05-07 02:41:26 +08:00
|
|
|
}
|
2018-01-22 20:52:43 +08:00
|
|
|
}
|
2013-02-26 20:31:05 +08:00
|
|
|
});
|
2017-05-07 02:41:26 +08:00
|
|
|
}
|
2015-01-22 03:25:25 +08:00
|
|
|
};
|