Skip to content
This repository
Browse code

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

…ire with the compiled js
  • Loading branch information...
commit a48017794af4332a9b13cc171c05c96cd1bb7585 1 parent 5804eba
Marcus Westin authored November 15, 2011

Showing 1 changed file with 15 additions and 0 deletions. Show diff stats Hide diff stats

  1. 15  compiler.js
15  compiler.js
@@ -4,6 +4,7 @@ var fs = require('fs'),
4 4
 
5 5
 module.exports = {
6 6
 	compile: compileFile,
  7
+	compileHTML: compileHTMLFile,
7 8
 	compileCode: compileCode,
8 9
 	dontAddClosureForModule: dontAddClosureForModule,
9 10
 	dontIncludeModule: dontIncludeModule,
@@ -53,6 +54,20 @@ function compileCode(code, opts) {
53 54
 	return _compile(code, opts, '<code passed into compiler.compile()>')
54 55
 }
55 56
 
  57
+function compileHTMLFile(filePath, opts) {
  58
+	var html = fs.readFileSync(filePath).toString()
  59
+	while (match = html.match(/<script src="\/require\/([\/\w\.]+)"><\/script>/)) {
  60
+		var js = compileFile(match[1].toString(), opts)
  61
+		
  62
+		var BACKREFERENCE_WORKAROUND = '____________backreference_workaround________'
  63
+		js = js.replace('\$\&', BACKREFERENCE_WORKAROUND)
  64
+		html = html.replace(match[0], '<script>'+js+'</script>')
  65
+		html = html.replace(BACKREFERENCE_WORKAROUND, '\$\&')
  66
+	}
  67
+	return html
  68
+}
  69
+
  70
+
56 71
 var _compile = function(code, opts, mainModule) {
57 72
 	var code = 'var __require__ = {}\n' + _compileModule(code, opts.basePath, mainModule)
58 73
 	if (opts.minify === false) { return code } // TODO use uglifyjs' beautifier?

0 notes on commit a480177

Please sign in to comment.
Something went wrong with that request. Please try again.