-
Notifications
You must be signed in to change notification settings - Fork 0
/
esbuild.mjs
109 lines (99 loc) · 2.91 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
import esbuild from "esbuild";
import { NodeModulesPolyfillPlugin } from "@esbuild-plugins/node-modules-polyfill";
import { spawn, spawnSync } from "child_process";
const releaseBuild = process.env.BUILD === 'true'
const targets = (process.env.TARGETS || '').split(',')
const buildDir = releaseBuild ? 'dist' : 'build'
if (targets.length === 0) {
throw new Error('TARGETS is not defined')
}
const buildExtension = targets.some(t => t === 'extension') || releaseBuild
const baseOptions = {
bundle: true,
loader: {
".ts": "tsx",
".tsx": "tsx",
".woff2": "file",
".woff": "file",
".html": "copy",
".json": "copy",
".ico": "copy",
},
plugins: [
NodeModulesPolyfillPlugin(),
],
minify: releaseBuild,
// sourcemap: 'external',
sourcemap: 'linked',
define: {
"global": "window",
"process.env.PRODUCTION": releaseBuild ? '"true"' : '"false"'
},
entryNames: "[name]",
logLevel: 'info',
}
const runTailwindBuild = (target, watch, outfile) => {
console.log(`[${target}] building Tailwind CSS...`);
try {
const command = 'npx';
const args = [
'tailwindcss',
'build',
'-i', 'src/tailwind.css',
'-o', outfile
];
if (watch) {
args.push('--watch')
spawn(command, args, {
stdio: 'inherit'
})
} else {
spawnSync(command, args, {
stdio: 'inherit'
});
}
console.log(`[${target}] CSS build successful!`);
} catch (error) {
console.error("Error building Tailwind CSS:", error.message);
}
};
async function doBuild(target, options, port) {
runTailwindBuild(target, !releaseBuild, `${options.outdir}/tailwind.css`);
if (releaseBuild) {
await esbuild.build(options);
} else {
try {
const context = await esbuild
.context(options);
await context.rebuild()
if (port !== undefined) {
console.log('serving', options.outdir)
context.serve({
port: port,
servedir: options.outdir,
fallback: `${buildDir}/site/index.html`,
onRequest: args => {
console.log(args.method, args.path)
}
})
}
await context.watch()
} catch (e) {
console.error('failed to build: ' + e)
}
}
}
await Promise.all([
buildExtension && doBuild('extension', {
...baseOptions,
entryPoints: [
"./src/tab.tsx",
"./src/options.tsx",
"./src/background.tsx",
"./src/options.html",
"./src/tab.html",
"./src/manifest.json",
],
outdir: `${buildDir}/extension/`,
})
])