Skip to content
This repository was archived by the owner on Dec 8, 2024. It is now read-only.
Closed
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
16 changes: 15 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const parseModuleName = require('./lib/parse-module-name');

module.exports = function(babel) {
let t = babel.types;

Expand Down Expand Up @@ -39,7 +41,19 @@ module.exports = function(babel) {
}

let template = path.node.quasi.quasis.map(quasi => quasi.value.cooked).join('');
let compiledTemplateString = `Ember.HTMLBars.template(${state.opts.precompile(template)})`;

let options = {
meta: {}
}

let filename = state.file.opts.filename;
let moduleName = parseModuleName(filename);

if (moduleName) {
options.meta.moduleName = moduleName;
}

let compiledTemplateString = `Ember.HTMLBars.template(${state.opts.precompile(template, options)})`;

path.replaceWithSourceString(compiledTemplateString);
},
Expand Down
43 changes: 43 additions & 0 deletions lib/parse-module-name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';

module.exports = function (filename) {
let parts = filename.split(/[/\\]/);

// Drop the application name from the parts
parts.shift();

let lastPrivateCollectionIndex = parts.lastIndexOf('-components');

if (lastPrivateCollectionIndex !== -1) {
// TODO: We likely do not need new array allocations for the prefix and
// collection parts

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this still WIP? otherwise this comment should be removed


// Take the prefix string
let prefixString = parts.slice(0,lastPrivateCollectionIndex).join('/');

// Take the rest of the string starting at -collectionName (including the -)
let collectionParts = parts.slice(lastPrivateCollectionIndex);

// In other cases we're not sure about the semantics in your templates.
if (collectionParts.length >= 3) {
if (collectionParts.length > 3) {
collectionParts.splice(-2);
return `${prefixString}/${collectionParts.join('/')}/template.hbs`;
}
return `${prefixString}/template.hbs`;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    if (collectionParts.length > 3) {
      collectionParts.splice(-2);
      return `${prefixString}/${collectionParts.join('/')}/template.hbs`;

    } else if (collectionParts.length === 3) {
      return `${prefixString}/template.hbs`;
    }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also it seems that the route with collectionParts.length < 3 is not currently tested

} else {
let componentsIndex = parts.indexOf('components');

// We don't process anything except components
if (componentsIndex !== -1) {
// If we don't have 3 segments left, then there is no change to perform.
if (parts.length - componentsIndex > 3) {
parts.splice(-2);
return `${parts.join('/')}/template.hbs`;
}
}
}

return null;
}
41 changes: 41 additions & 0 deletions tests/parse-module-name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

const parseModuleName = require('../lib/parse-module-name');

describe('parse-module-name helper', function() {
it('should return application template when using a route local -component', function() {
expect(
parseModuleName('emberconf/src/ui/routes/application/-components/footer-prompt/component-test.js')
).toEqual('src/ui/routes/application/template.hbs');
});

it('should return the parent component when using a component local component', function() {
expect(
parseModuleName('emberconf/src/ui/components/icon-emberconf/facey-face/component-test.js')
).toEqual('src/ui/components/icon-emberconf/template.hbs');
});

it('should return the parent component when using a component local component in private collection', function() {
expect(
parseModuleName('emberconf/src/ui/routes/application/foo/-components/footer-prompt/component-test.js')
).toEqual('src/ui/routes/application/foo/template.hbs');
});

it('should return when using a route local -component with nested naming', function() {
expect(
parseModuleName('emberconf/src/ui/routes/application/-components/footer-prompt/foo/component-test.js')
).toEqual('src/ui/routes/application/-components/footer-prompt/template.hbs');
});

it('should return null for public components', function() {
expect(
parseModuleName('emberconf/src/ui/components/foo-bar/component-test.js')
).toEqual(null);
});

it('should return null for public components in test folder', function() {
expect(
parseModuleName('emberconf/tests/integration/components/foo-bar/component-test.js')
).toEqual(null);
});
});