forked from parcel-bundler/parcel
/
dependencies.js
291 lines (262 loc) Β· 7.54 KB
/
dependencies.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// @flow
import type {AST, MutableAsset} from '@parcel/types';
import type {PostHTMLNode} from 'posthtml';
import PostHTML from 'posthtml';
// A list of all attributes that may produce a dependency
// Based on https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes
const ATTRS = {
src: [
'script',
'img',
'audio',
'video',
'source',
'track',
'iframe',
'embed',
'amp-img',
],
// Using href with <script> is described here: https://developer.mozilla.org/en-US/docs/Web/SVG/Element/script
href: ['link', 'a', 'use', 'script', 'image'],
srcset: ['img', 'source'],
imagesrcset: ['link'],
poster: ['video'],
'xlink:href': ['use', 'image', 'script'],
content: ['meta'],
data: ['object'],
};
// A list of metadata that should produce a dependency
// Based on:
// - http://schema.org/
// - http://ogp.me
// - https://developer.twitter.com/en/docs/tweets/optimize-with-cards/overview/markup
// - https://msdn.microsoft.com/en-us/library/dn255024.aspx
// - https://vk.com/dev/publications
const META = {
property: [
'og:image',
'og:image:url',
'og:image:secure_url',
'og:audio',
'og:audio:secure_url',
'og:video',
'og:video:secure_url',
'vk:image',
],
name: [
'twitter:image',
'msapplication-square150x150logo',
'msapplication-square310x310logo',
'msapplication-square70x70logo',
'msapplication-wide310x150logo',
'msapplication-TileImage',
'msapplication-config',
],
itemprop: [
'image',
'logo',
'screenshot',
'thumbnailUrl',
'contentUrl',
'downloadUrl',
],
};
const FEED_TYPES = new Set(['application/rss+xml', 'application/atom+xml']);
// Options to be passed to `addDependency` for certain tags + attributes
const OPTIONS = {
a: {
href: {needsStableName: true},
},
iframe: {
src: {needsStableName: true},
},
link(attrs) {
if (attrs.rel === 'stylesheet') {
return {
// Keep in the same bundle group as the HTML.
priority: 'parallel',
};
}
},
};
function collectSrcSetDependencies(asset, srcset, opts) {
let newSources = [];
for (const source of srcset.split(',')) {
let pair = source.trim().split(' ');
if (pair.length === 0) {
continue;
}
pair[0] = asset.addURLDependency(pair[0], opts);
newSources.push(pair.join(' '));
}
return newSources.join(',');
}
function getAttrDepHandler(attr) {
if (attr === 'srcset' || attr === 'imagesrcset') {
return collectSrcSetDependencies;
}
return (asset, src, opts) => asset.addURLDependency(src, opts);
}
export default function collectDependencies(
asset: MutableAsset,
ast: AST,
): boolean {
let isDirty = false;
let hasScripts = false;
let seen = new Set();
const errors = [];
PostHTML().walk.call(ast.program, node => {
let {tag, attrs} = node;
if (!attrs || seen.has(node)) {
return node;
}
seen.add(node);
if (tag === 'meta') {
const isMetaDependency = Object.keys(attrs).some(attr => {
let values = META[attr];
return (
values &&
values.includes(attrs[attr]) &&
attrs.content !== '' &&
!(attrs.name === 'msapplication-config' && attrs.content === 'none')
);
});
if (isMetaDependency) {
const metaAssetUrl = attrs.content;
if (metaAssetUrl) {
attrs.content = asset.addURLDependency(attrs.content, {
needsStableName: true,
});
isDirty = true;
asset.setAST(ast);
}
}
return node;
}
if (
tag === 'link' &&
(attrs.rel === 'canonical' ||
attrs.rel === 'manifest' ||
(attrs.rel === 'alternate' && FEED_TYPES.has(attrs.type))) &&
attrs.href
) {
let href = attrs.href;
if (attrs.rel === 'manifest') {
// A hack to allow manifest.json rather than manifest.webmanifest.
// If a custom pipeline is used, it is responsible for running @parcel/transformer-webmanifest.
if (!href.includes(':')) {
href = 'webmanifest:' + href;
}
}
attrs.href = asset.addURLDependency(href, {
needsStableName: true,
});
isDirty = true;
asset.setAST(ast);
return node;
}
if (tag === 'script' && attrs.src) {
let sourceType = attrs.type === 'module' ? 'module' : 'script';
let loc = node.location
? {
filePath: asset.filePath,
start: node.location.start,
end: node.location.end,
}
: undefined;
let outputFormat = 'global';
if (attrs.type === 'module' && asset.env.shouldScopeHoist) {
outputFormat = 'esmodule';
} else {
if (attrs.type === 'module') {
attrs.defer = '';
}
delete attrs.type;
}
// If this is a <script type="module">, and not all of the browser targets support ESM natively,
// add a copy of the script tag with a nomodule attribute.
let copy: ?PostHTMLNode;
if (
outputFormat === 'esmodule' &&
!asset.env.supports('esmodules', true)
) {
let attrs = Object.assign({}, node.attrs);
copy = {...node, attrs};
delete attrs.type;
attrs.nomodule = '';
attrs.defer = '';
attrs.src = asset.addURLDependency(attrs.src, {
// Keep in the same bundle group as the HTML.
priority: 'parallel',
bundleBehavior:
sourceType === 'script' || attrs.async != null
? 'isolated'
: undefined,
env: {
sourceType,
outputFormat: 'global',
loc,
},
});
seen.add(copy);
}
attrs.src = asset.addURLDependency(attrs.src, {
// Keep in the same bundle group as the HTML.
priority: 'parallel',
// If the script is async it can be executed in any order, so it cannot depend
// on any sibling scripts for dependencies. Keep all dependencies together.
// Also, don't share dependencies between classic scripts and nomodule scripts
// because nomodule scripts won't run when modules are supported.
bundleBehavior:
sourceType === 'script' || attrs.async != null
? 'isolated'
: undefined,
env: {
sourceType,
outputFormat,
loc,
},
});
asset.setAST(ast);
hasScripts = true;
return copy ? [node, copy] : node;
}
for (let attr in attrs) {
// Check for virtual paths
if (tag === 'a' && attrs[attr].split('#')[0].lastIndexOf('.') < 1) {
continue;
}
// Check for id references
if (attrs[attr][0] === '#') {
continue;
}
let elements = ATTRS[attr];
if (elements && elements.includes(node.tag)) {
// Check for empty string
if (attrs[attr].length === 0) {
errors.push({
message: `'${attr}' should not be empty string`,
filePath: asset.filePath,
loc: node.location,
});
}
let depHandler = getAttrDepHandler(attr);
let depOptionsHandler = OPTIONS[node.tag];
let depOptions =
typeof depOptionsHandler === 'function'
? depOptionsHandler(attrs, asset.env)
: depOptionsHandler && depOptionsHandler[attr];
attrs[attr] = depHandler(asset, attrs[attr], depOptions);
isDirty = true;
}
}
if (isDirty) {
asset.setAST(ast);
}
return node;
});
if (errors.length > 0) {
throw errors;
}
return hasScripts;
}