UNPKG

2.2 kBtext/coffeescriptView Raw
1require "coffee-script"
2require "../initializer"
3
4path = require "path"
5fs = require "../utils/my-fs"
6Mincer = require "mincer"
7Config = require "../config"
8
9module.exports = class Compiler
10
11 constructor: (config) ->
12 if config instanceof Config
13 @config = config
14 else
15 @config = new Config(config)
16
17 clean: () ->
18 fs.cleanSync(@config.destDir, false)
19 return
20
21 setup: () ->
22 unless fs.existsSync(@config.destDir)
23 fs.mkdirSync(@config.destDir)
24 return
25
26 compile: () ->
27 @setup()
28 @createDebug()
29 @createMinify()
30 return
31
32 getDebugDir: () ->
33 path.join(@config.destDir, "debug")
34
35 getMinifyDir: () ->
36 path.join(@config.destDir, "minify")
37
38 createFiles: (outputDir, options) ->
39 files = @config.compile.paths
40 environment = @config.createEnvironment(options)
41
42 paths = []
43 environment.eachLogicalPath files, (pathname) ->
44 paths.push(pathname)
45
46 assetsRootDir = path.join(outputDir, @config.assets.contextRoot)
47 paths.forEach (logicalPath) ->
48 asset = undefined
49 target = undefined
50 asset = environment.findAsset(logicalPath, {
51 bundle: true
52 })
53 unless asset
54 throw new Error("Can not find asset '" + logicalPath + "'")
55
56 target = path.join(assetsRootDir, logicalPath)
57 if fs.existsSync(target)
58 console.warn("skip file: #{target}")
59
60 buffer = asset.buffer
61 if asset.sourceMap? && options.useSourceMaps?
62 fs.outputFileSync "#{target}.map", asset.sourceMap
63 buffer = asset.buffer + "\n/*# sourceMappingURL=" + "#{logicalPath}.map */"
64
65 fs.outputFileSync(target, buffer)
66 console.info("assets create: " + logicalPath)
67
68 # public
69 publicRootDir = path.join(outputDir, @config.public.contextRoot)
70 for pubPath in @config.public.paths
71 fs.copySync(path.join(@config.workDir, pubPath), publicRootDir)
72 console.info("public copy: " + pubPath)
73
74 return
75
76 createDebug: () ->
77 @createFiles(@getDebugDir(), {
78 useSourceMaps: true
79 })
80
81 createMinify: () ->
82 @createFiles(@getMinifyDir(), {
83 useSourceMaps: true
84 jsCompressor: "uglify"
85 cssCompressor: "csswring"
86 })
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101