-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.js
98 lines (88 loc) · 2.96 KB
/
build.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
92
93
94
95
96
97
98
import autoprefixer from 'autoprefixer';
import * as esbuild from 'esbuild';
import {htmlPlugin} from 'esbuild-html-plugin';
import stylePlugin from 'esbuild-style-plugin';
import {rm} from 'node:fs/promises';
import {createRequire} from 'node:module';
import process from 'node:process';
const require = createRequire(import.meta.url);
const outdir = `dist`;
const nodeEnv = process.env.NODE_ENV ?? `production`;
const dev = nodeEnv !== `production`;
/** @type {import('esbuild').BuildOptions} */
const staticAppBuildOptions = {
entryPoints: [{out: `app`, in: `src/index.tsx`}],
entryNames: `[dir]/[name]-[hash]`,
bundle: true,
minify: !dev,
sourcemap: dev,
define: {
'process.env.CLIENT_ID': JSON.stringify(process.env.CLIENT_ID),
'process.env.NODE_ENV': JSON.stringify(nodeEnv),
},
target: `es2022`,
tsconfig: `tsconfig.base.json`,
outdir: `${outdir}/static`,
publicPath: `/static`,
plugins: [
stylePlugin({
postcss: {
// eslint-disable-next-line import/no-commonjs
plugins: [require(`tailwindcss`), autoprefixer],
},
}),
htmlPlugin({
outfile: `index.html`,
language: `en`,
createHeadElements: (outputUrls) => [
`<meta charset="utf-8" />`,
`<meta name="viewport" content="width=device-width, initial-scale=1" />`,
`<title>bookmark.wtf</title>`,
`<link rel="icon" href="/apple-touch-icon.png" />`,
`<link rel="apple-touch-icon" href="/apple-touch-icon.png" />`,
...outputUrls
.filter((url) => url.endsWith(`.css`))
.map((url) => `<link href="${url}" rel="stylesheet">`),
],
createBodyElements: (outputUrls) => [
`<main id="app"></main>`,
`<script src="${outputUrls.find(
(url) => url.includes(`app`) && url.endsWith(`.js`),
)}" async></script>`,
],
}),
],
};
/** @type {(handlerName: string) => import('esbuild').BuildOptions} */
function createLambdaBuildOptions(handlerName) {
return {
entryPoints: [{out: handlerName, in: `src/handlers/${handlerName}.ts`}],
entryNames: `[dir]/[name]`,
outExtension: {'.js': `.cjs`},
platform: `node`,
bundle: true,
minify: !dev,
sourcemap: dev,
define: {
'process.env.CLIENT_ID': JSON.stringify(process.env.CLIENT_ID),
'process.env.CLIENT_SECRET': JSON.stringify(process.env.CLIENT_SECRET),
'process.env.NODE_ENV': JSON.stringify(nodeEnv),
},
target: `es2022`,
tsconfig: `tsconfig.base.json`,
outdir: `${outdir}/api`,
};
}
const redirectLambdaBuildOptions = createLambdaBuildOptions(`redirect`);
if (process.argv.includes(`--watch`)) {
await Promise.all([
esbuild.context(staticAppBuildOptions).then((ctx) => ctx.watch()),
esbuild.context(redirectLambdaBuildOptions).then((ctx) => ctx.watch()),
]);
} else {
await rm(outdir, {recursive: true, force: true});
await Promise.all([
esbuild.build(staticAppBuildOptions),
esbuild.build(redirectLambdaBuildOptions),
]);
}