-
Notifications
You must be signed in to change notification settings - Fork 137
/
hbs-to-js.ts
36 lines (33 loc) · 1.08 KB
/
hbs-to-js.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
import jsStringEscape from 'js-string-escape';
import { sep } from 'path';
export interface Options {
filename?: string;
// this is a backward-compatibility feature that allows us to show old AST
// transforms the moduleName format they expect.
compatModuleNaming?: {
// the app root
rootDir: string;
// the app's module name
modulePrefix: string;
};
}
export function hbsToJS(hbsContents: string, options?: Options): string {
let optsSource = '';
if (options?.filename) {
let filename = options.filename;
let { compatModuleNaming: renaming } = options;
if (renaming) {
if (filename.startsWith(renaming.rootDir)) {
filename = renaming.modulePrefix + filename.slice(renaming.rootDir.length);
}
if (sep !== '/') {
filename = filename.replace(/\\/g, '/');
}
}
optsSource = `,{ moduleName: "${jsStringEscape(filename)}" }`;
}
return [
`import { precompileTemplate } from "@ember/template-compilation";`,
`export default precompileTemplate("${jsStringEscape(hbsContents)}"${optsSource})`,
].join('\n');
}