werkzeug
Version:
compiles ts, coffee, sass/scss, less/ stylus and packs browser ready bundles
89 lines (65 loc) • 2.42 kB
text/coffeescript
Coffee = require 'coffee-script'
FS = require 'fs'
FSE = require 'fs-extra'
Path = require 'path'
PH = require '../utils/path-helper'
IPC = require '../utils/ipc'
Log = require '../utils/log'
class CoffeeCompiler
constructor: () ->
= false
= null
= null
= 0
= new IPC(process, @)
init: () ->
null
compile: (files) ->
= []
for file in files
file
null
compileFile: (file) ->
path = file.path
++
FS.readFile path, 'utf8', (error, source) =>
if error
.push
path: path
error: 'file read error'
else
outPath = PH.outFromIn , 'coffee', path, true
mapPath = outPath + '.map'
options =
sourceMap: true
filename: path
generatedFile: Path.basename outPath
sourceRoot: ''
sourceFiles: [Path.relative Path.dirname(outPath), path]
try
result = Coffee.compile source, options
catch error
if error
text = /error: (.*?)(\r\n|\n)/.exec(error.toString())
text = text[1] if text
text = text or error.toString()
.push
path: error.filename
line: error.location.first_line + 1
col: error.location.first_column + 1
error: text
else
jsSrc = result.js
mapSrc = result.v3SourceMap
if mapSrc
jsSrc += "\n//# sourceMappingURL=#{Path.basename mapPath}\n"
FSE.ensureFileSync mapPath
FS.writeFileSync mapPath, mapSrc, 'utf8'
FSE.ensureFileSync outPath
FS.writeFileSync outPath, jsSrc, 'utf8'
() if -- == 0
null
compiled: () ->
= true
.send 'compiled', 'coffee',
module.exports = new CoffeeCompiler()