Skip to content
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
31 changes: 29 additions & 2 deletions plugin/compile-coffeescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,24 @@ var addSharedHeader = function (source, sourceMap) {
};
};

var tplCoffeeWrapper = function (source, filepath) {
// Find the file's name from the filepath
name = path.basename(filepath, '.tpl.coffee');

var header = "Template." + name + '.';

// prefix Tmplate.<name>. to each line that starts with events, helpers, etc.
// Alternative implementation would be to add the follwing
// to the beginning of the file the following:
// events = Template.<name>.events
// helpers = Template.<name>.helpers
// etc.
source = source.replace(/^(events|helpers|onRendered|onCreated|onDestroyed)/mg,
header + '$1');

return source;
}

var addWrapper = function (source, sourceMap, filepath, wrapper) {
// Find the file's name from the filepath
name = path.basename(filepath, '.' + wrapper + '.coffee');
Expand All @@ -136,7 +154,7 @@ var addWrapper = function (source, sourceMap, filepath, wrapper) {
};
}

var handler = function (compileStep, isLiterate, templateWrapper) {
var handler = function (compileStep, isLiterate, templateWrapper, tplWrapper) {
var source = compileStep.read().toString('utf8');
var outputFile = compileStep.inputPath + ".js";

Expand All @@ -154,6 +172,10 @@ var handler = function (compileStep, isLiterate, templateWrapper) {
sourceFiles: [compileStep.pathForSourceMap]
};

if (tplWrapper){
source = tplCoffeeWrapper(source, compileStep.inputPath)
}

try {
var output = coffee.compile(source, options);
} catch (e) {
Expand Down Expand Up @@ -196,8 +218,13 @@ var eventsHandler = function (compileStep) {
return handler(compileStep, false, 'events');
};

var tplHandler = function (compileStep) {
return handler(compileStep, false, false, true);
};

Plugin.registerSourceHandler("coffee", handler);
Plugin.registerSourceHandler("litcoffee", literateHandler);
Plugin.registerSourceHandler("coffee.md", literateHandler);
Plugin.registerSourceHandler("helpers.coffee", helpersHandler);
Plugin.registerSourceHandler("events.coffee", eventsHandler);
Plugin.registerSourceHandler("events.coffee", eventsHandler);
Plugin.registerSourceHandler("tpl.coffee", tplHandler);