From 42275a34eb74ec9e7192b543237c0eff06567598 Mon Sep 17 00:00:00 2001 From: tdamsma Date: Tue, 11 Aug 2015 23:09:02 +0200 Subject: [PATCH 1/3] Added .tpl format to remove need for separate events and helpers templates --- plugin/compile-coffeescript.js | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/plugin/compile-coffeescript.js b/plugin/compile-coffeescript.js index ab66d43..7570da5 100644 --- a/plugin/compile-coffeescript.js +++ b/plugin/compile-coffeescript.js @@ -113,11 +113,24 @@ var addSharedHeader = function (source, sourceMap) { }; }; +var addCoffeeWrapper = function (source, filepath, wrapper) { + // Find the file's name from the filepath + name = path.basename(filepath, '.' + wrapper + '.coffee'); + + var header = "Template." + name + '.'; + + + 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'); - var header = "Template." + name + "." + wrapper; + var header = "Template." + name + '.' + wrapper; // We find all instances of CoffeeScripts's helper // functions (such as __indexOf), and the file's @@ -136,7 +149,7 @@ var addWrapper = function (source, sourceMap, filepath, wrapper) { }; } -var handler = function (compileStep, isLiterate, templateWrapper) { +var handler = function (compileStep, isLiterate, templateWrapper, templateCoffeeWrapper) { var source = compileStep.read().toString('utf8'); var outputFile = compileStep.inputPath + ".js"; @@ -154,6 +167,12 @@ var handler = function (compileStep, isLiterate, templateWrapper) { sourceFiles: [compileStep.pathForSourceMap] }; + if (templateCoffeeWrapper){ + console.log(source) + source = addCoffeeWrapper(source, compileStep.inputPath, templateCoffeeWrapper) + console.log(source) + } + try { var output = coffee.compile(source, options); } catch (e) { @@ -196,8 +215,13 @@ var eventsHandler = function (compileStep) { return handler(compileStep, false, 'events'); }; +var tplHandler = function (compileStep) { + return handler(compileStep, false, false, 'tpl'); +}; + Plugin.registerSourceHandler("coffee", handler); Plugin.registerSourceHandler("litcoffee", literateHandler); Plugin.registerSourceHandler("coffee.md", literateHandler); Plugin.registerSourceHandler("helpers.coffee", helpersHandler); -Plugin.registerSourceHandler("events.coffee", eventsHandler); \ No newline at end of file +Plugin.registerSourceHandler("events.coffee", eventsHandler); +Plugin.registerSourceHandler("tpl.coffee", tplHandler); \ No newline at end of file From 210e8b75dbd2d15c67a4b1639a9ffedbf4b7512d Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 12 Aug 2015 09:13:56 +0200 Subject: [PATCH 2/3] Added some comments, renamed for clarity --- plugin/compile-coffeescript.js | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/plugin/compile-coffeescript.js b/plugin/compile-coffeescript.js index 7570da5..0d7daaa 100644 --- a/plugin/compile-coffeescript.js +++ b/plugin/compile-coffeescript.js @@ -113,13 +113,18 @@ var addSharedHeader = function (source, sourceMap) { }; }; -var addCoffeeWrapper = function (source, filepath, wrapper) { +var tplWrapper = function (source, filepath) { // Find the file's name from the filepath - name = path.basename(filepath, '.' + wrapper + '.coffee'); + name = path.basename(filepath, '.tpl.coffee'); var header = "Template." + name + '.'; - + // prefix Tmplate.. 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..events + // helpers = Template..helpers + // etc. source = source.replace(/^(events|helpers|onRendered|onCreated|onDestroyed)/mg, header + '$1'); @@ -130,7 +135,7 @@ var addWrapper = function (source, sourceMap, filepath, wrapper) { // Find the file's name from the filepath name = path.basename(filepath, '.' + wrapper + '.coffee'); - var header = "Template." + name + '.' + wrapper; + var header = "Template." + name + "." + wrapper; // We find all instances of CoffeeScripts's helper // functions (such as __indexOf), and the file's @@ -149,7 +154,7 @@ var addWrapper = function (source, sourceMap, filepath, wrapper) { }; } -var handler = function (compileStep, isLiterate, templateWrapper, templateCoffeeWrapper) { +var handler = function (compileStep, isLiterate, templateWrapper, tplWrapper) { var source = compileStep.read().toString('utf8'); var outputFile = compileStep.inputPath + ".js"; @@ -167,10 +172,8 @@ var handler = function (compileStep, isLiterate, templateWrapper, templateCoffee sourceFiles: [compileStep.pathForSourceMap] }; - if (templateCoffeeWrapper){ - console.log(source) - source = addCoffeeWrapper(source, compileStep.inputPath, templateCoffeeWrapper) - console.log(source) + if (tplWrapper){ + source = addCoffeeWrapper(source, compileStep.inputPath) } try { @@ -216,7 +219,7 @@ var eventsHandler = function (compileStep) { }; var tplHandler = function (compileStep) { - return handler(compileStep, false, false, 'tpl'); + return handler(compileStep, false, false, true); }; Plugin.registerSourceHandler("coffee", handler); From 612b281c6700488fa941c4d336b44ba90e909673 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 12 Aug 2015 10:09:47 +0200 Subject: [PATCH 3/3] more renaming --- plugin/compile-coffeescript.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin/compile-coffeescript.js b/plugin/compile-coffeescript.js index 0d7daaa..f94a711 100644 --- a/plugin/compile-coffeescript.js +++ b/plugin/compile-coffeescript.js @@ -113,7 +113,7 @@ var addSharedHeader = function (source, sourceMap) { }; }; -var tplWrapper = function (source, filepath) { +var tplCoffeeWrapper = function (source, filepath) { // Find the file's name from the filepath name = path.basename(filepath, '.tpl.coffee'); @@ -173,7 +173,7 @@ var handler = function (compileStep, isLiterate, templateWrapper, tplWrapper) { }; if (tplWrapper){ - source = addCoffeeWrapper(source, compileStep.inputPath) + source = tplCoffeeWrapper(source, compileStep.inputPath) } try {