-
Notifications
You must be signed in to change notification settings - Fork 0
/
esbuild.config.js
76 lines (72 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
import babel from 'esbuild-plugin-babel';
import chokidar from 'chokidar';
import esbuild from 'esbuild';
import rails from 'esbuild-rails';
import http from 'http';
import path from 'path';
import {stimulusPlugin} from 'esbuild-plugin-stimulus';
const clients = [];
const watch = process.argv.includes('--watch');
const watchedDirectories = [
'./app/javascript/**/*.js',
'./app/views/**/*.html.erb',
'./app/views/**/*.rb',
'./app/assets/stylesheets/*.css',
];
const bannerJs = watch
? ' (() => new EventSource("http://localhost:8082").onmessage = () => location.reload())();'
: '';
const config = {
entryPoints: ['application.js'],
bundle: true,
outdir: path.join(process.cwd(), 'app/assets/builds'),
absWorkingDir: path.join(process.cwd(), 'app/javascript'),
watch: process.argv.includes('--watch'),
plugins: [
// Plugin to easily import Rails JS files, such as Stimulus controllers and channels
// https://github.com/excid3/esbuild-rails
rails(),
stimulusPlugin(),
// Configures bundle with Babel. Babel configuration defined in babel.config.js
// Babel translates JS code to make it compatible with older JS versions.
// https://github.com/nativew/esbuild-plugin-babel
babel()],
sourcemap: true,
logLevel: 'info',
treeShaking: true,
splitting: false,
chunkNames: 'chunks/[name]-[hash]',
incremental: watch,
banner: { js: bannerJs },
define: {
global: 'window',
RAILS_ENV: JSON.stringify(process.env.RAILS_ENV || 'development'),
VERSION: JSON.stringify(process.env.IMAGE_TAG || 'beta'),
COMMITHASH: JSON.stringify(process.env.GIT_COMMIT || ''),
},
};
if (watch) {
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);
(async () => {
const result = await esbuild.build(config);
chokidar.watch(watchedDirectories).on('all', (event, path) => {
if (path.includes('javascript')) {
console.log(`rebuilding ${path}`);
result.rebuild();
}
clients.forEach((res) => res.write('data: update\n\n'));
clients.length = 0;
});
})();
} else esbuild.build(config).catch(() => process.exit(1));