forked from embroider-build/embroider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template-transform-plugin.ts
42 lines (35 loc) · 1.45 KB
/
template-transform-plugin.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
import make from '@embroider/core/src/babel-plugin-stage1-inline-hbs';
import { TemplateCompiler, TemplateCompilerParams } from '@embroider/core';
import { getEmberExports } from '@embroider/core/src/load-ember-template-compiler';
export type TemplateTransform = () => { name: string; visitor: {} };
export type TemplateTransformPlugin = TemplateTransform | string;
export interface Options {
// An array of either Glimmer AST plugins or paths that can be resolved to a plugin.
astTransforms?: TemplateTransformPlugin[];
// Defaults to 'ember-source/dist/ember-template-compiler'
compilerPath?: string;
}
function resolvePlugins(plugins: TemplateTransformPlugin[]) {
return plugins.map((somePlugin: TemplateTransformPlugin) => {
// If it's a string attempt to resolve the path to a module.
return typeof somePlugin === 'string'
? require(somePlugin) // eslint-disable-line @typescript-eslint/no-require-imports
: somePlugin;
});
}
export default make((options: Options) => {
let {
astTransforms: somePlugins = [],
compilerPath = 'ember-source/dist/ember-template-compiler',
} = options;
compilerPath = require.resolve(compilerPath);
const astTransforms: TemplateTransform[] = resolvePlugins(somePlugins);
const params: TemplateCompilerParams = {
EmberENV: {},
loadEmberTemplateCompiler: () => getEmberExports(compilerPath),
plugins: {
ast: astTransforms,
},
};
return new TemplateCompiler(params);
});