forked from parcel-bundler/parcel
/
HTMLPackager.js
251 lines (220 loc) Β· 6.69 KB
/
HTMLPackager.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
// @flow strict-local
import type {Bundle, BundleGraph, NamedBundle} from '@parcel/types';
import assert from 'assert';
import {Readable} from 'stream';
import {Packager} from '@parcel/plugin';
import {setDifference} from '@parcel/utils';
import posthtml from 'posthtml';
import {
bufferStream,
replaceInlineReferences,
replaceURLReferences,
urlJoin,
} from '@parcel/utils';
import nullthrows from 'nullthrows';
// https://www.w3.org/TR/html5/dom.html#metadata-content-2
const metadataContent = new Set([
'base',
'link',
'meta',
'noscript',
// 'script', // retain script order (somewhat)
'style',
'template',
'title',
]);
export default (new Packager({
async loadConfig({config}) {
let posthtmlConfig = await config.getConfig(
['.posthtmlrc', '.posthtmlrc.js', 'posthtml.config.js'],
{
packageKey: 'posthtml',
},
);
return {
render: posthtmlConfig?.contents?.render,
};
},
async package({bundle, bundleGraph, getInlineBundleContents, config}) {
let assets = [];
bundle.traverseAssets(asset => {
assets.push(asset);
});
assert.equal(assets.length, 1, 'HTML bundles must only contain one asset');
let asset = assets[0];
let code = await asset.getCode();
// Add bundles in the same bundle group that are not inline. For example, if two inline
// bundles refer to the same library that is extracted into a shared bundle.
let referencedBundles = [
...setDifference(
new Set(bundleGraph.getReferencedBundles(bundle)),
new Set(bundleGraph.getReferencedBundles(bundle, {recursive: false})),
),
];
let renderConfig = config?.render;
let {html} = await posthtml([
tree => insertBundleReferences(referencedBundles, tree),
tree =>
replaceInlineAssetContent(bundleGraph, getInlineBundleContents, tree),
]).process(code, {
...renderConfig,
xmlMode: bundle.type === 'xhtml',
closingSingleTag: bundle.type === 'xhtml' ? 'slash' : undefined,
});
let {contents, map} = replaceURLReferences({
bundle,
bundleGraph,
contents: html,
relative: false,
getReplacement: contents => contents.replace(/"/g, '"'),
});
return replaceInlineReferences({
bundle,
bundleGraph,
contents,
getInlineBundleContents,
getInlineReplacement: (dep, inlineType, contents) => ({
from: dep.id,
to: contents.replace(/"/g, '"').trim(),
}),
map,
});
},
}): Packager);
async function getAssetContent(
bundleGraph: BundleGraph<NamedBundle>,
getInlineBundleContents,
assetId,
) {
let inlineBundle: ?Bundle;
bundleGraph.traverseBundles((bundle, context, {stop}) => {
let entryAssets = bundle.getEntryAssets();
if (entryAssets.some(a => a.uniqueKey === assetId)) {
inlineBundle = bundle;
stop();
}
});
if (inlineBundle) {
const bundleResult = await getInlineBundleContents(
inlineBundle,
bundleGraph,
);
return {bundle: inlineBundle, contents: bundleResult.contents};
}
return null;
}
async function replaceInlineAssetContent(
bundleGraph: BundleGraph<NamedBundle>,
getInlineBundleContents,
tree,
) {
const inlineNodes = [];
tree.walk(node => {
if (node.attrs && node.attrs['data-parcel-key']) {
inlineNodes.push(node);
}
return node;
});
for (let node of inlineNodes) {
let newContent = await getAssetContent(
bundleGraph,
getInlineBundleContents,
node.attrs['data-parcel-key'],
);
if (newContent != null) {
let {contents, bundle} = newContent;
node.content = (
contents instanceof Readable ? await bufferStream(contents) : contents
).toString();
if (
node.tag === 'script' &&
nullthrows(bundle).env.outputFormat === 'esmodule'
) {
node.attrs.type = 'module';
}
// Escape closing script tags and HTML comments in JS content.
// https://www.w3.org/TR/html52/semantics-scripting.html#restrictions-for-contents-of-script-elements
// Avoid replacing </script with <\/script as it would break the following valid JS: 0</script/ (i.e. regexp literal).
// Instead, escape the s character.
if (node.tag === 'script') {
node.content = node.content
.replace(/<!--/g, '<\\!--')
.replace(/<\/(script)/gi, '</\\$1');
}
// Escape closing style tags in CSS content.
if (node.tag === 'style') {
node.content = node.content.replace(/<\/(style)/gi, '<\\/$1');
}
// remove attr from output
delete node.attrs['data-parcel-key'];
}
}
return tree;
}
function insertBundleReferences(siblingBundles, tree) {
const bundles = [];
for (let bundle of siblingBundles) {
if (bundle.type === 'css') {
bundles.push({
tag: 'link',
attrs: {
rel: 'stylesheet',
href: urlJoin(bundle.target.publicUrl, bundle.name),
},
});
} else if (bundle.type === 'js') {
let nomodule =
bundle.env.outputFormat !== 'esmodule' &&
bundle.env.sourceType === 'module' &&
bundle.env.shouldScopeHoist;
bundles.push({
tag: 'script',
attrs: {
type: bundle.env.outputFormat === 'esmodule' ? 'module' : undefined,
nomodule: nomodule ? '' : undefined,
defer: nomodule ? '' : undefined,
src: urlJoin(bundle.target.publicUrl, bundle.name),
},
});
}
}
addBundlesToTree(bundles, tree);
}
function addBundlesToTree(bundles, tree) {
const main = find(tree, 'head') || find(tree, 'html');
const content = main ? main.content || (main.content = []) : tree;
const index = findBundleInsertIndex(content);
content.splice(index, 0, ...bundles);
}
function find(tree, tag) {
let res;
tree.match({tag}, node => {
res = node;
return node;
});
return res;
}
function findBundleInsertIndex(content) {
// HTML document order (https://html.spec.whatwg.org/multipage/syntax.html#writing)
// - Any number of comments and ASCII whitespace.
// - A DOCTYPE.
// - Any number of comments and ASCII whitespace.
// - The document element, in the form of an html element.
// - Any number of comments and ASCII whitespace.
//
// -> Insert before first non-metadata (or script) element; if none was found, after the doctype
let doctypeIndex;
for (let index = 0; index < content.length; index++) {
const node = content[index];
if (node && node.tag && !metadataContent.has(node.tag)) {
return index;
}
if (
typeof node === 'string' &&
node.toLowerCase().startsWith('<!doctype')
) {
doctypeIndex = index;
}
}
return doctypeIndex ? doctypeIndex + 1 : 0;
}