-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
compile.ts
37 lines (32 loc) · 1.4 KB
/
compile.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
/**
@module ember
*/
import { precompileJSON } from '@glimmer/compiler';
import type { SerializedTemplateWithLazyBlock, TemplateFactory } from '@glimmer/interfaces';
import { templateFactory } from '@glimmer/opcode-compiler';
import type { EmberPrecompileOptions } from 'ember-template-compiler';
import { compileOptions } from 'ember-template-compiler';
/**
Uses HTMLBars `compile` function to process a string into a compiled template.
This is not present in production builds.
@private
@method compile
@param {String} templateString This is the string to be compiled by HTMLBars.
@param {Object} options This is an options hash to augment the compiler options.
*/
export default function compile(
templateSource: string,
options: Partial<EmberPrecompileOptions> = {},
scopeValues: Record<string, unknown> = {}
): TemplateFactory {
options.locals = options.locals ?? Object.keys(scopeValues ?? {});
let [block, usedLocals] = precompileJSON(templateSource, compileOptions(options));
let reifiedScopeValues = usedLocals.map((key) => scopeValues[key]);
let templateBlock: SerializedTemplateWithLazyBlock = {
block: JSON.stringify(block),
moduleName: options.moduleName ?? options.meta?.moduleName ?? '(unknown template module)',
scope: reifiedScopeValues.length > 0 ? () => reifiedScopeValues : null,
isStrictMode: options.strictMode ?? false,
};
return templateFactory(templateBlock);
}