UNPKG

4.61 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Ivan Kopeykin @vankop
4*/
5
6"use strict";
7
8const RuntimeGlobals = require("../RuntimeGlobals");
9const {
10 getDependencyUsedByExportsCondition
11} = require("../optimize/InnerGraph");
12const makeSerializable = require("../util/makeSerializable");
13const memoize = require("../util/memoize");
14const ModuleDependency = require("./ModuleDependency");
15
16/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
17/** @typedef {import("../ChunkGraph")} ChunkGraph */
18/** @typedef {import("../Dependency")} Dependency */
19/** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
20/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
21/** @typedef {import("../Module")} Module */
22/** @typedef {import("../ModuleGraph")} ModuleGraph */
23/** @typedef {import("../ModuleGraphConnection")} ModuleGraphConnection */
24/** @typedef {import("../ModuleGraphConnection").ConnectionState} ConnectionState */
25/** @typedef {import("../util/Hash")} Hash */
26/** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
27
28const getRawDataUrlModule = memoize(() => require("../asset/RawDataUrlModule"));
29
30class URLDependency extends ModuleDependency {
31 /**
32 * @param {string} request request
33 * @param {[number, number]} range range of the arguments of new URL( |> ... <| )
34 * @param {[number, number]} outerRange range of the full |> new URL(...) <|
35 * @param {boolean=} relative use relative urls instead of absolute with base uri
36 */
37 constructor(request, range, outerRange, relative) {
38 super(request);
39 this.range = range;
40 this.outerRange = outerRange;
41 this.relative = relative || false;
42 /** @type {Set<string> | boolean} */
43 this.usedByExports = undefined;
44 }
45
46 get type() {
47 return "new URL()";
48 }
49
50 get category() {
51 return "url";
52 }
53
54 /**
55 * @param {ModuleGraph} moduleGraph module graph
56 * @returns {null | false | function(ModuleGraphConnection, RuntimeSpec): ConnectionState} function to determine if the connection is active
57 */
58 getCondition(moduleGraph) {
59 return getDependencyUsedByExportsCondition(
60 this,
61 this.usedByExports,
62 moduleGraph
63 );
64 }
65
66 /**
67 * @param {string} context context directory
68 * @returns {Module} a module
69 */
70 createIgnoredModule(context) {
71 const RawDataUrlModule = getRawDataUrlModule();
72 return new RawDataUrlModule("data:,", `ignored-asset`, `(ignored asset)`);
73 }
74
75 serialize(context) {
76 const { write } = context;
77 write(this.outerRange);
78 write(this.relative);
79 write(this.usedByExports);
80 super.serialize(context);
81 }
82
83 deserialize(context) {
84 const { read } = context;
85 this.outerRange = read();
86 this.relative = read();
87 this.usedByExports = read();
88 super.deserialize(context);
89 }
90}
91
92URLDependency.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
121 runtimeRequirements.add(RuntimeGlobals.require);
122
123 if (dep.relative) {
124 runtimeRequirements.add(RuntimeGlobals.relativeUrl);
125 source.replace(
126 dep.outerRange[0],
127 dep.outerRange[1] - 1,
128 `/* asset import */ new ${
129 RuntimeGlobals.relativeUrl
130 }(${runtimeTemplate.moduleRaw({
131 chunkGraph,
132 module: moduleGraph.getModule(dep),
133 request: dep.request,
134 runtimeRequirements,
135 weak: false
136 })})`
137 );
138 } else {
139 runtimeRequirements.add(RuntimeGlobals.baseURI);
140
141 source.replace(
142 dep.range[0],
143 dep.range[1] - 1,
144 `/* asset import */ ${runtimeTemplate.moduleRaw({
145 chunkGraph,
146 module: moduleGraph.getModule(dep),
147 request: dep.request,
148 runtimeRequirements,
149 weak: false
150 })}, ${RuntimeGlobals.baseURI}`
151 );
152 }
153 }
154};
155
156makeSerializable(URLDependency, "webpack/lib/dependencies/URLDependency");
157
158module.exports = URLDependency;