Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add template only name #58

Closed
wants to merge 7 commits into from
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
26 changes: 13 additions & 13 deletions __tests__/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1468,7 +1468,7 @@ describe('htmlbars-inline-precompile', function () {
import { precompileTemplate } from "@ember/template-compilation";
import { setComponentTemplate } from "@ember/component";
import templateOnly from "@ember/component/template-only";
export default setComponentTemplate(precompileTemplate('<HelloWorld @color={{"#ff0000"}} />', { scope: () => ({ HelloWorld }), strictMode: true }), templateOnly());
export default setComponentTemplate(precompileTemplate('<HelloWorld @color={{"#ff0000"}} />', { scope: () => ({ HelloWorld }), strictMode: true }), templateOnly(undefined, "foo-bar"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be PascalCase, since it's what would get shown in the inspector, and how it would be invoked by a consumer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inspector would auto transform it to pascal case

Copy link
Contributor Author

@patricklx patricklx Jun 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

inspector:
image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it also doesn't matter if it starts with lower case. so my-template-only-2 would also be shown as MyTemplateOnly2

`);
});

Expand Down Expand Up @@ -1599,7 +1599,7 @@ describe('htmlbars-inline-precompile', function () {
precompileTemplate("Icon", {
strictMode: true,
}),
templateOnly()
templateOnly(undefined, "foo-bar:Icon")
);
`);
});
Expand All @@ -1626,7 +1626,7 @@ describe('htmlbars-inline-precompile', function () {
import { precompileTemplate } from "@ember/template-compilation";
import { setComponentTemplate } from "@ember/component";
import templateOnly from "@ember/component/template-only";
export default setComponentTemplate(precompileTemplate('<HelloWorld />', { strictMode: false, scope: () => ({ HelloWorld }) }), templateOnly());
export default setComponentTemplate(precompileTemplate('<HelloWorld />', { strictMode: false, scope: () => ({ HelloWorld }) }), templateOnly(undefined, "foo-bar"));
`);
});
});
Expand Down Expand Up @@ -1698,7 +1698,7 @@ describe('htmlbars-inline-precompile', function () {
scope: () => [HelloWorld],
isStrictMode: true,
}
), templateOnly());
), templateOnly(undefined, "foo-bar"));
`);
});

Expand Down Expand Up @@ -1897,7 +1897,7 @@ describe('htmlbars-inline-precompile', function () {
import { precompileTemplate } from "@ember/template-compilation";
import { setComponentTemplate } from "@ember/component";
import templateOnly from "@ember/component/template-only";
export default setComponentTemplate(precompileTemplate('<HelloWorld />', { strictMode: true, scope: () => ({ HelloWorld }) }), templateOnly());
export default setComponentTemplate(precompileTemplate('<HelloWorld />', { strictMode: true, scope: () => ({ HelloWorld }) }), templateOnly(undefined, "foo-bar"));
`);
});

Expand Down Expand Up @@ -1928,7 +1928,7 @@ describe('htmlbars-inline-precompile', function () {
import { setComponentTemplate } from "@ember/component";
import templateOnly from "@ember/component/template-only";
let div = 1;
export default setComponentTemplate(precompileTemplate('<div></div>', { strictMode: true, scope: () => ({ div })}), templateOnly());
export default setComponentTemplate(precompileTemplate('<div></div>', { strictMode: true, scope: () => ({ div })}), templateOnly(undefined, "foo-bar"));
`);
});

Expand All @@ -1955,7 +1955,7 @@ describe('htmlbars-inline-precompile', function () {
import { setComponentTemplate } from "@ember/component";
import templateOnly from "@ember/component/template-only";
let hasBlock = 1;
export default setComponentTemplate(precompileTemplate('{{hasBlock "thing"}}', { strictMode: true, scope: () => ({ hasBlock }) }), templateOnly());
export default setComponentTemplate(precompileTemplate('{{hasBlock "thing"}}', { strictMode: true, scope: () => ({ hasBlock }) }), templateOnly(undefined, "foo-bar"));
`);
});

Expand All @@ -1980,7 +1980,7 @@ describe('htmlbars-inline-precompile', function () {
import { precompileTemplate } from "@ember/template-compilation";
import { setComponentTemplate } from "@ember/component";
import templateOnly from "@ember/component/template-only";
export default setComponentTemplate(precompileTemplate('{{hasBlock "thing"}}', { strictMode: true }), templateOnly());
export default setComponentTemplate(precompileTemplate('{{hasBlock "thing"}}', { strictMode: true }), templateOnly(undefined, "foo-bar"));
`);
});

Expand Down Expand Up @@ -2020,7 +2020,7 @@ describe('htmlbars-inline-precompile', function () {
isStrictMode: true,
}
),
templateOnly()
templateOnly(undefined, "foo-bar")
);
`);
});
Expand Down Expand Up @@ -2049,7 +2049,7 @@ describe('htmlbars-inline-precompile', function () {
import { precompileTemplate } from "@ember/template-compilation";
import { setComponentTemplate } from "@ember/component";
import templateOnly from "@ember/component/template-only";
export default setComponentTemplate(precompileTemplate('<HelloWorld />', { strictMode: true, scope: () => ({ HelloWorld }) }), templateOnly());
export default setComponentTemplate(precompileTemplate('<HelloWorld />', { strictMode: true, scope: () => ({ HelloWorld }) }), templateOnly(undefined, "foo-bar"));
`);
});

Expand Down Expand Up @@ -2081,7 +2081,7 @@ describe('htmlbars-inline-precompile', function () {
import templateOnly from "@ember/component/template-only";
export default function() {
let { HelloWorld } = globalThis;
return setComponentTemplate(precompileTemplate('<HelloWorld />', { strictMode: true, scope: () => ({ HelloWorld })}), templateOnly());
return setComponentTemplate(precompileTemplate('<HelloWorld />', { strictMode: true, scope: () => ({ HelloWorld })}), templateOnly(undefined, undefined));
}
`);
});
Expand Down Expand Up @@ -2133,7 +2133,7 @@ describe('htmlbars-inline-precompile', function () {
isStrictMode: true,
}
),
templateOnly()
templateOnly(undefined, "foo-bar")
);
`);
});
Expand Down Expand Up @@ -2166,7 +2166,7 @@ describe('htmlbars-inline-precompile', function () {
import { precompileTemplate } from "@ember/template-compilation";
import { setComponentTemplate } from "@ember/component";
import templateOnly from "@ember/component/template-only";
const MyComponent = setComponentTemplate(precompileTemplate('<HelloWorld />', { strictMode: true, scope: () => ({ HelloWorld }) }), templateOnly());
const MyComponent = setComponentTemplate(precompileTemplate('<HelloWorld />', { strictMode: true, scope: () => ({ HelloWorld }) }), templateOnly(undefined, "foo-bar:MyComponent"));
`);
});

Expand Down
44 changes: 42 additions & 2 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { EmberTemplateCompiler, PreprocessOptions } from './ember-template-
import { LegacyModuleName } from './public-types';
import { ScopeLocals } from './scope-locals';
import { ASTPluginBuilder, preprocess, print } from '@glimmer/syntax';
import { basename, extname } from 'path';

export * from './public-types';

Expand Down Expand Up @@ -496,13 +497,31 @@ function insertCompiledTemplate<EnvSpecificOptions>(

let expression = t.callExpression(templateFactoryIdentifier, [templateExpression]);

let params = [];
let assignment = target.parent;
let rootName = basename(state.filename).slice(0, -extname(state.filename).length);
let assignmentName: t.StringLiteral | t.Identifier = t.identifier('undefined');
if (assignment.type === 'AssignmentExpression' && assignment.left.type === 'Identifier') {
assignmentName = t.stringLiteral(rootName + ':' + assignment.left.name);
}
if (assignment.type === 'VariableDeclarator' && assignment.id.type === 'Identifier') {
assignmentName = t.stringLiteral(rootName + ':' + assignment.id.name);
}
if (assignment.type === 'ExportDefaultDeclaration') {
assignmentName = t.stringLiteral(rootName);
}

if (process.env.EMBER_ENV !== 'production') {
params.push(t.identifier('undefined'), assignmentName);
}

if (config.rfc931Support) {
expression = t.callExpression(i.import('@ember/component', 'setComponentTemplate'), [
expression,
backingClass?.node ??
t.callExpression(
i.import('@ember/component/template-only', 'default', 'templateOnly'),
[]
params
),
]);
}
Expand Down Expand Up @@ -606,14 +625,35 @@ function updateCallForm<EnvSpecificOptions>(
convertStrictMode(babel, target);
removeEvalAndScope(target);
target.node.arguments = target.node.arguments.slice(0, 2);

let params: (Babel.types.Identifier | Babel.types.StringLiteral)[] = [];
let assignment = target.parent;
let rootName = basename(state.filename).slice(0, -extname(state.filename).length);
let assignmentName: Babel.types.Identifier | Babel.types.StringLiteral =
babel.types.identifier('undefined');
if (assignment.type === 'AssignmentExpression' && assignment.left.type === 'Identifier') {
assignmentName = babel.types.stringLiteral(rootName + ':' + assignment.left.name);
}
if (assignment.type === 'VariableDeclarator' && assignment.id.type === 'Identifier') {
assignmentName = babel.types.stringLiteral(rootName + ':' + assignment.id.name);
}
if (assignment.type === 'ExportDefaultDeclaration') {
const name = basename(state.filename).slice(0, -extname(state.filename).length);
assignmentName = babel.types.stringLiteral(name);
}

if (process.env.EMBER_ENV !== 'production') {
params.push(babel.types.identifier('undefined'), assignmentName);
}

state.recursionGuard.add(target.node);
state.util.replaceWith(target, (i) =>
babel.types.callExpression(i.import('@ember/component', 'setComponentTemplate'), [
target.node,
backingClass?.node ??
babel.types.callExpression(
i.import('@ember/component/template-only', 'default', 'templateOnly'),
[]
params
),
])
);
Expand Down
Loading