Skip to content

Commit

Permalink
Ensure co-located templates with re-exported class do not throw an error
Browse files Browse the repository at this point in the history
  • Loading branch information
robinborst95 committed Jul 31, 2023
1 parent 2479dec commit e63af7f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/colocated-broccoli-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,10 @@ module.exports = class ColocatedTemplateProcessor extends Plugin {
}
);

if (hasTemplate && !jsContents.includes('export default')) {
if (hasTemplate && jsContents.includes('export { default }')) {
jsContents = jsContents.replace('export { default }', 'import __Component');
jsContents += '\n\nexport default class extends __Component {}';
} else if (hasTemplate && !jsContents.includes('export default')) {
let message = `\`${relativePath}\` does not contain a \`default export\`. Did you forget to export the component class?`;
jsContents = `${jsContents}\nthrow new Error(${JSON.stringify(message)});`;
prefix = '';
Expand Down
56 changes: 56 additions & 0 deletions node-tests/colocated-broccoli-plugin-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,62 @@ describe('ColocatedTemplateCompiler', function () {
);
});

it('works for re-exported component with a template', async function () {
input.write({
'app-name-here': {
'router.js': '// stuff here',
components: {
'foo.hbs': `{{yield}}`,
'foo.js': `export { default } from 'some-place';`,
},
templates: {
'application.hbs': `{{outlet}}`,
},
},
});

let tree = new ColocatedTemplateCompiler(input.path());

output = createBuilder(tree);
await output.build();

assert.deepStrictEqual(output.read(), {
'app-name-here': {
'router.js': '// stuff here',
components: {
'foo.js': stripIndent`
import { hbs } from 'ember-cli-htmlbars';
const __COLOCATED_TEMPLATE__ = hbs("{{yield}}", {"contents":"{{yield}}","moduleName":"app-name-here/components/foo.hbs","parseOptions":{"srcName":"app-name-here/components/foo.hbs"}});
import __Component from 'some-place';
export default class extends __Component {}
`,
},
templates: {
'application.hbs': '{{outlet}}',
},
},
});

await output.build();

assert.deepStrictEqual(output.changes(), {}, 'NOOP update has no changes');

input.write({
'app-name-here': {
'router.js': '// other stuff here',
},
});

await output.build();

assert.deepStrictEqual(
output.changes(),
{ 'app-name-here/router.js': 'change' },
'has only related changes'
);
});

it('works for typescript component class with template', async function () {
input.write({
'app-name-here': {
Expand Down

0 comments on commit e63af7f

Please sign in to comment.