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

Allow addons to have pod based templates #4158

Merged
merged 2 commits into from
May 26, 2015
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 37 additions & 4 deletions lib/models/addon.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,38 @@ Addon.prototype.compileStyles = function(tree) {
}
};

/**
Looks in the addon/ and addon/templates trees to determine if template files
exists that need to be precompiled.

This is executed once when building, but not on rebuilds.

@private
@method shouldCompileTemplates
@returns Boolean indicates if templates need to be compiled for this addon
*/
Addon.prototype.shouldCompileTemplates = function() {
var templateExtensions = this.registry.extensionsForType('template');
var addonTreePath = path.join(this.root, this.treePaths['addon']);
var addonTemplatesTreePath = path.join(this.root, this.treePaths['addon-templates']);

var files = [];

if (fs.existsSync(addonTreePath)) {
files = files.concat(walkSync(addonTreePath));
}

if (fs.existsSync(addonTemplatesTreePath)) {
files = files.concat(walkSync(addonTemplatesTreePath));
}

var extensionMatcher = new RegExp('(' + templateExtensions.join('|') + ')$');

return files.some(function(file) {
return file.match(extensionMatcher);
});
};

/**
Runs the templates tree through preprocessors.

Expand All @@ -416,17 +448,18 @@ Addon.prototype.compileStyles = function(tree) {
Addon.prototype.compileTemplates = function(tree) {
this._requireBuildPackages();

if (tree) {
if (this.shouldCompileTemplates()) {
var plugins = this.registry.load('template');

if (plugins.length === 0) {
throw new SilentError('An `addon/templates` tree was detected, but there ' +
throw new SilentError('Addon templates were detected, but there ' +
'are no template compilers registered for `' + this.name + '`. ' +
'Please make sure your template precompiler (commonly `ember-cli-htmlbars`) ' +
'is listed in `dependencies` (NOT `devDependencies`) in ' +
'`' + this.name + '`\'s `package.json`.');
}

var standardTemplates = new Funnel(tree, {
var standardTemplates = this.pickFiles(this._treeFor('addon-templates'), {
srcDir: '/',
destDir: 'modules/' + this.name + '/templates'
});
Expand Down Expand Up @@ -459,7 +492,7 @@ Addon.prototype.compileAddon = function(tree) {
this._requireBuildPackages();

var addonJs = this.processedAddonJsFiles(tree);
var templatesTree = this.compileTemplates(this._treeFor('addon-templates'));
var templatesTree = this.compileTemplates(tree);
var reexported = reexport(this.name, this.name + '.js');
var trees = [addonJs, templatesTree, reexported].filter(Boolean);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Hi!</h1>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Derp</h1>
Empty file.
47 changes: 35 additions & 12 deletions tests/unit/models/addon-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,23 +473,46 @@ describe('models/addon.js', function() {
var packageContents = require(path.join(projectPath, 'package.json'));

project = new Project(projectPath, packageContents);

project.initializeAddons();

addon = findWhere(project.addons, { name: 'Ember CLI Generated with export' });
});

it('should throw a useful error if a template compiler is not present', function() {
var tree = path.join(fixturePath, 'simple');

expect(function() {
addon.compileTemplates(tree);
}).to.throw(
'An `addon/templates` tree was detected, but there ' +
'are no template compilers registered for `' + addon.name + '`. ' +
'Please make sure your template precompiler (commonly `ember-cli-htmlbars`) ' +
'is listed in `dependencies` (NOT `devDependencies`) in ' +
'`' + addon.name + '`\'s `package.json`.'
);
it('should throw a useful error if a template compiler is not present -- non-pods', function() {
addon.root = path.join(fixturePath, 'with-addon-templates');

expect(function() {
addon.compileTemplates();
}).to.throw(
'Addon templates were detected, but there ' +
'are no template compilers registered for `' + addon.name + '`. ' +
'Please make sure your template precompiler (commonly `ember-cli-htmlbars`) ' +
'is listed in `dependencies` (NOT `devDependencies`) in ' +
'`' + addon.name + '`\'s `package.json`.'
);
});

it('should throw a useful error if a template compiler is not present -- pods', function() {
addon.root = path.join(fixturePath, 'with-addon-pod-templates');

expect(function() {
addon.compileTemplates();
}).to.throw(
'Addon templates were detected, but there ' +
'are no template compilers registered for `' + addon.name + '`. ' +
'Please make sure your template precompiler (commonly `ember-cli-htmlbars`) ' +
'is listed in `dependencies` (NOT `devDependencies`) in ' +
'`' + addon.name + '`\'s `package.json`.'
);
});

it('should not throw an error if addon/templates is present but empty', function() {
addon.root = path.join(fixturePath, 'with-empty-addon-templates');

expect(function() {
addon.compileTemplates();
}).not.to.throw();
});
});

Expand Down