URLDependency.js 5.0 KB

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