1 | compiler = require "../compiler"
|
2 | through = require "through2"
|
3 | path = require "path"
|
4 | kit = require "./kit"
|
5 |
|
6 | module.exports = (filename,opts) ->
|
7 | opts ?= {}
|
8 | eopts = compiler.FILE_EXTENSIONS[path.extname filename]
|
9 | return through() if eopts is false
|
10 | eopts ?= {}
|
11 | opts = kit.merge {}, eopts, opts
|
12 | opts.transform ?= {}
|
13 | opts.transform.filename = filename
|
14 | chunks = []
|
15 | transform = (chunk, encoding, callback) ->
|
16 | chunks.push(chunk)
|
17 | callback()
|
18 | flush = (callback) ->
|
19 | source = Buffer.concat(chunks).toString()
|
20 | try
|
21 | if opts.timing
|
22 | console.time("transform #{filename}")
|
23 | res = compiler.compile(source, opts)
|
24 | if opts.timing
|
25 | console.timeEnd("transform #{filename}")
|
26 | if opts.transformDebug
|
27 | console.log "result:", res
|
28 | @push(res)
|
29 | callback()
|
30 | catch e
|
31 | callback(e)
|
32 | through(transform,flush)
|
33 |
|
34 |
|