Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
NullVoxPopuli committed Oct 7, 2024
1 parent f5f2506 commit 077f80e
Show file tree
Hide file tree
Showing 3 changed files with 12,809 additions and 9,782 deletions.
46 changes: 1 addition & 45 deletions packages/@ember/template-compiler/index.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1 @@
import { setComponentTemplate } from '@ember/component';
import templateOnly from '@ember/component/template-only';
import { precompileTemplate } from '@ember/template-compilation';
// TODO: internalize these types and re-export from @glint/template.
// @ts-expect-error these types are too useful to be external
import type { ComponentLike } from '@glint/template';

interface Params<ComponentClass extends abstract new (...args: any) => any> {
component?: ComponentClass;
strict?: boolean;
moduleName?: string;
eval?: () => Record<string, unknown>;
scope?: (
instance: ComponentClass extends ComponentLike<any> ? InstanceType<ComponentClass> : never
) => Record<string, unknown>;
}

export function template<Signature>(
templateContent: string,
params?: Params<never>
): ComponentLike<Signature>;

export function template<ComponentDefinition extends ComponentLike<any>>(
templateContent: string,
params: Params<ComponentDefinition>
): ComponentDefinition;

export function template(templateContent: string, config?: Params<ComponentLike<any>>) {
let strictMode = config?.strict ?? true;

let compiled = precompileTemplate(templateContent, {
strictMode,
// TODO: this implementation needs updated,
// because *this* scope doesn't pass the instance argument back.
// This is needed for private variable access.
// @ts-expect-error see above
scope: config?.scope,
});

let context = config?.component ? config.component : templateOnly();

setComponentTemplate(compiled, context);

return context;
}
export { template } from './runtime';
48 changes: 48 additions & 0 deletions packages/@ember/template-compiler/runtime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { setComponentTemplate } from '@ember/component';
import templateOnly from '@ember/component/template-only';
import { precompile as glimmerPrecomile } from '@glimmer/compiler';
import type { ComponentLike } from '@glint/template';
import { default as compileOptions } from 'ember-template-compiler/lib/system/compile-options';

interface Params<ComponentClass extends abstract new (...args: any) => any> {
component?: ComponentClass;
strict?: boolean;
moduleName?: string;
eval?: () => Record<string, unknown>;
scope?: (
instance: ComponentClass extends ComponentLike<any> ? InstanceType<ComponentClass> : never
) => Record<string, unknown>;
}

export function template<Signature>(
templateContent: string,
params?: Params<never>
): ComponentLike<Signature>;

export function template<ComponentDefinition extends ComponentLike<any>>(
templateContent: string,
params: Params<ComponentDefinition>
): ComponentDefinition;

export function template(templateContent: string, config?: Params<ComponentLike<any>>) {
let strictMode = config?.strict ?? true;

let compiled = glimmerPrecomile(
templateContent,
// TODO: this implementation needs updated,
// because *this* scope doesn't pass the instance argument back.
// This is needed for private variable access.
{
...compileOptions({}),
strictMode,
// @ts-expect-error, see above
scope: config?.scope,
}
);

let context = config?.component ? config.component : templateOnly();

setComponentTemplate(compiled, context);

return context;
}
Loading

0 comments on commit 077f80e

Please sign in to comment.