diff --git a/lib/colocated-babel-plugin.js b/lib/colocated-babel-plugin.js index 221128ff..2867e0d1 100644 --- a/lib/colocated-babel-plugin.js +++ b/lib/colocated-babel-plugin.js @@ -23,7 +23,19 @@ module.exports = function (babel) { ExportDefaultDeclaration(path, state) { if (state.colocatedTemplateFound !== true || state.setComponentTemplateInjected === true) { - return; + let colocatedFlagIndex = path.parent.body.findIndex((node) => { + if (node.type === 'VariableDeclaration') { + if ( + node.declarations.findIndex((declaration) => { + if (declaration.id.name === '__COLOCATED_TEMPLATE__') return true; + return false; + }) >= 0 + ) + return true; + return false; + } + }); + if (colocatedFlagIndex < 0) return; } state.setComponentTemplateInjected = true; diff --git a/lib/colocated-broccoli-plugin.js b/lib/colocated-broccoli-plugin.js index 41a110ea..b934d2cc 100644 --- a/lib/colocated-broccoli-plugin.js +++ b/lib/colocated-broccoli-plugin.js @@ -154,14 +154,16 @@ module.exports = class ColocatedTemplateProcessor extends Plugin { 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 = ''; - } + } else if (hasTemplate) jsContents = jsContents + `\n${prefix}`; } else { // create JS file, use null component pattern - jsContents = `import templateOnly from '@ember/component/template-only';\n\nexport default templateOnly();\n`; + jsContents = + prefix + + `import templateOnly from '@ember/component/template-only';\n\nexport default templateOnly();\n`; } - jsContents = prefix + jsContents; + //jsContents = prefix + jsContents; let jsOutputPath = path.join(this.outputPath, backingClassPath); diff --git a/node-tests/colocated-broccoli-plugin-test.js b/node-tests/colocated-broccoli-plugin-test.js index 4b888db0..84f06834 100644 --- a/node-tests/colocated-broccoli-plugin-test.js +++ b/node-tests/colocated-broccoli-plugin-test.js @@ -104,13 +104,14 @@ describe('ColocatedTemplateCompiler', function () { '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"}}); + 'foo.js': + stripIndent` import Component from '@glimmer/component'; export default class FooComponent extends Component {} - `, + 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"}});` + + '\n', }, templates: { 'application.hbs': '{{outlet}}', @@ -211,13 +212,14 @@ describe('ColocatedTemplateCompiler', function () { 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"}}); + 'foo.ts': + stripIndent` import Component from '@glimmer/component'; export default class FooComponent extends Component {} - `, + 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"}});` + + '\n', }, templates: { 'application.hbs': '{{outlet}}', @@ -254,12 +256,13 @@ describe('ColocatedTemplateCompiler', function () { assert.deepStrictEqual(output.read(), { 'app-name-here': { components: { - 'foo.coffee': stripIndent` - import { hbs } from 'ember-cli-htmlbars' - __COLOCATED_TEMPLATE__ = hbs("{{yield}}", {"contents":"{{yield}}","moduleName":"app-name-here/components/foo.hbs","parseOptions":{"srcName":"app-name-here/components/foo.hbs"}}) + 'foo.coffee': + stripIndent` import Component from '@ember/component' export default class extends Component - `, + import { hbs } from 'ember-cli-htmlbars' + __COLOCATED_TEMPLATE__ = hbs("{{yield}}", {"contents":"{{yield}}","moduleName":"app-name-here/components/foo.hbs","parseOptions":{"srcName":"app-name-here/components/foo.hbs"}})` + + '\n', }, templates: { 'application.hbs': '{{outlet}}', @@ -343,13 +346,14 @@ describe('ColocatedTemplateCompiler', function () { '@scope-name': { 'addon-name-here': { components: { - 'foo.js': stripIndent` - import { hbs } from 'ember-cli-htmlbars'; - const __COLOCATED_TEMPLATE__ = hbs("{{yield}}", {"contents":"{{yield}}","moduleName":"@scope-name/addon-name-here/components/foo.hbs","parseOptions":{"srcName":"@scope-name/addon-name-here/components/foo.hbs"}}); + 'foo.js': + stripIndent` import Component from '@glimmer/component'; export default class FooComponent extends Component {} - `, + import { hbs } from 'ember-cli-htmlbars'; + const __COLOCATED_TEMPLATE__ = hbs("{{yield}}", {"contents":"{{yield}}","moduleName":"@scope-name/addon-name-here/components/foo.hbs","parseOptions":{"srcName":"@scope-name/addon-name-here/components/foo.hbs"}});` + + '\n', }, templates: { 'application.hbs': '{{outlet}}', @@ -584,13 +588,14 @@ describe('ColocatedTemplateCompiler', function () { '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"}}); + 'foo.js': + stripIndent` import Component from '@glimmer/component'; export default class FooComponent extends Component {} - `, + 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"}});` + + '\n', }, templates: { 'application.hbs': '{{outlet}}', @@ -669,13 +674,14 @@ describe('ColocatedTemplateCompiler', function () { '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"}}); + 'foo.js': + stripIndent` import Component from '@glimmer/component'; export default class FooComponent extends Component {} - `, + 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"}});` + + '\n', }, templates: { 'application.hbs': '{{outlet}}', @@ -936,13 +942,14 @@ describe('ColocatedTemplateCompiler', function () { '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"}}); + 'foo.js': + stripIndent` import Component from '@glimmer/component'; export default class FooComponent extends Component {} - `, + 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"}});` + + '\n', }, templates: { 'application.hbs': '{{outlet}}', @@ -978,13 +985,14 @@ describe('ColocatedTemplateCompiler', function () { 'app-name-here': { 'router.js': '// stuff here', components: { - 'foo.js': stripIndent` - import { hbs } from 'ember-cli-htmlbars'; - const __COLOCATED_TEMPLATE__ = hbs("whoops!", {"contents":"whoops!","moduleName":"app-name-here/components/foo.hbs","parseOptions":{"srcName":"app-name-here/components/foo.hbs"}}); + 'foo.js': + stripIndent` import Component from '@glimmer/component'; export default class FooComponent extends Component {} - `, + import { hbs } from 'ember-cli-htmlbars'; + const __COLOCATED_TEMPLATE__ = hbs("whoops!", {"contents":"whoops!","moduleName":"app-name-here/components/foo.hbs","parseOptions":{"srcName":"app-name-here/components/foo.hbs"}});` + + '\n', }, templates: { 'application.hbs': '{{outlet}}', @@ -1024,13 +1032,14 @@ describe('ColocatedTemplateCompiler', function () { '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"}}); + 'foo.js': + stripIndent` import Component from '@glimmer/component'; export default class FooComponent extends Component {} - `, + 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"}});` + + '\n', }, templates: { 'application.hbs': '{{outlet}}', @@ -1064,13 +1073,14 @@ describe('ColocatedTemplateCompiler', function () { '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"}}); + 'foo.js': + stripIndent` import Component from '@glimmer/component'; export default class FooBarComponent extends Component {} - `, + 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"}});` + + '\n', }, templates: { 'application.hbs': '{{outlet}}', @@ -1116,13 +1126,14 @@ describe('ColocatedTemplateCompiler', function () { '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"}}); + 'foo.js': + stripIndent` import Component from '@glimmer/component'; export default class FooComponent extends Component {} - `, + 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"}});` + + '\n', }, templates: { 'application.hbs': '{{outlet}}', diff --git a/node-tests/colocated-test.js b/node-tests/colocated-test.js index 18949b3b..665d1ebe 100644 --- a/node-tests/colocated-test.js +++ b/node-tests/colocated-test.js @@ -131,6 +131,8 @@ describe('Colocation - Broccoli + Babel Integration', function () { 'app-name-here': { components: { 'foo.js': stripIndent` + import Component from '@glimmer/component'; + export default class FooComponent extends Component {} import { hbs } from 'ember-cli-htmlbars'; const __COLOCATED_TEMPLATE__ = hbs("{{yield}}", { @@ -141,9 +143,6 @@ describe('Colocation - Broccoli + Babel Integration', function () { } }); - import Component from '@glimmer/component'; - export default class FooComponent extends Component {} - Ember._setComponentTemplate(__COLOCATED_TEMPLATE__, FooComponent); `, }, @@ -179,6 +178,8 @@ describe('Colocation - Broccoli + Babel Integration', function () { 'app-name-here': { components: { 'foo.js': stripIndent` + import Component from '@glimmer/component'; + export default class FooComponent extends Component {} import { hbs } from 'ember-cli-htmlbars'; const __COLOCATED_TEMPLATE__ = hbs("{{yield}}", { @@ -189,9 +190,6 @@ describe('Colocation - Broccoli + Babel Integration', function () { } }); - import Component from '@glimmer/component'; - export default class FooComponent extends Component {} - Ember._setComponentTemplate(__COLOCATED_TEMPLATE__, FooComponent); `, }, @@ -274,6 +272,8 @@ describe('Colocation - Broccoli + Babel Integration', function () { 'addon-name-here': { components: { 'foo.js': stripIndent` + import Component from '@glimmer/component'; + export default class FooComponent extends Component {} import { hbs } from 'ember-cli-htmlbars'; const __COLOCATED_TEMPLATE__ = hbs("{{yield}}", { @@ -284,9 +284,6 @@ describe('Colocation - Broccoli + Babel Integration', function () { } }); - import Component from '@glimmer/component'; - export default class FooComponent extends Component {} - Ember._setComponentTemplate(__COLOCATED_TEMPLATE__, FooComponent); `, },