-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesbuild.dashboard.js
164 lines (150 loc) · 3.56 KB
/
esbuild.dashboard.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
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
const postCssPlugin = require('esbuild-style-plugin')
const esbuild = require('esbuild')
const path = require('path')
const fs = require('fs')
const server = require('esbuild-server')
const isProduction = process.env.NODE_ENV === 'production'
const buildOptions = {
entryPoints: ['src/dashboardWebView/dashboard.tsx'],
bundle: true,
outdir: 'dist',
minify: isProduction,
sourcemap: isProduction ? 'external' : true,
target: ['es2020'],
format: 'esm',
loader: {
'.tsx': 'tsx',
'.ts': 'ts',
'.css': 'css',
},
metafile: true,
define: {
'process.env.NODE_ENV': JSON.stringify(
process.env.NODE_ENV || 'development',
),
},
plugins: [
{
name: 'provide-process',
setup(build) {
build.onResolve({ filter: /^process$/ }, (args) => ({
path: require.resolve('process/browser'),
}))
},
},
{
name: 'rebuild-notify',
setup(build) {
build.onEnd((result) => {
console.log(`build ended with ${result.errors.length} errors`)
// HERE: somehow restart the server from here, e.g., by sending a signal that you trap and react to inside the server.
})
},
},
],
}
const cssBuildOptions = {
entryPoints: ['src/dashboardWebView/style/dashboard.css'],
outdir: 'dist/style',
bundle: true,
minify: isProduction,
loader: {
'.css': 'file',
},
sourcemap: isProduction ? 'external' : true,
define: {
'process.env.NODE_ENV': JSON.stringify(
process.env.NODE_ENV || 'development',
),
},
plugins: [
postCssPlugin({
postcss: {
plugins: [require('tailwindcss'), require('autoprefixer')],
},
}),
{
name: 'rebuild-notify',
setup(build) {
build.onEnd((result) => {
console.log(`build ended with ${result.errors.length} errors`)
// HERE: somehow restart the server from here, e.g., by sending a signal that you trap and react to inside the server.
})
},
},
],
}
if (isProduction) {
buildOptions.entryNames = '[name].[hash]'
buildOptions.chunkNames = '[name].[hash]'
}
const build = async () => {
try {
const result = await esbuild.build(buildOptions)
const cssResult = await esbuild.build(cssBuildOptions)
console.log({ result })
const ctx = await esbuild.context(buildOptions)
const cssCtx = await esbuild.context(cssBuildOptions)
// Generate manifest
const manifest = Object.keys(result.metafile.outputs).reduce((acc, key) => {
const relativePath = path.relative('dist', key)
acc[relativePath] = relativePath
return acc
}, {})
fs.writeFileSync(
path.join(__dirname, 'dist', 'dashboard.manifest.json'),
JSON.stringify(manifest, null, 2),
)
console.log('Build complete. Manifest generated.')
if (isProduction) {
// Generate bundle analysis
const analyzerHtml = `
<html>
<body>
<pre>${JSON.stringify(result.metafile, null, 2)}</pre>
</body>
</html>
`
fs.writeFileSync(
path.join(__dirname, 'dist', 'dashboard.html'),
analyzerHtml,
)
console.log('Bundle analysis generated.')
} else {
await ctx.watch()
await cssCtx.watch()
}
return result
} catch (error) {
console.error('Build failed:', error)
process.exit(1)
}
}
const main = async () => {
await build()
console.log('Build complete.')
// Development server
if (!isProduction) {
server
.createServer(
{
...buildOptions,
allowOverwrite: true,
entryPoints: ['dist/dashboard.js', 'dist/style/dashboard.css'],
},
{
port: 9000,
headers: {
'Access-Control-Allow-Origin': '*',
},
},
)
.start()
.then(() => {
console.log('Server started on port 9000')
})
} else {
process.exit(0)
}
}
main()