This repository has been archived by the owner on Jul 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
esbuild.config.js
91 lines (80 loc) · 2.35 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
#!/usr/bin/env node
// Esbuild is configured with 3 modes:
//
// `yarn build` - Build JavaScript and exit
// `yarn build --watch` - Rebuild JavaScript on change
// `yarn build --reload` - Reloads page when views, JavaScript, or stylesheets change
//
// Minify is enabled when "RAILS_ENV=production"
// Sourcemaps are enabled in non-production environments
const esbuild = require('esbuild')
const path = require('path')
const rails = require('esbuild-rails')
const clients = []
const entryPoints = [
"application.js",
"administrate.js"
]
const watchDirectories = [
"./app/javascript/**/*.js",
"./app/views/**/*.html.erb",
"./app/assets/stylesheets/*.css"
]
const config = {
absWorkingDir: path.join(process.cwd(), "app/javascript"),
bundle: true,
entryPoints: entryPoints,
outdir: path.join(process.cwd(), "app/assets/builds"),
plugins: [rails()],
sourcemap: process.env.RAILS_ENV != "production"
}
async function buildAndReload() {
const chokidar = require('chokidar')
const http = require('http')
// Reload uses an HTTP server as an even stream to reload the browser
http.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(8082);
let result = await esbuild.build({
...config,
incremental: true,
banner: {
js: ' (() => new EventSource("http://localhost:8082").onmessage = () => location.reload())();',
},
})
chokidar.watch(watchDirectories).on('all', (event, path) => {
if (path.includes("javascript")) {
result.rebuild()
}
clients.forEach((res) => res.write('data: update\n\n'))
clients.length = 0
});
}
if (process.argv.includes("--reload")) {
buildAndReload()
} else if (process.argv.includes("--watch")) {
// Watch uses esbuild's watch option
const watch = process.argv.includes("--watch") && {
onRebuild(error) {
if (error) console.error("[watch] build failed", error);
else console.log("[watch] build finished");
},
};
esbuild.build({
...config,
watch: watch,
}).catch(() => process.exit(1));
} else {
// Standard esbuild
esbuild.build({
...config,
minify: process.env.RAILS_ENV == "production",
}).catch(() => process.exit(1));
}