Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions packages/core/src/config/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,15 @@ export class GlintEnvironment {
* they support.
*/
public getStandaloneTemplateConfig():
| Pick<GlintTemplateConfig, 'typesModule' | 'specialForms'>
| Pick<
GlintTemplateConfig,
'typesModule' | 'specialForms' | 'preprocess' | 'postprocessAst' | 'mapTemplateContent'
>
| undefined {
if (this.standaloneTemplateConfig) {
let { typesModule, specialForms } = this.standaloneTemplateConfig;
return { typesModule, specialForms };
let { typesModule, specialForms, preprocess, postprocessAst, mapTemplateContent } =
this.standaloneTemplateConfig;
return { typesModule, specialForms, preprocess, postprocessAst, mapTemplateContent };
}
}

Expand Down Expand Up @@ -189,7 +193,6 @@ function loadMergedEnvironmentConfig(
}

let config = envFunction(envUserConfig ?? {}) as GlintEnvironmentConfig;

if (config.template) {
if (template) {
throw new SilentError(
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/config/types.cts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type * as ts from 'typescript';
import { AST } from '@glimmer/syntax';

// This file is explicitly `.cts` so that environment packages written
// in CJS can import its types from `@glint/core/config-types`.
Expand Down Expand Up @@ -96,6 +97,12 @@ export type PathCandidateWithDeferral = {
};

export type GlintTemplateConfig = {
preprocess?: (
templateInfo: { contents: string; filename: string },
args: { globals?: string[]; preamble?: string[] }
) => { globals: string[]; preamble: string[]; template: string };
postprocessAst?: (ast: AST.Template) => AST.Template;
mapTemplateContent?: Record<string, any>;
typesModule: string;
specialForms?: { [global: string]: GlintSpecialForm };
getPossibleTemplatePaths(scriptPath: string): Array<PathCandidate>;
Expand Down
13 changes: 10 additions & 3 deletions packages/core/src/transform/template/inlining/companion-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@ export function calculateCompanionTemplateSpans(
return { errors, directives, partialSpans };
}

let { typesModule, specialForms } = templateConfig;
let { typesModule, specialForms, preprocess, postprocessAst, mapTemplateContent } =
templateConfig;
let useJsDoc = environment.isUntypedScript(script.filename);
let targetNode = findCompanionTemplateTarget(ts, ast);
if (targetNode && ts.isClassLike(targetNode)) {
let rewriteResult = templateToTypescript(template.contents, {
let rewriteResult = templateToTypescript(template, {
preprocess,
postprocessAst,
mapTemplateContent,
typesModule,
specialForms,
useJsDoc,
Expand All @@ -56,7 +60,10 @@ export function calculateCompanionTemplateSpans(
: `({} as unknown as typeof import('./${moduleName}').default)`;
}

let rewriteResult = templateToTypescript(template.contents, {
let rewriteResult = templateToTypescript(template, {
preprocess,
postprocessAst,
mapTemplateContent,
typesModule,
backingValue,
specialForms,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export type EmbeddingSyntax = {

export type MapTemplateContentsOptions = {
embeddingSyntax: EmbeddingSyntax;
postprocessAst?: (ast: AST.Template) => AST.Template;
};

/**
Expand All @@ -128,14 +129,15 @@ export type MapTemplateContentsOptions = {
*/
export function mapTemplateContents(
template: string,
{ embeddingSyntax }: MapTemplateContentsOptions,
{ embeddingSyntax, postprocessAst }: MapTemplateContentsOptions,
callback: (ast: AST.Template | null, mapper: Mapper) => void
): RewriteResult {
let ast: AST.Template | null = null;
let errors: Array<{ message: string; location: Range | undefined }> = [];
let lineOffsets = calculateLineOffsets(template, embeddingSyntax.prefix.length);
try {
ast = preprocess(template);
ast = postprocessAst?.(ast) || ast;
} catch (error) {
let message = getErrorMessage(error);
let location: Range | undefined;
Expand Down
Loading