URLDependency.js 4.9 KB

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