NodeStuffPlugin.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 ExternalModuleDependency = require("./dependencies/ExternalModuleDependency");
  15. const {
  16. evaluateToString,
  17. expressionIsUnsupported
  18. } = require("./javascript/JavascriptParserHelpers");
  19. const { relative } = require("./util/fs");
  20. const { parseResource } = require("./util/identifier");
  21. /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
  22. /** @typedef {import("../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
  23. /** @typedef {import("../declarations/WebpackOptions").NodeOptions} NodeOptions */
  24. /** @typedef {import("./Compiler")} Compiler */
  25. /** @typedef {import("./Dependency")} Dependency */
  26. /** @typedef {import("./Dependency").DependencyLocation} DependencyLocation */
  27. /** @typedef {import("./DependencyTemplates")} DependencyTemplates */
  28. /** @typedef {import("./NormalModule")} NormalModule */
  29. /** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
  30. /** @typedef {import("./javascript/JavascriptParser")} JavascriptParser */
  31. /** @typedef {import("./javascript/JavascriptParser").Range} Range */
  32. /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
  33. const PLUGIN_NAME = "NodeStuffPlugin";
  34. class NodeStuffPlugin {
  35. /**
  36. * @param {NodeOptions} options options
  37. */
  38. constructor(options) {
  39. this.options = options;
  40. }
  41. /**
  42. * Apply the plugin
  43. * @param {Compiler} compiler the compiler instance
  44. * @returns {void}
  45. */
  46. apply(compiler) {
  47. const options = this.options;
  48. compiler.hooks.compilation.tap(
  49. PLUGIN_NAME,
  50. (compilation, { normalModuleFactory }) => {
  51. compilation.dependencyTemplates.set(
  52. ExternalModuleDependency,
  53. new ExternalModuleDependency.Template()
  54. );
  55. /**
  56. * @param {JavascriptParser} parser the parser
  57. * @param {JavascriptParserOptions} parserOptions options
  58. * @returns {void}
  59. */
  60. const handler = (parser, parserOptions) => {
  61. if (parserOptions.node === false) return;
  62. let localOptions = options;
  63. if (parserOptions.node) {
  64. localOptions = { ...localOptions, ...parserOptions.node };
  65. }
  66. if (localOptions.global !== false) {
  67. const withWarning = localOptions.global === "warn";
  68. parser.hooks.expression.for("global").tap(PLUGIN_NAME, expr => {
  69. const dep = new ConstDependency(
  70. RuntimeGlobals.global,
  71. /** @type {Range} */ (expr.range),
  72. [RuntimeGlobals.global]
  73. );
  74. dep.loc = /** @type {DependencyLocation} */ (expr.loc);
  75. parser.state.module.addPresentationalDependency(dep);
  76. // TODO webpack 6 remove
  77. if (withWarning) {
  78. parser.state.module.addWarning(
  79. new NodeStuffInWebError(
  80. dep.loc,
  81. "global",
  82. "The global namespace object is a Node.js feature and isn't available in browsers."
  83. )
  84. );
  85. }
  86. });
  87. parser.hooks.rename.for("global").tap(PLUGIN_NAME, expr => {
  88. const dep = new ConstDependency(
  89. RuntimeGlobals.global,
  90. /** @type {Range} */ (expr.range),
  91. [RuntimeGlobals.global]
  92. );
  93. dep.loc = /** @type {DependencyLocation} */ (expr.loc);
  94. parser.state.module.addPresentationalDependency(dep);
  95. return false;
  96. });
  97. }
  98. /**
  99. * @param {string} expressionName expression name
  100. * @param {(module: NormalModule) => string} fn function
  101. * @param {string=} warning warning
  102. * @returns {void}
  103. */
  104. const setModuleConstant = (expressionName, fn, warning) => {
  105. parser.hooks.expression
  106. .for(expressionName)
  107. .tap(PLUGIN_NAME, expr => {
  108. const dep = new CachedConstDependency(
  109. JSON.stringify(fn(parser.state.module)),
  110. /** @type {Range} */ (expr.range),
  111. expressionName
  112. );
  113. dep.loc = /** @type {DependencyLocation} */ (expr.loc);
  114. parser.state.module.addPresentationalDependency(dep);
  115. // TODO webpack 6 remove
  116. if (warning) {
  117. parser.state.module.addWarning(
  118. new NodeStuffInWebError(dep.loc, expressionName, warning)
  119. );
  120. }
  121. return true;
  122. });
  123. };
  124. /**
  125. * @param {string} expressionName expression name
  126. * @param {(value: string) => string} fn function
  127. * @returns {void}
  128. */
  129. const setUrlModuleConstant = (expressionName, fn) => {
  130. parser.hooks.expression
  131. .for(expressionName)
  132. .tap(PLUGIN_NAME, expr => {
  133. const dep = new ExternalModuleDependency(
  134. "url",
  135. [
  136. {
  137. name: "fileURLToPath",
  138. value: "__webpack_fileURLToPath__"
  139. }
  140. ],
  141. undefined,
  142. fn("__webpack_fileURLToPath__"),
  143. /** @type {Range} */ (expr.range),
  144. expressionName
  145. );
  146. dep.loc = /** @type {DependencyLocation} */ (expr.loc);
  147. parser.state.module.addPresentationalDependency(dep);
  148. return true;
  149. });
  150. };
  151. /**
  152. * @param {string} expressionName expression name
  153. * @param {string} value value
  154. * @param {string=} warning warning
  155. * @returns {void}
  156. */
  157. const setConstant = (expressionName, value, warning) =>
  158. setModuleConstant(expressionName, () => value, warning);
  159. const context = compiler.context;
  160. if (localOptions.__filename) {
  161. switch (localOptions.__filename) {
  162. case "mock":
  163. setConstant("__filename", "/index.js");
  164. break;
  165. case "warn-mock":
  166. setConstant(
  167. "__filename",
  168. "/index.js",
  169. "__filename is a Node.js feature and isn't available in browsers."
  170. );
  171. break;
  172. case "node-module":
  173. setUrlModuleConstant(
  174. "__filename",
  175. functionName => `${functionName}(import.meta.url)`
  176. );
  177. break;
  178. case true:
  179. setModuleConstant("__filename", module =>
  180. relative(
  181. /** @type {InputFileSystem} */ (compiler.inputFileSystem),
  182. context,
  183. module.resource
  184. )
  185. );
  186. break;
  187. }
  188. parser.hooks.evaluateIdentifier
  189. .for("__filename")
  190. .tap(PLUGIN_NAME, expr => {
  191. if (!parser.state.module) return;
  192. const resource = parseResource(parser.state.module.resource);
  193. return evaluateToString(resource.path)(expr);
  194. });
  195. }
  196. if (localOptions.__dirname) {
  197. switch (localOptions.__dirname) {
  198. case "mock":
  199. setConstant("__dirname", "/");
  200. break;
  201. case "warn-mock":
  202. setConstant(
  203. "__dirname",
  204. "/",
  205. "__dirname is a Node.js feature and isn't available in browsers."
  206. );
  207. break;
  208. case "node-module":
  209. setUrlModuleConstant(
  210. "__dirname",
  211. functionName =>
  212. `${functionName}(import.meta.url + "/..").slice(0, -1)`
  213. );
  214. break;
  215. case true:
  216. setModuleConstant("__dirname", module =>
  217. relative(
  218. /** @type {InputFileSystem} */ (compiler.inputFileSystem),
  219. context,
  220. /** @type {string} */ (module.context)
  221. )
  222. );
  223. break;
  224. }
  225. parser.hooks.evaluateIdentifier
  226. .for("__dirname")
  227. .tap(PLUGIN_NAME, expr => {
  228. if (!parser.state.module) return;
  229. return evaluateToString(
  230. /** @type {string} */ (parser.state.module.context)
  231. )(expr);
  232. });
  233. }
  234. parser.hooks.expression
  235. .for("require.extensions")
  236. .tap(
  237. PLUGIN_NAME,
  238. expressionIsUnsupported(
  239. parser,
  240. "require.extensions is not supported by webpack. Use a loader instead."
  241. )
  242. );
  243. };
  244. normalModuleFactory.hooks.parser
  245. .for(JAVASCRIPT_MODULE_TYPE_AUTO)
  246. .tap(PLUGIN_NAME, handler);
  247. normalModuleFactory.hooks.parser
  248. .for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
  249. .tap(PLUGIN_NAME, handler);
  250. }
  251. );
  252. }
  253. }
  254. module.exports = NodeStuffPlugin;