Permalink
Browse files

Function for compiling an HTML file, replacing its inline script requ…

…ire with the compiled js
  • Loading branch information...
1 parent 5804eba commit a48017794af4332a9b13cc171c05c96cd1bb7585 @marcuswestin committed Nov 16, 2011
Showing with 15 additions and 0 deletions.
  1. +15 −0 compiler.js
View
15 compiler.js
@@ -4,6 +4,7 @@ var fs = require('fs'),
module.exports = {
compile: compileFile,
+ compileHTML: compileHTMLFile,
compileCode: compileCode,
dontAddClosureForModule: dontAddClosureForModule,
dontIncludeModule: dontIncludeModule,
@@ -53,6 +54,20 @@ function compileCode(code, opts) {
return _compile(code, opts, '<code passed into compiler.compile()>')
}
+function compileHTMLFile(filePath, opts) {
+ var html = fs.readFileSync(filePath).toString()
+ while (match = html.match(/<script src="\/require\/([\/\w\.]+)"><\/script>/)) {
+ var js = compileFile(match[1].toString(), opts)
+
+ var BACKREFERENCE_WORKAROUND = '____________backreference_workaround________'
+ js = js.replace('\$\&', BACKREFERENCE_WORKAROUND)
+ html = html.replace(match[0], '<script>'+js+'</script>')
+ html = html.replace(BACKREFERENCE_WORKAROUND, '\$\&')
+ }
+ return html
+}
+
+
var _compile = function(code, opts, mainModule) {
var code = 'var __require__ = {}\n' + _compileModule(code, opts.basePath, mainModule)
if (opts.minify === false) { return code } // TODO use uglifyjs' beautifier?

0 comments on commit a480177

Please sign in to comment.