Skip to content

Commit

Permalink
Avoid adding the import for setComponentTemplate.
Browse files Browse the repository at this point in the history
This will be done by an AST plugin instead.
  • Loading branch information
rwjblue committed Sep 9, 2019
1 parent 6de52e5 commit 5709c0f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
23 changes: 11 additions & 12 deletions lib/colocated-broccoli-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,16 @@ module.exports = class ColocatedTemplateProcessor extends Plugin {
}

_transformExistingComponent(filePath, jsContents, templateContents) {
// TODO: deal with hygiene
if (!jsContents.includes('export default')) {
let message = `\`${filePath}\` does not contain a \`default export\`. Did you forget to export the component class?`;
return `${jsContents}\nthrow new Error(${JSON.stringify(message)});`;
}

let modifiedJS = jsContents.replace('export default', 'const CLASS =');

// TODO: deal with hygiene?
let output = stripIndents`
${modifiedJS}
const setComponentTemplate = Ember._setComponentTemplate;
const TEMPLATE = ${this.options.precompile(templateContents)};
export default setComponentTemplate(TEMPLATE, CLASS);`;
import hbs from 'ember-cli-htmlbars-inline-precompile';
const __COLOCATED_TEMPLATE__ = hbs\`${templateContents}\`;
${jsContents}`;

return output;
}
Expand Down Expand Up @@ -81,11 +78,13 @@ module.exports = class ColocatedTemplateProcessor extends Plugin {
jsContents = this._transformExistingComponent(filePath, jsContents, templateContents);
} else {
// create JS file, use null component pattern
jsContents = `const templateOnlyComponent = Ember._templateOnlyComponent;
const setComponentTemplate = Ember._setComponentTemplate;
const TEMPLATE = ${this.options.precompile(templateContents)};
const CLASS = templateOnlyComponent();
export default setComponentTemplate(TEMPLATE, CLASS);`;

jsContents = stripIndents`
import templateOnly from '@ember/component/template-only';
import hbs from 'ember-cli-htmlbars-inline-precompile';
const __COLOCATED_TEMPLATE__ = hbs\`${templateContents}\`;
export default templateOnly();`
}

let outputPath = path.join(this.outputPath, possibleJSPath);
Expand Down
10 changes: 6 additions & 4 deletions node-tests/colocated-plugin-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ describe('ColocatedTemplateCompiler', function() {
'app-name-here': {
components: {
'foo.js': stripIndent`
import { setComponentTemplate } from '@ember/component';
import hbs from 'ember-cli-htmlbars-inline-precompile';
const __COLOCATED_TEMPLATE__ = hbs\`{{yield}}\`;
import Component from '@glimmer/component';
const CLASS = class FooComponent extends Component {}
const setComponentTemplate = Ember._setComponentTemplate;
const TEMPLATE = {"template":"{{yield}}"};
export default setComponentTemplate(TEMPLATE, CLASS);
export default class FooComponent extends Component {}
`,
},
templates: {
Expand Down Expand Up @@ -215,4 +215,6 @@ describe('ColocatedTemplateCompiler', function() {
},
});
});

it('does not break class decorator usage');
});

0 comments on commit 5709c0f

Please sign in to comment.