generated from ministryofjustice/hmpps-template-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesbuild.config.js
94 lines (85 loc) · 2.68 KB
/
esbuild.config.js
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
/* eslint-disable no-console */
/* eslint-disable import/no-extraneous-dependencies */
const path = require('path')
const { glob } = require('glob')
const chokidar = require('chokidar')
const { spawn } = require('child_process')
const buildAssets = require('./assets.config')
const buildApp = require('./app.config')
const nwDir = path.dirname(process.execPath)
const cwd = process.cwd()
const buildConfig = {
isProduction: process.env.NODE_ENV === 'production',
sourcemap: process.env.NODE_ENV === 'development' ? 'inline' : false,
app: {
outDir: path.join(cwd, 'dist'),
entryPoints: path.join(cwd, 'server.ts'),
copy: [
{
from: path.join(cwd, 'server/views/**/*'),
to: path.join(cwd, 'dist/server/views'),
},
],
},
assets: {
outDir: path.join(cwd, 'dist/assets'),
entryPoints: glob.sync([path.join(cwd, 'assets/js/index.js'), path.join(cwd, 'assets/scss/application.scss')]),
copy: [
{
from: path.join(cwd, 'assets/images/**/*'),
to: path.join(cwd, 'dist/assets/images'),
},
{
from: path.join(cwd, 'package.json'),
to: path.join(cwd, 'dist/package.json'),
},
],
clear: glob.sync([path.join(cwd, 'dist/assets/{css,js}')]),
},
}
function main() {
const chokidarOptions = {
persistent: true,
ignoreInitial: true,
}
const args = process.argv
if (args.includes('--build')) {
Promise.all([buildApp(buildConfig), buildAssets(buildConfig)]).catch(() => process.exit(1))
}
if (args.includes('--feature-dev-server')) {
let serverProcess = null
chokidar.watch(['dist']).on('all', () => {
if (serverProcess) serverProcess.kill()
serverProcess = spawn(
`${nwDir}/node`,
['--inspect=0.0.0.0', '--enable-source-maps', 'dist/server.js', ' | bunyan -o short'],
{
stdio: 'inherit',
},
)
})
}
if (args.includes('--dev-server')) {
let serverProcess = null
chokidar.watch(['dist']).on('all', () => {
if (serverProcess) serverProcess.kill()
serverProcess = spawn(
`${nwDir}/node`,
['--inspect=0.0.0.0', '--enable-source-maps', '-r', 'dotenv/config', 'dist/server.js', ' | bunyan -o short'],
{
stdio: 'inherit',
},
)
})
}
if (args.includes('--watch')) {
console.log('\u{1b}[1m\u{1F52D} Watching for changes...\u{1b}[0m')
// Assets
chokidar.watch(['assets/**/*'], chokidarOptions).on('all', () => buildAssets(buildConfig).catch(console.error))
// App
chokidar
.watch(['server/**/*', 'app/**/*'], { ...chokidarOptions, ignored: ['**/*.test.ts'] })
.on('all', () => buildApp(buildConfig).catch(console.error))
}
}
main()