-
Notifications
You must be signed in to change notification settings - Fork 142
/
babel-plugin-inline-hbs.ts
232 lines (214 loc) · 7.48 KB
/
babel-plugin-inline-hbs.ts
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
import {
TaggedTemplateExpression,
CallExpression,
templateLiteral,
templateElement,
ExpressionStatement,
stringLiteral,
File,
Program,
functionExpression,
blockStatement,
throwStatement,
newExpression,
} from '@babel/types';
import { NodePath } from '@babel/traverse';
import { join } from 'path';
import { TemplateCompiler, TemplateCompilerParams } from './template-compiler';
import { identifier, callExpression, memberExpression } from '@babel/types';
import { parse } from '@babel/core';
import { ResolvedDep } from './resolver';
import { importDeclaration } from '@babel/types';
import { importDefaultSpecifier } from '@babel/types';
import { expressionStatement } from '@babel/types';
import { returnStatement } from '@babel/types';
// These are the known names that people are using to import the `hbs` macro
// from. In theory the original plugin lets people customize these names, but
// that is a terrible idea.
const modulePaths = [
['htmlbars-inline-precompile', 'default'],
['ember-cli-htmlbars-inline-precompile', 'default'],
['ember-cli-htmlbars', 'hbs'],
];
interface State {
opts: {
templateCompiler: TemplateCompilerParams;
// the stages here correspond to the two places in the overall Embroider
// architecture that this transform applies. In stage1 HBS stays as HBS, but
// we still need to run any custom AST transforms inside that HBS. In
// stage3, we are running more like the traditional
// ember-cli-htmlbars-inline-precompile by compiling the HBS to Javascript.
stage: 1 | 3;
};
file: {
code: string;
opts: {
filename: string;
};
};
dependencies: Map<string, ResolvedDep>;
templateCompiler: TemplateCompiler | undefined;
}
export type Params = State['opts'];
export default function inlineHBSTransform(): unknown {
return {
visitor: {
Program: {
enter(_: NodePath, state: State) {
state.dependencies = new Map();
},
exit(path: NodePath<Program>, state: State) {
if (state.opts.stage === 3) {
pruneImports(path);
}
let counter = 0;
for (let dep of state.dependencies.values()) {
path.node.body.unshift(amdDefine(dep.runtimeName, counter));
path.node.body.unshift(
importDeclaration([importDefaultSpecifier(identifier(`a${counter++}`))], stringLiteral(dep.path))
);
}
},
},
TaggedTemplateExpression(path: NodePath<TaggedTemplateExpression>, state: State) {
for (let [modulePath, identifier] of modulePaths) {
if (path.get('tag').referencesImport(modulePath, identifier)) {
handleTagged(path, state);
}
}
},
CallExpression(path: NodePath<CallExpression>, state: State) {
for (let [modulePath, identifier] of modulePaths) {
if (path.get('callee').referencesImport(modulePath, identifier)) {
handleCalled(path, state);
}
}
},
},
};
}
inlineHBSTransform._parallelBabel = {
requireFile: __filename,
};
inlineHBSTransform.baseDir = function () {
return join(__dirname, '..');
};
function handleTagged(path: NodePath<TaggedTemplateExpression>, state: State) {
if (path.node.quasi.expressions.length) {
throw path.buildCodeFrameError('placeholders inside a tagged template string are not supported');
}
let template = path.node.quasi.quasis.map(quasi => quasi.value.cooked).join('');
if (state.opts.stage === 1) {
let compiled = compiler(state).applyTransforms(state.file.opts.filename, template);
path.get('quasi').replaceWith(templateLiteral([templateElement({ raw: compiled, cooked: compiled })], []));
} else {
let { compiled, dependencies } = compiler(state).precompile(state.file.opts.filename, template);
for (let dep of dependencies) {
state.dependencies.set(dep.runtimeName, dep);
}
let func = memberExpression(memberExpression(identifier('Ember'), identifier('HTMLBars')), identifier('template'));
path.replaceWith(callExpression(func, [jsonLiteral(compiled)]));
}
}
function handleCalled(path: NodePath<CallExpression>, state: State) {
let { template, insertRuntimeErrors } = getCallArguments(path);
let compilerInstance = compiler(state);
if (state.opts.stage === 1) {
let compiled: string;
try {
compiled = compilerInstance.applyTransforms(state.file.opts.filename, template);
} catch (err) {
if (insertRuntimeErrors) {
// in stage 1 we just leave the bad template in place (we were only
// trying to run transforms and re-emit hbs), so that it will be handled
// at stage3 instead.
return;
}
throw err;
}
(path.get('arguments')[0] as NodePath).replaceWith(stringLiteral(compiled));
} else {
let result: ReturnType<TemplateCompiler['precompile']>;
try {
result = compilerInstance.precompile(state.file.opts.filename, template);
} catch (err) {
if (insertRuntimeErrors) {
path.replaceWith(
callExpression(
functionExpression(
null,
[],
blockStatement([throwStatement(newExpression(identifier('Error'), [stringLiteral(err.message)]))])
),
[]
)
);
return;
}
throw err;
}
let { compiled, dependencies } = result;
for (let dep of dependencies) {
state.dependencies.set(dep.runtimeName, dep);
}
let func = memberExpression(memberExpression(identifier('Ember'), identifier('HTMLBars')), identifier('template'));
path.replaceWith(callExpression(func, [jsonLiteral(compiled)]));
}
}
function pruneImports(path: NodePath) {
if (!path.isProgram()) {
return;
}
for (let topLevelPath of path.get('body')) {
if (topLevelPath.isImportDeclaration()) {
let modulePath = topLevelPath.get('source').node.value;
if (modulePaths.find(p => p[0] === modulePath)) {
topLevelPath.remove();
}
}
}
}
function jsonLiteral(value: unknown | undefined) {
if (typeof value === 'undefined') {
return identifier('undefined');
}
let ast = parse(`a(${value})`, {}) as File;
let statement = ast.program.body[0] as ExpressionStatement;
let expression = statement.expression as CallExpression;
return expression.arguments[0];
}
function compiler(state: State) {
if (!state.templateCompiler) {
state.templateCompiler = new TemplateCompiler(state.opts.templateCompiler);
}
return state.templateCompiler;
}
function amdDefine(runtimeName: string, importCounter: number) {
return expressionStatement(
callExpression(memberExpression(identifier('window'), identifier('define')), [
stringLiteral(runtimeName),
functionExpression(null, [], blockStatement([returnStatement(identifier(`a${importCounter}`))])),
])
);
}
function getCallArguments(path: NodePath<CallExpression>): { template: string; insertRuntimeErrors: boolean } {
let [template, options] = path.node.arguments;
if (template?.type !== 'StringLiteral') {
throw path.buildCodeFrameError('hbs accepts only a string literal argument');
}
let insertRuntimeErrors =
options?.type === 'ObjectExpression' &&
options.properties.some(
prop =>
prop.type === 'ObjectProperty' &&
prop.computed === false &&
prop.key.type === 'Identifier' &&
prop.key.name === 'insertRuntimeErrors' &&
prop.value.type === 'BooleanLiteral' &&
prop.value.value
);
return {
template: template.value,
insertRuntimeErrors,
};
}