NodeStuffPlugin.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const {
  7. JAVASCRIPT_MODULE_TYPE_AUTO,
  8. JAVASCRIPT_MODULE_TYPE_DYNAMIC
  9. } = require("./ModuleTypeConstants");
  10. const NodeStuffInWebError = require("./NodeStuffInWebError");
  11. const RuntimeGlobals = require("./RuntimeGlobals");
  12. const CachedConstDependency = require("./dependencies/CachedConstDependency");
  13. const ConstDependency = require("./dependencies/ConstDependency");
  14. const {
  15. evaluateToString,
  16. expressionIsUnsupported
  17. } = require("./javascript/JavascriptParserHelpers");
  18. const { relative } = require("./util/fs");
  19. const { parseResource } = require("./util/identifier");
  20. /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
  21. /** @typedef {import("../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
  22. /** @typedef {import("../declarations/WebpackOptions").NodeOptions} NodeOptions */
  23. /** @typedef {import("./Compiler")} Compiler */
  24. /** @typedef {import("./Dependency")} Dependency */
  25. /** @typedef {import("./Dependency").DependencyLocation} DependencyLocation */
  26. /** @typedef {import("./DependencyTemplates")} DependencyTemplates */
  27. /** @typedef {import("./NormalModule")} NormalModule */
  28. /** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
  29. /** @typedef {import("./javascript/JavascriptParser")} JavascriptParser */
  30. /** @typedef {import("./javascript/JavascriptParser").Range} Range */
  31. const PLUGIN_NAME = "NodeStuffPlugin";
  32. class NodeStuffPlugin {
  33. /**
  34. * @param {NodeOptions} options options
  35. */
  36. constructor(options) {
  37. this.options = options;
  38. }
  39. /**
  40. * Apply the plugin
  41. * @param {Compiler} compiler the compiler instance
  42. * @returns {void}
  43. */
  44. apply(compiler) {
  45. const options = this.options;
  46. compiler.hooks.compilation.tap(
  47. PLUGIN_NAME,
  48. (compilation, { normalModuleFactory }) => {
  49. /**
  50. * @param {JavascriptParser} parser the parser
  51. * @param {JavascriptParserOptions} parserOptions options
  52. * @returns {void}
  53. */
  54. const handler = (parser, parserOptions) => {
  55. if (parserOptions.node === false) return;
  56. let localOptions = options;
  57. if (parserOptions.node) {
  58. localOptions = { ...localOptions, ...parserOptions.node };
  59. }
  60. if (localOptions.global !== false) {
  61. const withWarning = localOptions.global === "warn";
  62. parser.hooks.expression.for("global").tap(PLUGIN_NAME, expr => {
  63. const dep = new ConstDependency(
  64. RuntimeGlobals.global,
  65. /** @type {Range} */ (expr.range),
  66. [RuntimeGlobals.global]
  67. );
  68. dep.loc = /** @type {DependencyLocation} */ (expr.loc);
  69. parser.state.module.addPresentationalDependency(dep);
  70. // TODO webpack 6 remove
  71. if (withWarning) {
  72. parser.state.module.addWarning(
  73. new NodeStuffInWebError(
  74. dep.loc,
  75. "global",
  76. "The global namespace object is a Node.js feature and isn't available in browsers."
  77. )
  78. );
  79. }
  80. });
  81. parser.hooks.rename.for("global").tap(PLUGIN_NAME, expr => {
  82. const dep = new ConstDependency(
  83. RuntimeGlobals.global,
  84. /** @type {Range} */ (expr.range),
  85. [RuntimeGlobals.global]
  86. );
  87. dep.loc = /** @type {DependencyLocation} */ (expr.loc);
  88. parser.state.module.addPresentationalDependency(dep);
  89. return false;
  90. });
  91. }
  92. /**
  93. * @param {string} expressionName expression name
  94. * @param {(module: NormalModule) => string} fn function
  95. * @param {string=} warning warning
  96. * @returns {void}
  97. */
  98. const setModuleConstant = (expressionName, fn, warning) => {
  99. parser.hooks.expression
  100. .for(expressionName)
  101. .tap(PLUGIN_NAME, expr => {
  102. const dep = new CachedConstDependency(
  103. JSON.stringify(fn(parser.state.module)),
  104. /** @type {Range} */ (expr.range),
  105. expressionName
  106. );
  107. dep.loc = /** @type {DependencyLocation} */ (expr.loc);
  108. parser.state.module.addPresentationalDependency(dep);
  109. // TODO webpack 6 remove
  110. if (warning) {
  111. parser.state.module.addWarning(
  112. new NodeStuffInWebError(dep.loc, expressionName, warning)
  113. );
  114. }
  115. return true;
  116. });
  117. };
  118. /**
  119. * @param {string} expressionName expression name
  120. * @param {string} value value
  121. * @param {string=} warning warning
  122. * @returns {void}
  123. */
  124. const setConstant = (expressionName, value, warning) =>
  125. setModuleConstant(expressionName, () => value, warning);
  126. const context = compiler.context;
  127. if (localOptions.__filename) {
  128. switch (localOptions.__filename) {
  129. case "mock":
  130. setConstant("__filename", "/index.js");
  131. break;
  132. case "warn-mock":
  133. setConstant(
  134. "__filename",
  135. "/index.js",
  136. "__filename is a Node.js feature and isn't available in browsers."
  137. );
  138. break;
  139. case true:
  140. setModuleConstant("__filename", module =>
  141. relative(compiler.inputFileSystem, context, module.resource)
  142. );
  143. break;
  144. }
  145. parser.hooks.evaluateIdentifier
  146. .for("__filename")
  147. .tap(PLUGIN_NAME, expr => {
  148. if (!parser.state.module) return;
  149. const resource = parseResource(parser.state.module.resource);
  150. return evaluateToString(resource.path)(expr);
  151. });
  152. }
  153. if (localOptions.__dirname) {
  154. switch (localOptions.__dirname) {
  155. case "mock":
  156. setConstant("__dirname", "/");
  157. break;
  158. case "warn-mock":
  159. setConstant(
  160. "__dirname",
  161. "/",
  162. "__dirname is a Node.js feature and isn't available in browsers."
  163. );
  164. break;
  165. case true:
  166. setModuleConstant("__dirname", module =>
  167. relative(compiler.inputFileSystem, context, module.context)
  168. );
  169. break;
  170. }
  171. parser.hooks.evaluateIdentifier
  172. .for("__dirname")
  173. .tap(PLUGIN_NAME, expr => {
  174. if (!parser.state.module) return;
  175. return evaluateToString(parser.state.module.context)(expr);
  176. });
  177. }
  178. parser.hooks.expression
  179. .for("require.extensions")
  180. .tap(
  181. PLUGIN_NAME,
  182. expressionIsUnsupported(
  183. parser,
  184. "require.extensions is not supported by webpack. Use a loader instead."
  185. )
  186. );
  187. };
  188. normalModuleFactory.hooks.parser
  189. .for(JAVASCRIPT_MODULE_TYPE_AUTO)
  190. .tap(PLUGIN_NAME, handler);
  191. normalModuleFactory.hooks.parser
  192. .for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
  193. .tap(PLUGIN_NAME, handler);
  194. }
  195. );
  196. }
  197. }
  198. module.exports = NodeStuffPlugin;