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

support for compiling ember components #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
143 changes: 80 additions & 63 deletions tasks/ember-handlebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,79 +21,96 @@
*/

module.exports = function(grunt) {
'use strict';
'use strict';

var _ = grunt.util._,
helpers = require('grunt-lib-contrib').init(grunt),
precompiler = require('../lib/precompiler');
var _ = grunt.util._,
helpers = require('grunt-lib-contrib').init(grunt),
precompiler = require('../lib/precompiler');

// filename conversion for templates
var defaultProcessName = function(name) { return name; };
// filename conversion for templates
var defaultProcessName = function(name) { return name; };

// filename conversion for partials
var defaultProcessPartialName = function(filePath) {
var pieces = _.last(filePath.split('/')).split('.');
var name = _(pieces).without(_.last(pieces)).join('.'); // strips file extension
return name;
};
// filename conversion for partials
var defaultProcessPartialName = function(filePath) {
var pieces = _.last(filePath.split('/')).split('.');
var name = _(pieces).without(_.last(pieces)).join('.'); // strips file extension
return name;
};

grunt.registerMultiTask('ember_handlebars', 'Precompile Ember Handlebars templates.', function() {
var options = this.options({
namespace: 'Ember.TEMPLATES',
separator: grunt.util.linefeed + grunt.util.linefeed,
wrapped: true
});
grunt.verbose.writeflags(options, 'Options');
grunt.registerMultiTask('ember_handlebars', 'Precompile Ember Handlebars templates.', function() {
var options = this.options({
namespace: 'Ember.TEMPLATES',
componentRegex: /\/components\/[^/]+$/,
separator: grunt.util.linefeed + grunt.util.linefeed,
wrapped: true
});
grunt.verbose.writeflags(options, 'Options');

var nsInfo = helpers.getNamespaceDeclaration(options.namespace);

// assign regex for partial detection
var isPartial = options.partialRegex || /^_/;

// assign filename transformation functions
var processName = options.processName || defaultProcessName;
var processPartialName = options.processPartialName || defaultProcessPartialName;

var nsInfo = helpers.getNamespaceDeclaration(options.namespace);
// assign components detection.
var isComponent = options.componentRegex;
var componentObserved = false

// assign regex for partial detection
var isPartial = options.partialRegex || /^_/;
this.files.forEach(function(f) {
var partials = [],
templates = [],
componentObserved = false;

// assign filename transformation functions
var processName = options.processName || defaultProcessName;
var processPartialName = options.processPartialName || defaultProcessPartialName;
// iterate files, processing partials and templates separately
f.src.filter(function(filepath) {
// Warn on and remove invalid source files (if nonull was set).
if (!grunt.file.exists(filepath)) {
grunt.log.warn('Source file "' + filepath + '" not found.');
return false;
} else {
return true;
}
})
.forEach(function(filepath) {
var src = grunt.file.read(filepath);
var compiled, filename;
try {
compiled = precompiler.precompile(src);
// if configured to, wrap template in Handlebars.template call
if (options.wrapped) {
compiled = 'Ember.Handlebars.template('+compiled+')';
}
} catch (e) {
grunt.log.error(e);
grunt.fail.warn('Handlebars failed to compile '+filepath+'.');
}

this.files.forEach(function(f) {
var partials = [],
templates = [];
//process var name/namespace of template.
if (isPartial.test(_.last(filepath.split('/'))) ) {
filename = processPartialName(filepath);
templates.push(nsInfo.namespace+'['+JSON.stringify(filename)+'] = '+compiled+';');
} else if(isComponent.test(filepath)){
filename = processName(filepath);
templates.push(nsInfo.namespace + "[" + JSON.stringify( 'components/' + filename)+"] = " +compiled+';');
} else {
filename = processName(filepath);
templates.push(nsInfo.namespace +'['+JSON.stringify(filename)+'] = '+compiled+';');
}

// iterate files, processing partials and templates separately
f.src.filter(function(filepath) {
// Warn on and remove invalid source files (if nonull was set).
if (!grunt.file.exists(filepath)) {
grunt.log.warn('Source file "' + filepath + '" not found.');
return false;
} else {
return true;
}
})
.forEach(function(filepath) {
var src = grunt.file.read(filepath);
var compiled, filename;
try {
compiled = precompiler.precompile(src);
// if configured to, wrap template in Handlebars.template call
if (options.wrapped) {
compiled = 'Ember.Handlebars.template('+compiled+')';
}
} catch (e) {
grunt.log.error(e);
grunt.fail.warn('Handlebars failed to compile '+filepath+'.');
}

filename = isPartial.test(_.last(filepath.split('/'))) ? processPartialName(filepath) : processName(filepath);
templates.push(nsInfo.namespace+'['+JSON.stringify(filename)+'] = '+compiled+';');
});
});

var output = partials.concat(templates);
if (output.length < 1) {
grunt.log.warn('Destination not written because compiled files were empty.');
} else {
output.unshift(nsInfo.declaration);
grunt.file.write(f.dest, output.join(grunt.util.normalizelf(options.separator)));
grunt.log.writeln('File "' + f.dest + '" created.');
}
var output = partials.concat(templates);
if (output.length < 1) {
grunt.log.warn('Destination not written because compiled files were empty.');
} else {
output.unshift(nsInfo.declaration);
grunt.file.write(f.dest, output.join(grunt.util.normalizelf(options.separator)));
grunt.log.writeln('File "' + f.dest + '" created.');
}
});
});
});
};