1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const RuntimeGlobals = require("../RuntimeGlobals");
|
9 | const {
|
10 | getDependencyUsedByExportsCondition
|
11 | } = require("../optimize/InnerGraph");
|
12 | const makeSerializable = require("../util/makeSerializable");
|
13 | const memoize = require("../util/memoize");
|
14 | const ModuleDependency = require("./ModuleDependency");
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | const getRawDataUrlModule = memoize(() => require("../asset/RawDataUrlModule"));
|
29 |
|
30 | class URLDependency extends ModuleDependency {
|
31 | |
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | constructor(request, range, outerRange, relative) {
|
38 | super(request);
|
39 | this.range = range;
|
40 | this.outerRange = outerRange;
|
41 | this.relative = relative || false;
|
42 |
|
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 |
|
56 |
|
57 |
|
58 | getCondition(moduleGraph) {
|
59 | return getDependencyUsedByExportsCondition(
|
60 | this,
|
61 | this.usedByExports,
|
62 | moduleGraph
|
63 | );
|
64 | }
|
65 |
|
66 | |
67 |
|
68 |
|
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 |
|
92 | URLDependency.Template = class URLDependencyTemplate extends (
|
93 | ModuleDependency.Template
|
94 | ) {
|
95 | |
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | apply(dependency, source, templateContext) {
|
102 | const {
|
103 | chunkGraph,
|
104 | moduleGraph,
|
105 | runtimeRequirements,
|
106 | runtimeTemplate,
|
107 | runtime
|
108 | } = templateContext;
|
109 | const dep = (dependency);
|
110 | const connection = moduleGraph.getConnection(dep);
|
111 |
|
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 |
|
156 | makeSerializable(URLDependency, "webpack/lib/dependencies/URLDependency");
|
157 |
|
158 | module.exports = URLDependency;
|