URLPlugin.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Ivan Kopeykin @vankop
  4. */
  5. "use strict";
  6. const { pathToFileURL } = require("url");
  7. const BasicEvaluatedExpression = require("../javascript/BasicEvaluatedExpression");
  8. const { approve } = require("../javascript/JavascriptParserHelpers");
  9. const InnerGraph = require("../optimize/InnerGraph");
  10. const URLDependency = require("./URLDependency");
  11. /** @typedef {import("estree").NewExpression} NewExpressionNode */
  12. /** @typedef {import("../Compiler")} Compiler */
  13. /** @typedef {import("../NormalModule")} NormalModule */
  14. /** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
  15. class URLPlugin {
  16. /**
  17. * @param {Compiler} compiler compiler
  18. */
  19. apply(compiler) {
  20. compiler.hooks.compilation.tap(
  21. "URLPlugin",
  22. (compilation, { normalModuleFactory }) => {
  23. compilation.dependencyFactories.set(URLDependency, normalModuleFactory);
  24. compilation.dependencyTemplates.set(
  25. URLDependency,
  26. new URLDependency.Template()
  27. );
  28. /**
  29. * @param {NormalModule} module module
  30. * @returns {URL} file url
  31. */
  32. const getUrl = module => {
  33. return pathToFileURL(module.resource);
  34. };
  35. /**
  36. * @param {JavascriptParser} parser parser
  37. * @param {object} parserOptions options
  38. */
  39. const parserCallback = (parser, parserOptions) => {
  40. if (parserOptions.url === false) return;
  41. const relative = parserOptions.url === "relative";
  42. /**
  43. * @param {NewExpressionNode} expr expression
  44. * @returns {undefined | string} request
  45. */
  46. const getUrlRequest = expr => {
  47. if (expr.arguments.length !== 2) return;
  48. const [arg1, arg2] = expr.arguments;
  49. if (
  50. arg2.type !== "MemberExpression" ||
  51. arg1.type === "SpreadElement"
  52. )
  53. return;
  54. const chain = parser.extractMemberExpressionChain(arg2);
  55. if (
  56. chain.members.length !== 1 ||
  57. chain.object.type !== "MetaProperty" ||
  58. chain.object.meta.name !== "import" ||
  59. chain.object.property.name !== "meta" ||
  60. chain.members[0] !== "url"
  61. )
  62. return;
  63. const request = parser.evaluateExpression(arg1).asString();
  64. return request;
  65. };
  66. parser.hooks.canRename.for("URL").tap("URLPlugin", approve);
  67. parser.hooks.evaluateNewExpression
  68. .for("URL")
  69. .tap("URLPlugin", expr => {
  70. const request = getUrlRequest(expr);
  71. if (!request) return;
  72. const url = new URL(request, getUrl(parser.state.module));
  73. return new BasicEvaluatedExpression()
  74. .setString(url.toString())
  75. .setRange(expr.range);
  76. });
  77. parser.hooks.new.for("URL").tap("URLPlugin", _expr => {
  78. const expr = /** @type {NewExpressionNode} */ (_expr);
  79. const request = getUrlRequest(expr);
  80. if (!request) return;
  81. const [arg1, arg2] = expr.arguments;
  82. const dep = new URLDependency(
  83. request,
  84. [arg1.range[0], arg2.range[1]],
  85. expr.range,
  86. relative
  87. );
  88. dep.loc = expr.loc;
  89. parser.state.current.addDependency(dep);
  90. InnerGraph.onUsage(parser.state, e => (dep.usedByExports = e));
  91. return true;
  92. });
  93. parser.hooks.isPure.for("NewExpression").tap("URLPlugin", _expr => {
  94. const expr = /** @type {NewExpressionNode} */ (_expr);
  95. const { callee } = expr;
  96. if (callee.type !== "Identifier") return;
  97. const calleeInfo = parser.getFreeInfoFromVariable(callee.name);
  98. if (!calleeInfo || calleeInfo.name !== "URL") return;
  99. const request = getUrlRequest(expr);
  100. if (request) return true;
  101. });
  102. };
  103. normalModuleFactory.hooks.parser
  104. .for("javascript/auto")
  105. .tap("URLPlugin", parserCallback);
  106. normalModuleFactory.hooks.parser
  107. .for("javascript/esm")
  108. .tap("URLPlugin", parserCallback);
  109. }
  110. );
  111. }
  112. }
  113. module.exports = URLPlugin;