Skip to content

Place colocated templates after the default export #558

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 13 additions & 1 deletion lib/colocated-babel-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 5 additions & 3 deletions lib/colocated-broccoli-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
99 changes: 55 additions & 44 deletions node-tests/colocated-broccoli-plugin-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}}',
Expand Down Expand Up @@ -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}}',
Expand Down Expand Up @@ -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}}',
Expand Down Expand Up @@ -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}}',
Expand Down Expand Up @@ -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}}',
Expand Down Expand Up @@ -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}}',
Expand Down Expand Up @@ -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}}',
Expand Down Expand Up @@ -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}}',
Expand Down Expand Up @@ -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}}',
Expand Down Expand Up @@ -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}}',
Expand Down Expand Up @@ -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}}',
Expand Down
15 changes: 6 additions & 9 deletions node-tests/colocated-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}}", {
Expand All @@ -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);
`,
},
Expand Down Expand Up @@ -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}}", {
Expand All @@ -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);
`,
},
Expand Down Expand Up @@ -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}}", {
Expand All @@ -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);
`,
},
Expand Down