Skip to content

Commit b367bb3

Browse files
author
Robert Jackson
authored
Add co-location support to CoffeeScript component class files (#373)
Add co-location support to CoffeeScript component class files
2 parents 37823fa + dbef9d0 commit b367bb3

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

lib/colocated-broccoli-plugin.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ module.exports = class ColocatedTemplateProcessor extends Plugin {
8282
} else if (fs.existsSync(path.join(this.inputPaths[0], backingClassPath + '.ts'))) {
8383
backingClassPath += '.ts';
8484
hasBackingClass = true;
85+
} else if (fs.existsSync(path.join(this.inputPaths[0], backingClassPath + '.coffee'))) {
86+
backingClassPath += '.coffee';
87+
hasBackingClass = true;
8588
} else {
8689
backingClassPath += '.js';
8790
hasBackingClass = false;
@@ -101,6 +104,9 @@ module.exports = class ColocatedTemplateProcessor extends Plugin {
101104
hbsInvocationOptions
102105
)})`;
103106
let prefix = `import { hbs } from 'ember-cli-htmlbars';\nconst __COLOCATED_TEMPLATE__ = ${hbsInvocation};\n`;
107+
if (backingClassPath.endsWith('.coffee')) {
108+
prefix = `import { hbs } from 'ember-cli-htmlbars'\n__COLOCATED_TEMPLATE__ = ${hbsInvocation}\n`;
109+
}
104110

105111
logger.debug(
106112
`processing colocated template: ${filePath} (template-only: ${hasBackingClass})`

node-tests/colocated-broccoli-plugin-test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,48 @@ describe('ColocatedTemplateCompiler', function() {
149149
});
150150
});
151151

152+
it('works for coffeescript component class with template', async function() {
153+
input.write({
154+
'app-name-here': {
155+
components: {
156+
'foo.hbs': `{{yield}}`,
157+
'foo.coffee': stripIndent`
158+
import Component from '@ember/component'
159+
export default class extends Component
160+
`,
161+
},
162+
templates: {
163+
'application.hbs': `{{outlet}}`,
164+
},
165+
},
166+
});
167+
168+
let tree = new ColocatedTemplateCompiler(input.path(), {
169+
precompile(template) {
170+
return JSON.stringify({ template });
171+
},
172+
});
173+
174+
output = createBuilder(tree);
175+
await output.build();
176+
177+
assert.deepStrictEqual(output.read(), {
178+
'app-name-here': {
179+
components: {
180+
'foo.coffee': stripIndent`
181+
import { hbs } from 'ember-cli-htmlbars'
182+
__COLOCATED_TEMPLATE__ = hbs("{{yield}}", {"contents":"{{yield}}","moduleName":"app-name-here/components/foo.hbs","parseOptions":{"srcName":"app-name-here/components/foo.hbs"}})
183+
import Component from '@ember/component'
184+
export default class extends Component
185+
`,
186+
},
187+
templates: {
188+
'application.hbs': '{{outlet}}',
189+
},
190+
},
191+
});
192+
});
193+
152194
it('works for scoped addon using template only component', async function() {
153195
input.write({
154196
'@scope-name': {

0 commit comments

Comments
 (0)