forked from chaskiq/chaskiq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesbuild.config.mjs
99 lines (93 loc) · 2.76 KB
/
esbuild.config.mjs
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
import esbuild from 'esbuild';
import babel from './babel-esbuild.mjs';
import { createServer } from 'http'
import fs from 'fs'
const clients = []
const watch = process.argv.includes('--watch')
const minify = process.argv.includes('--minify')
const metafile = process.argv.includes('--metafile')
console.log("WATCH: ", watch)
console.log("MINIFY: ", minify)
console.log("METAFILE: ", metafile)
const watchOptions = {
onRebuild(error) {
clients.forEach((res) => res.write("data: update\n\n"));
clients.length = 0;
console.log(error ? error : "load complete");
},
}
esbuild
.build({
logLevel: 'info',
target: 'es2020',
sourcemap: watch,
define: {
'process.env.NODE_ENV': `"${process.env.NODE_ENV}"`,
'global': 'window',
'process.env.NODE_DEBUG': '""',
},
platform: 'browser',
inject: ['./esbuild/process-shim.js'],
entryPoints: [
"app/javascript/application.js",
"app/javascript/embed.js",
"app/javascript/article.js",
"app/javascript/docs.js",
"app/javascript/locales.js",
"app/javascript/twilio_phone_package.js"
],
bundle: true,
loader: {
'.png': 'file',
'.js': 'jsx',
},
metafile,
minify,
publicPath: "/assets",
assetNames: '[name]-[hash].digested',
//splitting: true,
//chunkNames: '[name]-[hash].digested',
//format: 'esm',
banner: {
js: `
${
watch ?
`(() => new EventSource("http://localhost:3001").onmessage = () => location.reload())();`
: ''
}
`,
},
watch: watch && watchOptions,
outdir: 'app/assets/builds',
plugins: [
babel({
filter: /\.([^cpj].*|c([^s].*)?|cs([^s].*)?|css.+|p([^n].*)?|pn([^g].*)?|png.+|j([^s].*)?|js([^o].*)?|jso([^n].*)?|json.+)$/,
})
]
})
.then(result => {
console.log(result)
if (watch) {
console.log('Build finished, watching for changes...')
} else {
console.log('Build finished, Congrats')
}
if(metafile){
fs.writeFileSync('esbuild-meta.json', JSON.stringify(result.metafile))
}
}).catch(result => {
console.log(result)
process.exit(1)
})
if( watch){
createServer((req, res) => {
return clients.push(
res.writeHead(200, {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
"Access-Control-Allow-Origin": "*",
Connection: "keep-alive",
}),
);
}).listen(3001);
}