diff options
Diffstat (limited to 'esbuild.mjs')
-rwxr-xr-x | esbuild.mjs | 150 |
1 files changed, 150 insertions, 0 deletions
diff --git a/esbuild.mjs b/esbuild.mjs new file mode 100755 index 000000000..0df31be47 --- /dev/null +++ b/esbuild.mjs | |||
@@ -0,0 +1,150 @@ | |||
1 | #!/usr/bin/env node | ||
2 | import { build, serve } from 'esbuild'; | ||
3 | import { sassPlugin } from 'esbuild-sass-plugin'; | ||
4 | import { copy } from 'esbuild-plugin-copy'; | ||
5 | import glob from 'tiny-glob'; | ||
6 | import livereload from 'gulp-livereload'; | ||
7 | import * as fs from 'fs'; | ||
8 | import * as buildInfo from 'preval-build-info'; | ||
9 | import fsPkg from 'fs-extra'; | ||
10 | import chalk from 'chalk'; | ||
11 | import { performance } from 'perf_hooks'; | ||
12 | import moment from 'moment'; | ||
13 | |||
14 | const { log } = console; | ||
15 | |||
16 | const outDir = 'build'; | ||
17 | |||
18 | const staticAssets = () => [ | ||
19 | copy({ | ||
20 | assets: { | ||
21 | from: ['./src/internal-server/**/*.{json,ini,edge,css,png,sqlite}'], | ||
22 | to: ['./internal-server'], | ||
23 | }, | ||
24 | }), | ||
25 | copy({ | ||
26 | assets: { | ||
27 | from: ['./src/internal-server/ace'], | ||
28 | to: ['./internal-server'], | ||
29 | }, | ||
30 | }), | ||
31 | copy({ | ||
32 | assets: { | ||
33 | from: ['./recipes/archives/*.tar.gz'], | ||
34 | to: ['./recipes'], | ||
35 | }, | ||
36 | }), | ||
37 | copy({ | ||
38 | assets: { | ||
39 | from: ['./recipes/*.json'], | ||
40 | to: ['./recipes'], | ||
41 | }, | ||
42 | }), | ||
43 | copy({ | ||
44 | assets: { | ||
45 | from: ['./src/**/*.json'], | ||
46 | to: ['./'], | ||
47 | }, | ||
48 | }), | ||
49 | copy({ | ||
50 | assets: { | ||
51 | from: ['./src/assets/**'], | ||
52 | to: ['./assets'], | ||
53 | }, | ||
54 | }), | ||
55 | copy({ | ||
56 | assets: { | ||
57 | from: ['./src/index.html'], | ||
58 | to: ['./'], | ||
59 | }, | ||
60 | }), | ||
61 | ]; | ||
62 | |||
63 | const copyManualAssets = async () => { | ||
64 | if (!fs.existsSync(outDir)) { | ||
65 | fs.mkdirSync(outDir); | ||
66 | } | ||
67 | fs.copyFileSync('package.json', `${outDir}/package.json`); | ||
68 | fs.copyFileSync('electron-builder.npmrc', `${outDir}/.npmrc`); | ||
69 | |||
70 | const buildInfoData = { | ||
71 | timestamp: buildInfo.timestamp, | ||
72 | gitHashShort: buildInfo.gitHashShort, | ||
73 | gitBranch: buildInfo.gitBranch, | ||
74 | }; | ||
75 | await fsPkg.outputJson(`${outDir}/buildInfo.json`, buildInfoData); | ||
76 | }; | ||
77 | |||
78 | const runEsbuild = async () => { | ||
79 | const startTime = performance.now(); | ||
80 | |||
81 | let watch = false; | ||
82 | let isDev = false; | ||
83 | |||
84 | const myArgs = process.argv.slice(2); | ||
85 | if (myArgs.includes('--watch')) { | ||
86 | watch = true; | ||
87 | isDev = true; | ||
88 | } | ||
89 | log(chalk.blue(`Starting with args`), myArgs); | ||
90 | |||
91 | if (fs.existsSync(outDir)) { | ||
92 | fs.rmSync(outDir, { force: true, recursive: true }); | ||
93 | log(chalk.blue(`Cleaning`), outDir); | ||
94 | } | ||
95 | await copyManualAssets(); | ||
96 | |||
97 | // Source files | ||
98 | const entryPoints = await glob('./src/**/*.{ts,tsx,js,jsx}'); | ||
99 | |||
100 | // Scss entry points | ||
101 | entryPoints.push( | ||
102 | 'src/styles/main.scss', | ||
103 | 'src/styles/vertical.scss', | ||
104 | 'src/styles/animations.scss', | ||
105 | ); | ||
106 | |||
107 | // Run build | ||
108 | await build({ | ||
109 | entryPoints, | ||
110 | format: 'cjs', | ||
111 | minify: false, | ||
112 | minifyWhitespace: true, | ||
113 | minifyIdentifiers: true, | ||
114 | keepNames: true, | ||
115 | outdir: outDir, | ||
116 | watch: isDev && | ||
117 | watch && { | ||
118 | onRebuild(error, result) { | ||
119 | if (error) { | ||
120 | log(chalk.red(`watch build failed: ${error}`)); | ||
121 | } else { | ||
122 | log(chalk.blue(`watch build success:`), result); | ||
123 | livereload.reload(); | ||
124 | } | ||
125 | }, | ||
126 | }, | ||
127 | incremental: isDev && watch, | ||
128 | plugins: [sassPlugin(), ...staticAssets()], | ||
129 | }); | ||
130 | |||
131 | if (watch) { | ||
132 | const serveResult = await serve( | ||
133 | { | ||
134 | servedir: outDir, | ||
135 | port: 8080, | ||
136 | }, | ||
137 | {}, | ||
138 | ); | ||
139 | log(chalk.green(`Listening on ${serveResult.host}:${serveResult.port}`)); | ||
140 | livereload.listen(); | ||
141 | } else { | ||
142 | const endTime = performance.now(); | ||
143 | const duration = endTime - startTime; | ||
144 | log( | ||
145 | chalk.green(`Completed build in ${moment(duration).format('ss.m')}sec`), | ||
146 | ); | ||
147 | } | ||
148 | }; | ||
149 | |||
150 | runEsbuild(); | ||