-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCakefile
183 lines (162 loc) · 4.81 KB
/
Cakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
bach = require 'bach'
coffee = require 'coffeescript'
connect = require 'connect'
fs = require 'fs'
fsp = require 'fs/promises'
http = require 'http'
lr = require 'livereload'
{ extname } = require 'path'
rimraf = require 'rimraf'
{ rollup, watch } = require 'rollup'
rust = require '@wasm-tool/rollup-plugin-rust'
sass = require 'sass'
serveStatic = require 'serve-static'
{ terser } = require 'rollup-terser'
# OPTIONS #############################
option '-d', '--debug', 'set compilation mode to debug (default)'
option '-r', '--release', 'set compilation mode to release'
# GLOBAL VARS #########################
dest = ''
envRelease = false
watching = false
# ROLLUP PLUGINS ######################
rollCoffee = =>
name: 'rolling-coffee'
transform: (code, id) ->
if extname(id) != '.coffee' then return null
out = null
try
out = coffee.compile code
catch e
throw e
code: out
rollHtml = (filename) =>
name: 'rolling-html'
buildStart: ->
this.addWatchFile filename
generateBundle: ->
cfg =
type: 'asset'
fileName: filename
source: fs.readFileSync filename
this.emitFile cfg
rollSass = =>
css = ''
name: 'rolling-sass'
transform: (_, file) ->
style = if envRelease then 'compressed' else 'expanded'
rendered = sass.compile file, {style}
css = rendered.css
'export default ""'
generateBundle: (options, bundle) ->
Object.keys(bundle).forEach (filename) => delete bundle[filename]
fs.writeFileSync options.file, css
return null
# ACTION FUNCTIONS ####################
checkEnv = (options) ->
if options.debug and options.release
console.error 'error: debug and release can\'t be active together'
throw '`debug` and `release` in the same time'
else
if options.release then envRelease = true
dest = if envRelease then 'docs' else 'dist'
compileJs = (cb) ->
rustCfg =
debug: not envRelease
inOpts =
input: './scripts/index.coffee'
plugins: [
rollCoffee()
rust rustCfg
]
outOpts =
file: "./#{dest}/index.js"
format: 'iife'
assetFileNames: 'wasm/[name][extname]'
plugins: (if envRelease then [terser()] else [])
bundle = await rollup inOpts
await bundle.write outOpts
stmp = new Date().toLocaleString()
console.log "#{stmp} => coffee/wasm compilation done"
cb null, 0
compileSass = (cb) ->
targets =
targets: [{src: 'index.html', dest: "./#{dest}"}]
copyOnce: true
inOpts =
input: './style.sass'
plugins: [rollSass(), rollHtml 'index.html']
outOpts =
file: "./#{dest}/style.css"
exports: 'default'
format: 'cjs'
if watching
watcher = watch {inOpts..., output: outOpts}
watcher.on 'event', (event) =>
if event.code == 'ERROR' then console.log event.error
else if event.code == 'END'
stmp = new Date().toLocaleString()
console.log "#{stmp} => sass/html compilation done"
cb null, 0
else
sass = await rollup inOpts
await sass.write outOpts
stmp = new Date().toLocaleString()
console.log "#{stmp} => sass/html compilation done"
cb null, 0
copyDevin = (cb) ->
args = {force: true, recursive: true}
fs.cp './devin', "./#{dest}/devin", args, (r) ->
if r? then cb r, null
else
console.log 'copy devin files => done'
cb null, 0
createDir = (cb) ->
try
await fsp.mkdir "./#{dest}"
cb null, 0
catch e
if e.code = 'EEXIST'
if not envRelease
console.warn 'Warning: \'dist\' already exists'
cb null, 1
else cb e, null
launchServer = ->
console.log 'launching server...'
app = connect()
app.use(serveStatic './dist')
http.createServer(app).listen 5000
lrServer = lr.createServer()
console.log __dirname
lrServer.watch './dist'
# TASKS ###############################
task 'build', 'build the app (core + sass + wasm)', (options) ->
checkEnv options
console.log 'building...'
building = bach.series copyDevin, compileSass, compileJs
building (e, _) ->
if e?
console.log 'Something go wrong'
console.log e
else console.log 'building => done'
task 'clean', 'rm ./dist', (options) ->
console.log 'cleaning `dist`...'
rimraf './dist', (e) =>
if e? then console.log e
else console.log '`dist` removed successfully'
task 'sass', 'compile only sass and html', (options) ->
checkEnv options
sassing = bach.series createDir, compileSass
sassing (e, _) -> if e? then console.log e
task 'serve', 'launch a micro server and watch files', (options) ->
checkEnv options
if envRelease
console.error 'Impossible to use `serve` in `release` mode!'
else
watching = true
serving = bach.series copyDevin, compileJs,
(bach.parallel compileSass, launchServer)
serving (e, _) -> if e? then console.log e
task 'wasm', 'use rollup to compile wasm and coffee', (options) ->
checkEnv options
compileJs => 42