URLDependency.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Ivan Kopeykin @vankop
  4. */
  5. "use strict";
  6. const RuntimeGlobals = require("../RuntimeGlobals");
  7. const {
  8. getDependencyUsedByExportsCondition
  9. } = require("../optimize/InnerGraph");
  10. const makeSerializable = require("../util/makeSerializable");
  11. const memoize = require("../util/memoize");
  12. const ModuleDependency = require("./ModuleDependency");
  13. /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
  14. /** @typedef {import("../ChunkGraph")} ChunkGraph */
  15. /** @typedef {import("../Dependency")} Dependency */
  16. /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
  17. /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
  18. /** @typedef {import("../Module")} Module */
  19. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  20. /** @typedef {import("../ModuleGraphConnection")} ModuleGraphConnection */
  21. /** @typedef {import("../ModuleGraphConnection").ConnectionState} ConnectionState */
  22. /** @typedef {import("../util/Hash")} Hash */
  23. /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
  24. const getRawDataUrlModule = memoize(() => require("../asset/RawDataUrlModule"));
  25. class URLDependency extends ModuleDependency {
  26. /**
  27. * @param {string} request request
  28. * @param {[number, number]} range range of the arguments of new URL( |> ... <| )
  29. * @param {[number, number]} outerRange range of the full |> new URL(...) <|
  30. * @param {boolean=} relative use relative urls instead of absolute with base uri
  31. */
  32. constructor(request, range, outerRange, relative) {
  33. super(request);
  34. this.range = range;
  35. this.outerRange = outerRange;
  36. this.relative = relative || false;
  37. /** @type {Set<string> | boolean} */
  38. this.usedByExports = undefined;
  39. }
  40. get type() {
  41. return "new URL()";
  42. }
  43. get category() {
  44. return "url";
  45. }
  46. /**
  47. * @param {ModuleGraph} moduleGraph module graph
  48. * @returns {null | false | function(ModuleGraphConnection, RuntimeSpec): ConnectionState} function to determine if the connection is active
  49. */
  50. getCondition(moduleGraph) {
  51. return getDependencyUsedByExportsCondition(
  52. this,
  53. this.usedByExports,
  54. moduleGraph
  55. );
  56. }
  57. /**
  58. * @param {string} context context directory
  59. * @returns {Module} a module
  60. */
  61. createIgnoredModule(context) {
  62. const RawDataUrlModule = getRawDataUrlModule();
  63. return new RawDataUrlModule("data:,", `ignored-asset`, `(ignored asset)`);
  64. }
  65. serialize(context) {
  66. const { write } = context;
  67. write(this.outerRange);
  68. write(this.relative);
  69. write(this.usedByExports);
  70. super.serialize(context);
  71. }
  72. deserialize(context) {
  73. const { read } = context;
  74. this.outerRange = read();
  75. this.relative = read();
  76. this.usedByExports = read();
  77. super.deserialize(context);
  78. }
  79. }
  80. URLDependency.Template = class URLDependencyTemplate extends (
  81. ModuleDependency.Template
  82. ) {
  83. /**
  84. * @param {Dependency} dependency the dependency for which the template should be applied
  85. * @param {ReplaceSource} source the current replace source which can be modified
  86. * @param {DependencyTemplateContext} templateContext the context object
  87. * @returns {void}
  88. */
  89. apply(dependency, source, templateContext) {
  90. const {
  91. chunkGraph,
  92. moduleGraph,
  93. runtimeRequirements,
  94. runtimeTemplate,
  95. runtime
  96. } = templateContext;
  97. const dep = /** @type {URLDependency} */ (dependency);
  98. const connection = moduleGraph.getConnection(dep);
  99. // Skip rendering depending when dependency is conditional
  100. if (connection && !connection.isTargetActive(runtime)) {
  101. source.replace(
  102. dep.outerRange[0],
  103. dep.outerRange[1] - 1,
  104. "/* unused asset import */ undefined"
  105. );
  106. return;
  107. }
  108. runtimeRequirements.add(RuntimeGlobals.require);
  109. if (dep.relative) {
  110. runtimeRequirements.add(RuntimeGlobals.relativeUrl);
  111. source.replace(
  112. dep.outerRange[0],
  113. dep.outerRange[1] - 1,
  114. `/* asset import */ new ${
  115. RuntimeGlobals.relativeUrl
  116. }(${runtimeTemplate.moduleRaw({
  117. chunkGraph,
  118. module: moduleGraph.getModule(dep),
  119. request: dep.request,
  120. runtimeRequirements,
  121. weak: false
  122. })})`
  123. );
  124. } else {
  125. runtimeRequirements.add(RuntimeGlobals.baseURI);
  126. source.replace(
  127. dep.range[0],
  128. dep.range[1] - 1,
  129. `/* asset import */ ${runtimeTemplate.moduleRaw({
  130. chunkGraph,
  131. module: moduleGraph.getModule(dep),
  132. request: dep.request,
  133. runtimeRequirements,
  134. weak: false
  135. })}, ${RuntimeGlobals.baseURI}`
  136. );
  137. }
  138. }
  139. };
  140. makeSerializable(URLDependency, "webpack/lib/dependencies/URLDependency");
  141. module.exports = URLDependency;