-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathbuild.js
More file actions
71 lines (63 loc) · 1.9 KB
/
build.js
File metadata and controls
71 lines (63 loc) · 1.9 KB
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
const esbuild = require('esbuild')
const { spawn } = require('child_process')
const fs = require('fs')
const path = require('path')
const args = process.argv.slice(2)
const watch = args.includes('--watch')
const deploy = args.includes('--deploy')
// Copy static assets to priv/static
const staticSrc = path.join(__dirname, 'static')
const staticDest = path.join(__dirname, '..', 'priv', 'static')
function copyDir(src, dest) {
fs.mkdirSync(dest, { recursive: true })
for (const entry of fs.readdirSync(src, { withFileTypes: true })) {
const from = path.join(src, entry.name)
const to = path.join(dest, entry.name)
entry.isDirectory() ? copyDir(from, to) : fs.copyFileSync(from, to)
}
}
if (fs.existsSync(staticSrc)) copyDir(staticSrc, staticDest)
// Tailwind CSS via CLI
const twArgs = ['--input', 'css/app.css', '--output', '../priv/static/css/app.css']
if (watch) twArgs.push('--watch')
if (deploy) twArgs.push('--minify')
const tailwind = spawn(process.execPath, ['./node_modules/.bin/tailwindcss', ...twArgs], {
stdio: 'inherit',
})
tailwind.on('error', err => {
console.error('tailwindcss error:', err.message)
process.exit(1)
})
tailwind.on('exit', (code) => {
if (code !== 0 && code !== null && !watch) {
process.exit(code)
}
})
// esbuild JS bundle
const esbuildOpts = {
entryPoints: ['js/app.js'],
bundle: true,
target: 'es2017',
outdir: '../priv/static/js',
minify: deploy,
sourcemap: !deploy,
logLevel: 'info',
}
if (watch) {
esbuild.context(esbuildOpts).then(ctx => {
ctx.watch()
// Exit cleanly when Phoenix closes stdin (server shutdown)
process.stdin.resume()
process.stdin.on('end', () => {
ctx.dispose()
tailwind.kill()
process.exit(0)
})
}).catch((err) => {
console.error('esbuild context error:', err.message)
tailwind.kill()
process.exit(1)
})
} else {
esbuild.build(esbuildOpts).catch(() => process.exit(1))
}