RequireIncludeDependencyParserPlugin.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. module.exports = class RequireIncludeDependencyParserPlugin {
  14. constructor(warn) {
  15. this.warn = warn;
  16. }
  17. apply(parser) {
  18. const { warn } = this;
  19. parser.hooks.call
  20. .for("require.include")
  21. .tap("RequireIncludeDependencyParserPlugin", expr => {
  22. if (expr.arguments.length !== 1) return;
  23. const param = parser.evaluateExpression(expr.arguments[0]);
  24. if (!param.isString()) return;
  25. if (warn) {
  26. parser.state.module.addWarning(
  27. new RequireIncludeDeprecationWarning(expr.loc)
  28. );
  29. }
  30. const dep = new RequireIncludeDependency(param.string, expr.range);
  31. dep.loc = expr.loc;
  32. parser.state.current.addDependency(dep);
  33. return true;
  34. });
  35. parser.hooks.evaluateTypeof
  36. .for("require.include")
  37. .tap("RequireIncludePlugin", expr => {
  38. if (warn) {
  39. parser.state.module.addWarning(
  40. new RequireIncludeDeprecationWarning(expr.loc)
  41. );
  42. }
  43. return evaluateToString("function")(expr);
  44. });
  45. parser.hooks.typeof
  46. .for("require.include")
  47. .tap("RequireIncludePlugin", expr => {
  48. if (warn) {
  49. parser.state.module.addWarning(
  50. new RequireIncludeDeprecationWarning(expr.loc)
  51. );
  52. }
  53. return toConstantDependency(parser, JSON.stringify("function"))(expr);
  54. });
  55. }
  56. };
  57. class RequireIncludeDeprecationWarning extends WebpackError {
  58. constructor(loc) {
  59. super("require.include() is deprecated and will be removed soon.");
  60. this.name = "RequireIncludeDeprecationWarning";
  61. this.loc = loc;
  62. }
  63. }
  64. makeSerializable(
  65. RequireIncludeDeprecationWarning,
  66. "webpack/lib/dependencies/RequireIncludeDependencyParserPlugin",
  67. "RequireIncludeDeprecationWarning"
  68. );