Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
patricklx committed May 16, 2024
1 parent e03fabb commit f130369
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
29 changes: 17 additions & 12 deletions packages/addon-dev/src/rollup-hbs-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,34 @@ export default function rollupHbsPlugin({
}
},

transform(code: string, id: string) {
let meta = getMeta(this, id);
if (hbsFilter(id) && meta?.type !== 'template-js') {
return getHbsToJSCode(code);
async transform(code: string, id: string) {
if (!hbsFilter(id)) {
return;
}
if (meta) {
if (meta?.type === 'template-js') {
return getHbsToJSCode(code);
}
let meta = getMeta(this, id);
if (meta?.type === 'template-only-component-js') {
return {
code: templateOnlyComponent,
code: templateOnlyComponent(code),
};
}
return getHbsToJSCode(code);
},
};
}

const templateOnlyComponent =
`import templateOnly from '@ember/component/template-only';\n` +
`export default templateOnly();\n`;
function templateOnlyComponent(hbsCode: string) {
const code = hbsCode.replace(/`/g, '\\`').replace(/\$/g, '\\$');
return `
import templateOnly from '@ember/component/template-only';
import { precompileTemplate } from '@ember/template-compilation';
import { setComponentTemplate } from '@ember/component';
export default setComponentTemplate(precompileTemplate(\`${code}\`), templateOnly());
`;
}

type Meta = {
type: 'template-only-component-js' | 'template-js';
hbsFile: string;
};

function getMeta(context: PluginContext, id: string): Meta | null {
Expand Down
6 changes: 3 additions & 3 deletions tests/scenarios/v2-addon-dev-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ appScenarios
addon.dependencies(),
addon.publicAssets('public'),
babel({ babelHelpers: 'bundled', extensions: ['.js', '.hbs', '.gjs'] }),
babel({ babelHelpers: 'bundled', extensions: ['.js', '.hbs', '.gjs'] }),
],
};
`,
Expand Down Expand Up @@ -210,12 +210,12 @@ appScenarios
exclude: ['**/-excluded/**/*'],
}),
addon.clean(),
addon.hbs(),
addon.publicAssets('public', { namespace: '' }),
babel({ babelHelpers: 'bundled', extensions: ['.js', '.hbs', '.gjs'] }),
addon.clean(),
],
};
`,
Expand Down

0 comments on commit f130369

Please sign in to comment.