WebpackIsIncludedPlugin.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Ivan Kopeykin @vankop
  4. */
  5. "use strict";
  6. const IgnoreErrorModuleFactory = require("./IgnoreErrorModuleFactory");
  7. const {
  8. JAVASCRIPT_MODULE_TYPE_AUTO,
  9. JAVASCRIPT_MODULE_TYPE_DYNAMIC,
  10. JAVASCRIPT_MODULE_TYPE_ESM
  11. } = require("./ModuleTypeConstants");
  12. const WebpackIsIncludedDependency = require("./dependencies/WebpackIsIncludedDependency");
  13. const {
  14. toConstantDependency
  15. } = require("./javascript/JavascriptParserHelpers");
  16. /** @typedef {import("enhanced-resolve").Resolver} Resolver */
  17. /** @typedef {import("./Compiler")} Compiler */
  18. /** @typedef {import("./Dependency").DependencyLocation} DependencyLocation */
  19. /** @typedef {import("./Module")} Module */
  20. /** @typedef {import("./javascript/JavascriptParser")} JavascriptParser */
  21. /** @typedef {import("./javascript/JavascriptParser").Range} Range */
  22. const PLUGIN_NAME = "WebpackIsIncludedPlugin";
  23. class WebpackIsIncludedPlugin {
  24. /**
  25. * @param {Compiler} compiler the compiler instance
  26. * @returns {void}
  27. */
  28. apply(compiler) {
  29. compiler.hooks.compilation.tap(
  30. PLUGIN_NAME,
  31. (compilation, { normalModuleFactory }) => {
  32. compilation.dependencyFactories.set(
  33. WebpackIsIncludedDependency,
  34. new IgnoreErrorModuleFactory(normalModuleFactory)
  35. );
  36. compilation.dependencyTemplates.set(
  37. WebpackIsIncludedDependency,
  38. new WebpackIsIncludedDependency.Template()
  39. );
  40. /**
  41. * @param {JavascriptParser} parser the parser
  42. * @returns {void}
  43. */
  44. const handler = parser => {
  45. parser.hooks.call
  46. .for("__webpack_is_included__")
  47. .tap(PLUGIN_NAME, expr => {
  48. if (
  49. expr.type !== "CallExpression" ||
  50. expr.arguments.length !== 1 ||
  51. expr.arguments[0].type === "SpreadElement"
  52. )
  53. return;
  54. const request = parser.evaluateExpression(expr.arguments[0]);
  55. if (!request.isString()) return;
  56. const dep = new WebpackIsIncludedDependency(
  57. /** @type {string} */ (request.string),
  58. /** @type {Range} */ (expr.range)
  59. );
  60. dep.loc = /** @type {DependencyLocation} */ (expr.loc);
  61. parser.state.module.addDependency(dep);
  62. return true;
  63. });
  64. parser.hooks.typeof
  65. .for("__webpack_is_included__")
  66. .tap(
  67. PLUGIN_NAME,
  68. toConstantDependency(parser, JSON.stringify("function"))
  69. );
  70. };
  71. normalModuleFactory.hooks.parser
  72. .for(JAVASCRIPT_MODULE_TYPE_AUTO)
  73. .tap(PLUGIN_NAME, handler);
  74. normalModuleFactory.hooks.parser
  75. .for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
  76. .tap(PLUGIN_NAME, handler);
  77. normalModuleFactory.hooks.parser
  78. .for(JAVASCRIPT_MODULE_TYPE_ESM)
  79. .tap(PLUGIN_NAME, handler);
  80. }
  81. );
  82. }
  83. }
  84. module.exports = WebpackIsIncludedPlugin;