URLDependency.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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("../Dependency")} Dependency */
  16. /** @typedef {import("../Dependency").GetConditionFn} GetConditionFn */
  17. /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
  18. /** @typedef {import("../Module")} Module */
  19. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  20. /** @typedef {import("../javascript/JavascriptParser").Range} Range */
  21. /** @typedef {import("../optimize/InnerGraph").UsedByExports} UsedByExports */
  22. /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  23. /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  24. const getIgnoredRawDataUrlModule = memoize(
  25. () => new RawDataUrlModule("data:,", "ignored-asset", "(ignored asset)")
  26. );
  27. class URLDependency extends ModuleDependency {
  28. /**
  29. * Creates an instance of URLDependency.
  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 {UsedByExports | undefined} */
  41. this.usedByExports = undefined;
  42. }
  43. get type() {
  44. return "new URL()";
  45. }
  46. get category() {
  47. return "url";
  48. }
  49. /**
  50. * Returns function to determine if the connection is active.
  51. * @param {ModuleGraph} moduleGraph module graph
  52. * @returns {null | false | GetConditionFn} function to determine if the connection is active
  53. */
  54. getCondition(moduleGraph) {
  55. return getDependencyUsedByExportsCondition(
  56. this,
  57. this.usedByExports,
  58. moduleGraph
  59. );
  60. }
  61. /**
  62. * Creates an ignored module.
  63. * @param {string} context context directory
  64. * @returns {Module} ignored module
  65. */
  66. createIgnoredModule(context) {
  67. return getIgnoredRawDataUrlModule();
  68. }
  69. /**
  70. * Serializes this instance into the provided serializer context.
  71. * @param {ObjectSerializerContext} context context
  72. */
  73. serialize(context) {
  74. const { write } = context;
  75. write(this.outerRange);
  76. write(this.relative);
  77. write(this.usedByExports);
  78. super.serialize(context);
  79. }
  80. /**
  81. * Restores this instance from the provided deserializer context.
  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. * Applies the plugin by registering its hooks on the compiler.
  97. * @param {Dependency} dependency the dependency for which the template should be applied
  98. * @param {ReplaceSource} source the current replace source which can be modified
  99. * @param {DependencyTemplateContext} templateContext the context object
  100. * @returns {void}
  101. */
  102. apply(dependency, source, templateContext) {
  103. const {
  104. chunkGraph,
  105. moduleGraph,
  106. runtimeRequirements,
  107. runtimeTemplate,
  108. runtime
  109. } = templateContext;
  110. const dep = /** @type {URLDependency} */ (dependency);
  111. const connection = moduleGraph.getConnection(dep);
  112. // Skip rendering depending when dependency is conditional
  113. if (connection && !connection.isTargetActive(runtime)) {
  114. source.replace(
  115. dep.outerRange[0],
  116. dep.outerRange[1] - 1,
  117. "/* unused asset import */ undefined"
  118. );
  119. return;
  120. }
  121. runtimeRequirements.add(RuntimeGlobals.require);
  122. if (dep.relative) {
  123. runtimeRequirements.add(RuntimeGlobals.relativeUrl);
  124. source.replace(
  125. dep.outerRange[0],
  126. dep.outerRange[1] - 1,
  127. `/* asset import */ new ${
  128. RuntimeGlobals.relativeUrl
  129. }(${runtimeTemplate.moduleRaw({
  130. chunkGraph,
  131. module: moduleGraph.getModule(dep),
  132. request: dep.request,
  133. runtimeRequirements,
  134. weak: false
  135. })})`
  136. );
  137. } else {
  138. runtimeRequirements.add(RuntimeGlobals.baseURI);
  139. source.replace(
  140. dep.range[0],
  141. dep.range[1] - 1,
  142. `/* asset import */ ${runtimeTemplate.moduleRaw({
  143. chunkGraph,
  144. module: moduleGraph.getModule(dep),
  145. request: dep.request,
  146. runtimeRequirements,
  147. weak: false
  148. })}, ${RuntimeGlobals.baseURI}`
  149. );
  150. }
  151. }
  152. };
  153. makeSerializable(URLDependency, "webpack/lib/dependencies/URLDependency");
  154. module.exports = URLDependency;