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

Enable colocated component classes to be TypeScript #333

Merged
Merged
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
28 changes: 20 additions & 8 deletions lib/colocated-broccoli-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,25 @@ module.exports = class ColocatedTemplateProcessor extends Plugin {
return;
}

// TODO: deal with alternate extensions (e.g. ts)
let possibleJSPath = path.join(filePathParts.dir, filePathParts.name + '.js');
let hasJSFile = fs.existsSync(path.join(this.inputPaths[0], possibleJSPath));

if (filePathParts.name === 'template') {
filesToCopy.push(filePath);
return;
}

let hasBackingClass = false;
let backingClassPath = path.join(filePathParts.dir, filePathParts.name);

if (fs.existsSync(path.join(this.inputPaths[0], backingClassPath + '.js'))) {
backingClassPath += '.js';
hasBackingClass = true;
} else if (fs.existsSync(path.join(this.inputPaths[0], backingClassPath + '.ts'))) {
backingClassPath += '.ts';
hasBackingClass = true;
} else {
backingClassPath += '.js';
hasBackingClass = false;
}

let templateContents = fs.readFileSync(inputPath, { encoding: 'utf8' });
let jsContents = null;

Expand All @@ -92,12 +102,14 @@ module.exports = class ColocatedTemplateProcessor extends Plugin {
)})`;
let prefix = `import { hbs } from 'ember-cli-htmlbars';\nconst __COLOCATED_TEMPLATE__ = ${hbsInvocation};\n`;

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

if (hasJSFile) {
if (hasBackingClass) {
// add the template, call setComponentTemplate

jsContents = fs.readFileSync(path.join(this.inputPaths[0], possibleJSPath), {
jsContents = fs.readFileSync(path.join(this.inputPaths[0], backingClassPath), {
encoding: 'utf8',
});

Expand All @@ -114,7 +126,7 @@ module.exports = class ColocatedTemplateProcessor extends Plugin {

jsContents = prefix + jsContents;

let outputPath = path.join(this.outputPath, possibleJSPath);
let outputPath = path.join(this.outputPath, backingClassPath);

// TODO: don't speculatively mkdirSync (likely do in a try/catch with ENOENT)
mkdirp.sync(path.dirname(outputPath));
Expand Down
44 changes: 44 additions & 0 deletions node-tests/colocated-plugin-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,50 @@ describe('ColocatedTemplateCompiler', function() {
});
});

it('works for typescript component class with template', async function() {
input.write({
'app-name-here': {
components: {
'foo.hbs': `{{yield}}`,
'foo.ts': stripIndent`
import Component from '@glimmer/component';

export default class FooComponent extends Component {}
`,
},
templates: {
'application.hbs': `{{outlet}}`,
},
},
});

let tree = new ColocatedTemplateCompiler(input.path(), {
precompile(template) {
return JSON.stringify({ template });
},
});

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

assert.deepStrictEqual(output.read(), {
'app-name-here': {
components: {
'foo.ts': 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 '@glimmer/component';

export default class FooComponent extends Component {}
`,
},
templates: {
'application.hbs': '{{outlet}}',
},
},
});
});

it('works for scoped addon using template only component', async function() {
input.write({
'@scope-name': {
Expand Down