提交学习笔记专用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.6 KiB

  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("./Compiler")} Compiler */
  17. /** @typedef {import("./Dependency").DependencyLocation} DependencyLocation */
  18. /** @typedef {import("./javascript/JavascriptParser")} JavascriptParser */
  19. /** @typedef {import("./javascript/JavascriptParser").Range} Range */
  20. const PLUGIN_NAME = "WebpackIsIncludedPlugin";
  21. class WebpackIsIncludedPlugin {
  22. /**
  23. * @param {Compiler} compiler the compiler instance
  24. * @returns {void}
  25. */
  26. apply(compiler) {
  27. compiler.hooks.compilation.tap(
  28. PLUGIN_NAME,
  29. (compilation, { normalModuleFactory }) => {
  30. compilation.dependencyFactories.set(
  31. WebpackIsIncludedDependency,
  32. new IgnoreErrorModuleFactory(normalModuleFactory)
  33. );
  34. compilation.dependencyTemplates.set(
  35. WebpackIsIncludedDependency,
  36. new WebpackIsIncludedDependency.Template()
  37. );
  38. /**
  39. * @param {JavascriptParser} parser the parser
  40. * @returns {void}
  41. */
  42. const handler = (parser) => {
  43. parser.hooks.call
  44. .for("__webpack_is_included__")
  45. .tap(PLUGIN_NAME, (expr) => {
  46. if (
  47. expr.type !== "CallExpression" ||
  48. expr.arguments.length !== 1 ||
  49. expr.arguments[0].type === "SpreadElement"
  50. ) {
  51. return;
  52. }
  53. const request = parser.evaluateExpression(expr.arguments[0]);
  54. if (!request.isString()) return;
  55. const dep = new WebpackIsIncludedDependency(
  56. /** @type {string} */ (request.string),
  57. /** @type {Range} */ (expr.range)
  58. );
  59. dep.loc = /** @type {DependencyLocation} */ (expr.loc);
  60. parser.state.module.addDependency(dep);
  61. return true;
  62. });
  63. parser.hooks.typeof
  64. .for("__webpack_is_included__")
  65. .tap(
  66. PLUGIN_NAME,
  67. toConstantDependency(parser, JSON.stringify("function"))
  68. );
  69. };
  70. normalModuleFactory.hooks.parser
  71. .for(JAVASCRIPT_MODULE_TYPE_AUTO)
  72. .tap(PLUGIN_NAME, handler);
  73. normalModuleFactory.hooks.parser
  74. .for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
  75. .tap(PLUGIN_NAME, handler);
  76. normalModuleFactory.hooks.parser
  77. .for(JAVASCRIPT_MODULE_TYPE_ESM)
  78. .tap(PLUGIN_NAME, handler);
  79. }
  80. );
  81. }
  82. }
  83. module.exports = WebpackIsIncludedPlugin;