Last active
December 24, 2015 07:59
-
-
Save tresbailey/6767271 to your computer and use it in GitHub Desktop.
Compiler for javascript templates (using underscorejs syntax) to be output as a *.js file. Write your templates in HTML and include them into the single page app as js file includes instead of keeping them all in HTML source. Watches the file system for changes to the html files in the given directory to run automatically on changes. Requires no…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fs = require('fs') | |
under = require('underscore') | |
path = require('path') | |
fs.watch('market/static/templates', function(event, fileName) { | |
if ( '.html' == path.extname(fileName) ) { | |
console.log('Received a change on '+ fileName +'...'); | |
var templ = fs.readFileSync('market/static/templates/'+ fileName).toString(); | |
var html_name = path.basename(fileName, '.html'); | |
fs.writeFileSync('market/static/templates/_compiled/'+ html_name +'.js', 'var '+ html_name.replace(/-([^-]*)$/,'_'+'$1') +' = '+ under.template(templ).source); | |
console.log('Recompiled the template'); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fs = require('fs') | |
_ = require('underscore') | |
path = require('path') | |
https = require('https') | |
$ = require('jquery') | |
var program = require('commander') | |
program | |
.option('-t, --templ_dir <templ_dir>', 'The directory of raw templates') | |
.option('-c, --compiled_dir <compiled_dir>', 'The directory of compiled templates to write to') | |
.option('-u, --l10n_url <l10n_url>', 'The url of all language bundles') | |
.parse(process.argv); | |
var template_dir = program.templ_dir; | |
var l10n_url = program.l10n_url; | |
var compiled_dir = program.compiled_dir || template_dir +'_compiled/'; | |
function compile_changed_templates(fileName, language, bundle) { | |
if ( '.html' == path.extname(fileName) ) { | |
console.log('Received a change on '+ fileName +'...'); | |
var templ = fs.readFileSync(template_dir+ fileName).toString(); | |
var html_name = path.basename(fileName, '.html'); | |
var target_dir = (!_.isEmpty(language)) ? compiled_dir + language + '/' : compiled_dir; | |
console.log('Compiled directory: %s', target_dir); | |
var write_file = function() { | |
// Function that will write into the actual file the data | |
var old_interpolate = _.templateSettings['interpolate']; | |
_.templateSettings = { interpolate: /\{%(.+?)%\}/g }; | |
var locale_template = _.template(templ); | |
source = locale_template(bundle); | |
_.templateSettings = { interpolate: old_interpolate }; | |
fs.writeFileSync(target_dir + html_name +'.js', 'var '+ html_name.replace(/-([^-]*)$/,'_'+'$1') +' = '+ _.template(templ).source); | |
} | |
fs.exists(target_dir, function(exists) { | |
// If the target language hasn't been done before, there is not a dir for it, so create it and write the file | |
if (!exists) { | |
fs.mkdir(target_dir, write_file); | |
} else { | |
write_file(); | |
} | |
}); | |
console.log('Recompiled the template'); | |
} | |
} | |
fs.watch(template_dir, function(event, fileName) { | |
console.log('Received a change on '+ fileName +'...'); | |
console.log('Will use localized version...%s', !_.isEmpty(l10n_url)); | |
if (!_.isEmpty(l10n_url)) { | |
// If a url was supplied, call it and get back the list of locales | |
$.ajax({ | |
url: l10n_url, | |
}).done(function( data ) { | |
// Locale data is assumed to be formatted as list of strings, as: ["en", "de_DE"] | |
var targeted_compile = _.partial(compile_changed_templates, fileName); | |
// Pass the language name into each compile function call | |
_.each($.parseJSON(data), function(lang, index, list) { | |
$.ajax({ | |
url: l10n_url +'/'+ lang, | |
}).done(function(bundle) { | |
compile_changed_templates(fileName, lang, $.parseJSON(bundle)); | |
}); | |
}); | |
}); | |
} else { | |
// Compile with the default bundles | |
compile_changed_templates(fileName, '', {}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment