2013-01-31 01:49:25 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
2018-07-30 23:08:51 +08:00
|
|
|
|
2017-05-07 02:41:26 +08:00
|
|
|
"use strict";
|
2018-07-03 16:24:29 +08:00
|
|
|
|
2018-12-06 19:11:01 +08:00
|
|
|
const RuntimeGlobals = require("../RuntimeGlobals");
|
|
|
|
const ConstDependency = require("../dependencies/ConstDependency");
|
2013-01-31 01:49:25 +08:00
|
|
|
|
2018-12-05 21:49:36 +08:00
|
|
|
/** @typedef {import("../../declarations/WebpackOptions").NodeOptions} NodeOptions */
|
2018-11-09 05:59:19 +08:00
|
|
|
/** @typedef {import("../Compiler")} Compiler */
|
|
|
|
|
2017-05-07 02:41:26 +08:00
|
|
|
module.exports = class NodeSourcePlugin {
|
2018-12-05 21:49:36 +08:00
|
|
|
/**
|
|
|
|
* @param {NodeOptions | false} options plugin options
|
|
|
|
*/
|
2017-05-07 02:41:26 +08:00
|
|
|
constructor(options) {
|
2020-08-20 05:17:33 +08:00
|
|
|
this._options = options;
|
2014-03-19 05:34:35 +08:00
|
|
|
}
|
2018-11-09 05:59:19 +08:00
|
|
|
|
|
|
|
/**
|
2020-04-23 16:48:36 +08:00
|
|
|
* Apply the plugin
|
2018-11-09 05:59:19 +08:00
|
|
|
* @param {Compiler} compiler the compiler instance
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2017-05-07 02:41:26 +08:00
|
|
|
apply(compiler) {
|
2020-08-20 05:17:33 +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
|
|
|
|
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) {
|
2019-06-19 19:16:05 +08:00
|
|
|
localOptions = { ...localOptions, ...parserOptions.node };
|
2018-05-29 20:50:40 +08:00
|
|
|
}
|
2018-12-06 19:11:01 +08:00
|
|
|
|
2018-02-25 09:00:20 +08:00
|
|
|
if (localOptions.global) {
|
|
|
|
parser.hooks.expression
|
|
|
|
.for("global")
|
2018-07-25 02:05:29 +08:00
|
|
|
.tap("NodeSourcePlugin", expr => {
|
2018-12-06 19:11:01 +08:00
|
|
|
const dep = new ConstDependency(
|
|
|
|
RuntimeGlobals.global,
|
|
|
|
expr.range,
|
|
|
|
[RuntimeGlobals.global]
|
2018-02-25 09:00:20 +08:00
|
|
|
);
|
2018-07-25 03:57:48 +08:00
|
|
|
dep.loc = expr.loc;
|
2019-10-30 13:40:40 +08:00
|
|
|
parser.state.module.addPresentationalDependency(dep);
|
2018-02-25 09:00:20 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2018-12-05 21:49:36 +08:00
|
|
|
|
2018-02-25 09:00:20 +08:00
|
|
|
normalModuleFactory.hooks.parser
|
|
|
|
.for("javascript/auto")
|
|
|
|
.tap("NodeSourcePlugin", handler);
|
|
|
|
normalModuleFactory.hooks.parser
|
|
|
|
.for("javascript/dynamic")
|
|
|
|
.tap("NodeSourcePlugin", handler);
|
2018-12-20 15:51:54 +08:00
|
|
|
normalModuleFactory.hooks.parser
|
|
|
|
.for("javascript/esm")
|
|
|
|
.tap("NodeSourcePlugin", handler);
|
2018-02-25 09:00:20 +08:00
|
|
|
}
|
|
|
|
);
|
2017-05-07 02:41:26 +08:00
|
|
|
}
|
2015-01-22 03:25:25 +08:00
|
|
|
};
|