/
HTMLTransformer.js
127 lines (110 loc) Β· 3.06 KB
/
HTMLTransformer.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
// @flow
import {Transformer} from '@parcel/plugin';
import type {AST} from '@parcel/types';
import {parser as parse} from 'posthtml-parser';
import nullthrows from 'nullthrows';
import type {PostHTMLExpression, PostHTMLNode} from 'posthtml';
import PostHTML from 'posthtml';
import {render} from 'posthtml-render';
import semver from 'semver';
import collectDependencies from './dependencies';
import extractInlineAssets from './inline';
import ThrowableDiagnostic from '@parcel/diagnostic';
export default (new Transformer({
canReuseAST({ast}) {
return ast.type === 'posthtml' && semver.satisfies(ast.version, '^0.4.0');
},
async parse({asset}) {
return {
type: 'posthtml',
version: '0.4.1',
program: parse(await asset.getCode(), {
lowerCaseTags: true,
lowerCaseAttributeNames: true,
sourceLocations: true,
xmlMode: asset.type === 'xhtml',
}),
};
},
async transform({asset, options}) {
if (asset.type === 'htm') {
asset.type = 'html';
}
asset.bundleBehavior = 'isolated';
let ast = nullthrows(await asset.getAST());
let hasScripts;
try {
hasScripts = collectDependencies(asset, ast);
} catch (errors) {
if (Array.isArray(errors)) {
throw new ThrowableDiagnostic({
diagnostic: errors.map(error => ({
message: error.message,
origin: '@parcel/transformer-html',
codeFrames: [
{
filePath: error.filePath,
language: 'html',
codeHighlights: [error.loc],
},
],
})),
});
}
throw errors;
}
const {assets: inlineAssets, hasScripts: hasInlineScripts} =
extractInlineAssets(asset, ast);
const result = [asset, ...inlineAssets];
// empty <script></script> is added to make sure HMR is working even if user
// didn't add any.
if (options.hmrOptions && !(hasScripts || hasInlineScripts)) {
const script = {
tag: 'script',
attrs: {
src: asset.addURLDependency('hmr.js', {
priority: 'parallel',
}),
},
content: [],
};
const found = findFirstMatch(ast, [{tag: 'body'}, {tag: 'html'}]);
if (found) {
found.content = found.content || [];
found.content.push(script);
} else {
// Insert at the very end.
ast.program.push(script);
}
asset.setAST(ast);
result.push({
type: 'js',
content: '',
uniqueKey: 'hmr.js',
});
}
return result;
},
generate({ast, asset}) {
return {
content: render(ast.program, {
closingSingleTag: asset.type === 'xhtml' ? 'slash' : undefined,
}),
};
},
}): Transformer);
function findFirstMatch(
ast: AST,
expressions: PostHTMLExpression[],
): ?PostHTMLNode {
let found;
for (const expression of expressions) {
PostHTML().match.call(ast.program, expression, node => {
found = node;
return node;
});
if (found) {
return found;
}
}
}