/
build.mjs
154 lines (135 loc) · 3.66 KB
/
build.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
142
143
144
145
146
147
148
149
150
151
152
153
154
import preactIslandPlugin from '@barelyhuman/preact-island-plugins/esbuild'
import autoprefixer from 'autoprefixer'
import * as esbuild from 'esbuild'
import { nodeExternalsPlugin } from 'esbuild-node-externals'
import fs from 'fs/promises'
import process from 'node:process'
import { dirname, resolve } from 'path'
import postcss from 'postcss'
import tailwindcss from 'tailwindcss'
import glob from 'tiny-glob'
import * as url from 'url'
import Watcher from 'watcher'
import nodemon from 'nodemon'
import { green, dim, stripColors } from 'kolorist'
const __dirname = dirname(url.fileURLToPath(import.meta.url))
const atomic = true
let nodemonRunner
const watch = process.argv.slice(2).includes('-w')
const foldersToExclude = ['dist', '.generated', 'node_modules']
const currTime = () => String(dim(new Date().toLocaleString()))
const dimLog = msg => log(dim(msg))
// remove all ansi colors
const justText = msg => {
// taken from https://github.com/chalk/ansi-regex/blob/02fa893d619d3da85411acc8fd4e2eea0e95a9d9/index.js
return stripColors(msg)
}
const log = msg => {
const time = currTime()
const cols = process.stdout.columns
const msgLen = justText(msg).length
const timLen = justText(time).length
console.log(`${msg}${' '.repeat(cols - timLen - msgLen)}${time}`)
}
const commonConfig = {
bundle: true,
// logLevel: 'info',
jsx: 'automatic',
minify: true,
loader: {
'.js': 'jsx',
'.woff': 'dataurl',
'.woff2': 'dataurl',
'.svg': 'dataurl',
},
target: 'node14',
format: 'cjs',
jsxImportSource: 'preact',
}
async function main(options = {}) {
const buildSuccess = green('Done')
await server()
log(`${buildSuccess}: Server`)
await buildPostcss()
log(`${buildSuccess}: PostCss`)
if (!nodemonRunner) {
nodemonRunner = nodemon({
script: 'dist/server.js',
restartable: 'rs',
ext: 'js,json,css',
verbose: false,
watch: ['server', 'components', 'lib', 'pages'],
})
}
nodemonRunner.restart()
watch &&
!options.rebuild &&
nodemonRunner
.on('start', function () {
log(`${buildSuccess}: Nodemon`)
})
.on('exit', function () {})
.on('error', function (err) {
console.error(err)
})
.on('restart', function (files) {
dimLog(`Restarting, ${files.join(',')}`)
})
}
async function buildPostcss() {
const css = await fs.readFile('./styles/global.css', 'utf8')
postcss([autoprefixer, tailwindcss])
.process(css, { from: './styles/global.css', to: 'dist/server.css' })
.then(result => {
fs.writeFile('dist/server.css', result.css, () => true)
})
}
async function server() {
return esbuild.build({
...commonConfig,
metafile: true,
platform: 'node',
entryPoints: ['./server.js'],
plugins: [
nodeExternalsPlugin(),
preactIslandPlugin({
client: {
output: '.islands',
},
tsconfig: './tsconfig.json',
baseURL: '/islands/',
atomic,
rootDir: '.',
}),
],
outfile: 'dist/server.js',
})
}
try {
await main()
} catch (err) {
// digest
console.log({ err })
}
const isInExclusionFolders = path =>
foldersToExclude
.map(x => resolve(__dirname, x))
.filter(x => path.startsWith(x)).length > 0
if (watch) {
const gPaths = await glob('./**/*.{js,jsx,css}', { absolute: true })
const validPaths = gPaths.filter(x => !isInExclusionFolders(x))
const watcher = new Watcher(validPaths)
watcher.on('error', error => {
// console.error(error)
})
watcher.on('close', () => {
process.exit(0)
})
watcher.on('change', async () => {
try {
await main({ rebuild: true })
} catch (err) {
// digest
}
})
}