/
replaceBundleReferences.js
220 lines (197 loc) Β· 5.46 KB
/
replaceBundleReferences.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// @flow strict-local
import type SourceMap from '@parcel/source-map';
import type {
Async,
Blob,
Bundle,
BundleGraph,
Dependency,
NamedBundle,
} from '@parcel/types';
import {Readable} from 'stream';
import nullthrows from 'nullthrows';
import invariant from 'assert';
import URL from 'url';
import {bufferStream, relativeBundlePath, urlJoin} from './';
type ReplacementMap = Map<
string /* dependency id */,
{|from: string, to: string|},
>;
/*
* Replaces references to dependency ids for URL dependencies with:
* - in the case of an unresolvable url dependency, the original specifier.
* These are external requests that Parcel did not bundle.
* - in the case of a reference to another bundle, the relative url to that
* bundle from the current bundle.
*/
export function replaceURLReferences({
bundle,
bundleGraph,
contents,
map,
relative = true,
shouldBeEscaped = false,
}: {|
bundle: NamedBundle,
bundleGraph: BundleGraph<NamedBundle>,
contents: string,
relative?: boolean,
map?: ?SourceMap,
shouldBeEscaped?: boolean,
|}): {|+contents: string, +map: ?SourceMap|} {
let replacements = new Map();
let urlDependencies = [];
bundle.traverse(node => {
if (node.type === 'dependency' && node.value.specifierType === 'url') {
urlDependencies.push(node.value);
}
});
for (let dependency of urlDependencies) {
if (dependency.specifierType !== 'url') {
continue;
}
let placeholder = dependency.meta?.placeholder ?? dependency.id;
invariant(typeof placeholder === 'string');
let resolved = bundleGraph.getReferencedBundle(dependency, bundle);
if (resolved == null) {
replacements.set(placeholder, {
from: placeholder,
to: dependency.specifier,
});
continue;
}
if (resolved.bundleBehavior === 'inline') {
// If a bundle is inline, it should be replaced with inline contents,
// not a URL.
continue;
}
replacements.set(
placeholder,
getURLReplacement({
dependency,
fromBundle: bundle,
toBundle: resolved,
relative,
}),
);
}
return performReplacement(replacements, contents, map, shouldBeEscaped);
}
/*
* Replaces references to dependency ids for inline bundles with the packaged
* contents of that bundle.
*/
export async function replaceInlineReferences({
bundle,
bundleGraph,
contents,
map,
getInlineReplacement,
getInlineBundleContents,
}: {|
bundle: Bundle,
bundleGraph: BundleGraph<NamedBundle>,
contents: string,
getInlineReplacement: (
Dependency,
?'string',
string,
) => {|from: string, to: string|},
getInlineBundleContents: (
Bundle,
BundleGraph<NamedBundle>,
) => Async<{|contents: Blob|}>,
map?: ?SourceMap,
|}): Promise<{|+contents: string, +map: ?SourceMap|}> {
let replacements = new Map();
let dependencies = [];
bundle.traverse(node => {
if (node.type === 'dependency') {
dependencies.push(node.value);
}
});
for (let dependency of dependencies) {
let entryBundle = bundleGraph.getReferencedBundle(dependency, bundle);
if (entryBundle?.bundleBehavior !== 'inline') {
continue;
}
let packagedBundle = await getInlineBundleContents(
entryBundle,
bundleGraph,
);
let packagedContents = (
packagedBundle.contents instanceof Readable
? await bufferStream(packagedBundle.contents)
: packagedBundle.contents
).toString();
let inlineType = nullthrows(entryBundle.getMainEntry()).meta.inlineType;
if (inlineType == null || inlineType === 'string') {
let placeholder = dependency.meta?.placeholder ?? dependency.id;
invariant(typeof placeholder === 'string');
replacements.set(
placeholder,
getInlineReplacement(dependency, inlineType, packagedContents),
);
}
}
return performReplacement(replacements, contents, map);
}
export function getURLReplacement({
dependency,
fromBundle,
toBundle,
relative,
}: {|
dependency: Dependency,
fromBundle: NamedBundle,
toBundle: NamedBundle,
relative: boolean,
|}): {|from: string, to: string|} {
let to;
let orig = URL.parse(dependency.specifier);
if (relative) {
to = URL.format({
pathname: relativeBundlePath(fromBundle, toBundle, {
leadingDotSlash: false,
}),
hash: orig.hash,
});
// If the resulting path includes a colon character and doesn't start with a ./ or ../
// we need to add one so that the first part before the colon isn't parsed as a URL protocol.
if (to.includes(':') && !to.startsWith('./') && !to.startsWith('../')) {
to = './' + to;
}
} else {
to = urlJoin(
toBundle.target.publicUrl,
URL.format({
pathname: nullthrows(toBundle.name),
hash: orig.hash,
}),
);
}
let placeholder = dependency.meta?.placeholder ?? dependency.id;
invariant(typeof placeholder === 'string');
return {
from: placeholder,
to,
};
}
function performReplacement(
replacements: ReplacementMap,
contents: string,
map?: ?SourceMap,
shouldBeEscaped = true,
): {|+contents: string, +map: ?SourceMap|} {
let finalContents = contents;
for (let {from, to} of replacements.values()) {
to = shouldBeEscaped ? to.replace(/"/g, '\\"').replace(/'/g, "\\'") : to;
// Perform replacement
finalContents = finalContents.split(from).join(to);
}
return {
contents: finalContents,
// TODO: Update sourcemap with adjusted contents
map,
};
}