RequireIncludeDependencyParserPlugin.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const WebpackError = require("../WebpackError");
  7. const {
  8. evaluateToString,
  9. toConstantDependency
  10. } = require("../javascript/JavascriptParserHelpers");
  11. const makeSerializable = require("../util/makeSerializable");
  12. const RequireIncludeDependency = require("./RequireIncludeDependency");
  13. /** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
  14. /** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
  15. /** @typedef {import("../javascript/JavascriptParser").Range} Range */
  16. module.exports = class RequireIncludeDependencyParserPlugin {
  17. /**
  18. * @param {boolean} warn true: warn about deprecation, false: don't warn
  19. */
  20. constructor(warn) {
  21. this.warn = warn;
  22. }
  23. /**
  24. * @param {JavascriptParser} parser the parser
  25. * @returns {void}
  26. */
  27. apply(parser) {
  28. const { warn } = this;
  29. parser.hooks.call
  30. .for("require.include")
  31. .tap("RequireIncludeDependencyParserPlugin", expr => {
  32. if (expr.arguments.length !== 1) return;
  33. const param = parser.evaluateExpression(expr.arguments[0]);
  34. if (!param.isString()) return;
  35. if (warn) {
  36. parser.state.module.addWarning(
  37. new RequireIncludeDeprecationWarning(
  38. /** @type {DependencyLocation} */ (expr.loc)
  39. )
  40. );
  41. }
  42. const dep = new RequireIncludeDependency(
  43. /** @type {string} */ (param.string),
  44. /** @type {Range} */ (expr.range)
  45. );
  46. dep.loc = /** @type {DependencyLocation} */ (expr.loc);
  47. parser.state.current.addDependency(dep);
  48. return true;
  49. });
  50. parser.hooks.evaluateTypeof
  51. .for("require.include")
  52. .tap("RequireIncludePlugin", expr => {
  53. if (warn) {
  54. parser.state.module.addWarning(
  55. new RequireIncludeDeprecationWarning(
  56. /** @type {DependencyLocation} */ (expr.loc)
  57. )
  58. );
  59. }
  60. return evaluateToString("function")(expr);
  61. });
  62. parser.hooks.typeof
  63. .for("require.include")
  64. .tap("RequireIncludePlugin", expr => {
  65. if (warn) {
  66. parser.state.module.addWarning(
  67. new RequireIncludeDeprecationWarning(
  68. /** @type {DependencyLocation} */ (expr.loc)
  69. )
  70. );
  71. }
  72. return toConstantDependency(parser, JSON.stringify("function"))(expr);
  73. });
  74. }
  75. };
  76. class RequireIncludeDeprecationWarning extends WebpackError {
  77. /**
  78. * @param {DependencyLocation} loc location
  79. */
  80. constructor(loc) {
  81. super("require.include() is deprecated and will be removed soon.");
  82. this.name = "RequireIncludeDeprecationWarning";
  83. this.loc = loc;
  84. }
  85. }
  86. makeSerializable(
  87. RequireIncludeDeprecationWarning,
  88. "webpack/lib/dependencies/RequireIncludeDependencyParserPlugin",
  89. "RequireIncludeDeprecationWarning"
  90. );