1 | Coffeescript = require 'coffee-script'
|
2 | child_process = require 'child_process'
|
3 | fs = require 'fs-jetpack'
|
4 | path = require 'path'
|
5 | md5 = require 'md5'
|
6 | COFFEE_CACHE_DIR = if process.env.COFFEE_CACHE_DIR then path.resolve(process.env.COFFEE_CACHE_DIR) else path.join __dirname,'..','.cache'
|
7 | COFFEE_NO_CACHE = process.env.COFFEE_NO_CACHE
|
8 | serveCached = not COFFEE_NO_CACHE
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | register = (extensions)->
|
14 | targetExtensions = [].concat(extensions, '.coffee')
|
15 | fs.dir(COFFEE_CACHE_DIR)
|
16 | cachedFiles = fs.list(COFFEE_CACHE_DIR).filter (file)-> file.slice(-3) is '.js'
|
17 |
|
18 | loadFile = (module, filename)->
|
19 | content = fs.read(filename)
|
20 | hash = md5(content)
|
21 | cachedFile = "#{hash}.js"
|
22 | cachedFilePath = path.join COFFEE_CACHE_DIR,cachedFile
|
23 |
|
24 | if serveCached and cachedFiles.indexOf(cachedFile) isnt -1
|
25 | compiledContent = fs.read cachedFilePath
|
26 | else
|
27 | compiledContent = Coffeescript.compile content, {filename, bare:true, inlineMap:true}
|
28 | fs.write cachedFilePath, compiledContent
|
29 |
|
30 | module._compile compiledContent, filename
|
31 |
|
32 |
|
33 | for extension in targetExtensions when extension
|
34 | require.extensions[extension] = loadFile
|
35 |
|
36 | return register
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | if child_process
|
45 | origFork = child_process.fork
|
46 | coffeeBinary = path.resolve './node_modules/coffee-script/bin/coffee'
|
47 |
|
48 | child_process.fork = (filePath, args, options)->
|
49 | if path.extname(filePath) is '.coffee'
|
50 | unless Array.isArray(args)
|
51 | options = args or {}
|
52 | args = []
|
53 |
|
54 | args = [filePath].concat args
|
55 | filePath = coffeeBinary
|
56 |
|
57 | origFork filePath, args, options
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | module.exports = register()
|
63 |
|
64 |
|
65 |
|