forked from martpie/museeks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesbuild.mjs
141 lines (124 loc) · 3.9 KB
/
esbuild.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
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
import fs from 'fs';
import path from 'path';
import esbuild from 'esbuild';
import postCssPlugin from 'esbuild-plugin-postcss2';
import esbuildPluginSvg from 'esbuild-plugin-svg';
import htmlPlugin from '@chialab/esbuild-plugin-html';
import postCssImport from 'postcss-import';
import postCssNested from 'postcss-nested';
import postCssUrl from 'postcss-url';
const shouldWatch = process.argv.includes('--watch'); // infinite watch loop
const isProduction = process.argv.includes('--production');
const minify = !shouldWatch && isProduction;
/*
|--------------------------------------------------------------------------
| Helpers
|--------------------------------------------------------------------------
*/
// const onWatch = (name) => {
// /** @type esbuild.WatchMode */
// const watchMode = {
// onRebuild: (_errors, results) => {
// console.log(`${name} rebuilt with ${results.errors.length} errors and ${results.warnings.length} warnings`);
// },
// };
// return watchMode;
// };
/*
|--------------------------------------------------------------------------
| Main process config
|--------------------------------------------------------------------------
*/
/** @type esbuild.BuildOptions */
const configMain = {
entryPoints: ['./src/main/main.ts'],
bundle: true,
outfile: 'dist/main/bundle.js',
platform: 'node',
target: 'node16.13',
external: ['electron'],
// watch: shouldWatch ? onWatch('main') : null,
incremental: shouldWatch,
minify,
};
/*
|--------------------------------------------------------------------------
| Renderer process config
|--------------------------------------------------------------------------
*/
/** @type esbuild.BuildOptions */
let configRenderer = {
entryPoints: ['./src/renderer/app.html'],
bundle: true,
assetNames: '[name]',
outdir: 'dist/renderer',
platform: 'browser',
target: 'chrome91',
external: ['electron', 'fs', 'stream', 'path', 'platform', 'assert', 'os', 'constants', 'util', 'events'],
// watch: shouldWatch ? onWatch('renderer') : null,
incremental: shouldWatch,
minify,
sourcemap: !isProduction,
plugins: [
htmlPlugin(),
esbuildPluginSvg(),
postCssPlugin.default({
plugins: [postCssImport, postCssNested, postCssUrl({ url: 'inline' })],
// https://github.com/martonlederer/esbuild-plugin-postcss2/pull/30
modules: true,
rootDir: process.cwd(),
sassOptions: {},
lessOptions: {},
stylusOptions: {},
writeToFile: true,
}),
],
loader: {
'.eot': 'file',
'.woff': 'file',
'.woff2': 'file',
'.svg': 'file',
'.ttf': 'file',
'.png': 'file',
},
};
/*
|--------------------------------------------------------------------------
| Bundlers (prod + dev)
|--------------------------------------------------------------------------
*/
async function bundle() {
try {
await Promise.all([esbuild.build(configMain), esbuild.build(configRenderer)]);
console.log('[museeks] built');
} catch {
process.exit(1);
}
}
async function bundleWatch() {
try {
let [resultMain, resultRenderer] = await Promise.all([esbuild.build(configMain), esbuild.build(configRenderer)]);
console.log('[museeks] built, listening to change...');
// Custom watcher
// See https://github.com/martonlederer/esbuild-plugin-postcss2/issues/19
if (shouldWatch) {
fs.watch(path.join('.', 'src'), { recursive: true }, async (eventType, filename) => {
console.log(`[museeks][${eventType}] ${filename}`);
try {
await Promise.all([resultMain.rebuild(), resultRenderer.rebuild()]);
} catch {
// nothing
}
console.log('[museeks] rebuilt');
});
}
} catch {
// nothing
}
}
/*
|--------------------------------------------------------------------------
| Fire things up
|--------------------------------------------------------------------------
*/
shouldWatch ? bundleWatch() : bundle();