-
-
Save StanAngeloff/2882059 to your computer and use it in GitHub Desktop.
fs = require 'fs' | |
path = require 'path' | |
CoffeeScript = require 'coffee-script' | |
CoffeeScript.on 'compile', (task) -> | |
task.input = task.input.replace /^([ \t]*)#=\s*include\s+(.*)$/im, (group, indent, file) -> | |
"#{indent}`#{ fs.readFileSync(path.join(path.dirname(task.file), file), 'utf8') }`" |
Hey! It was more of a quick way to pull in external JavaScript files at a pre-determined location for a little tool I am working on. Your code poses an interesting case, but given the limited scope, the way CS handles extensions and the explicit syntax I am OK with the current behaviour.
Yeah, it should be fine for most practical cases. Still, my preferred way to do it would be to write regular function calls as statements (include "someFileName"
), parse the file, replace those subtrees with the parse tree of the referenced file, and compile that instead.
I actually do keep around a very old branch that did import
s in a similar way (extended the grammar). I agree, AST manipulation is a more practical and bullet-proof approach, but at this compiler stage I'd need to re-parse the source twice (once in the extension and once in CS itself) and the LoC would likely explode.
Looking forward to anything you could offer in CS2 in terms of extensibility, been keeping up with the Wiki and Twitter.
EDIT: OK, maybe not explode, but definitely grow somewhat. I guess a parse -> AST -> walk -> substitute -> return JS chain would be enough.
Unfortunately, this fails when the file is
edit: Though, I guess you could consider that a desired behaviour.