-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
build.ts
238 lines (200 loc) · 7.17 KB
/
build.ts
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import { watch as createWatcher } from 'chokidar'
import * as esbuild from 'esbuild'
import glob from 'globby'
import path from 'path'
import { debounce } from '../blaze/debounce'
import { flatten } from '../blaze/flatten'
import { handle } from '../blaze/handle'
import { map } from '../blaze/map'
import { omit } from '../blaze/omit'
import { pipe } from '../blaze/pipe'
import { transduce } from '../blaze/transduce'
import { depCheckPlugin } from './plugins/depCheckPlugin'
import { fixImportsPlugin } from './plugins/fixImportsPlugin'
import { onErrorPlugin } from './plugins/onErrorPlugin'
import { tscPlugin } from './plugins/tscPlugin'
export type BuildResult = esbuild.BuildResult
export type BuildOptions = esbuild.BuildOptions & {
name?: string
emitTypes?: boolean
outbase?: never // we don't support this
}
const DEFAULT_BUILD_OPTIONS = {
platform: 'node',
target: 'ES2020',
keepNames: true,
logLevel: 'error',
tsconfig: 'tsconfig.build.json',
incremental: process.env.WATCH === 'true',
metafile: true,
} as const
/**
* Apply defaults to allow us to build tree-shaken esm
* @param options the original build options
*/
const applyCjsDefaults = (options: BuildOptions): BuildOptions => ({
...DEFAULT_BUILD_OPTIONS,
format: 'cjs',
outExtension: { '.js': '.js' },
resolveExtensions: ['.ts', '.js', '.node'],
entryPoints: glob.sync('./src/**/*.{j,t}s', {
ignore: ['./src/__tests__/**/*'],
}),
mainFields: ['module', 'main'],
...options,
// outfile has precedence over outdir, hence these ternaries
outfile: options.outfile ? getOutFile(options) : undefined,
outdir: options.outfile ? undefined : getOutDir(options),
plugins: [...(options.plugins ?? []), fixImportsPlugin, tscPlugin(options.emitTypes), onErrorPlugin],
})
/**
* Create two deferred builds for esm and cjs. The one follows the other:
* - 1. The code gets compiled to an optimized tree-shaken esm output
* - 2. We take that output and compile it to an optimized cjs output
* @param options the original build options
* @returns if options = [a, b], we get [a-esm, a-cjs, b-esm, b-cjs]
*/
function createBuildOptions(options: BuildOptions[]) {
return flatten(
map(options, (options) => [
// we defer it so that we don't trigger glob immediately
() => applyCjsDefaults(options),
// ... here can go more steps
]),
)
}
/**
* We only want to trigger the glob search once we are ready, and that is when
* the previous build has finished. We get the build options from the deferred.
*/
function computeOptions(options: () => BuildOptions) {
return options()
}
/**
* Extensions are not automatically by esbuild set for `options.outfile`. We
* look at the set `options.outExtension` and we add that to `options.outfile`.
*/
function addExtensionFormat(options: BuildOptions) {
if (options.outfile && options.outExtension) {
const ext = options.outExtension['.js']
options.outfile = `${options.outfile}${ext}`
}
return options
}
/**
* If we don't have `options.outfile`, we default `options.outdir`
*/
function addDefaultOutDir(options: BuildOptions) {
if (options.outfile === undefined) {
options.outdir = getOutDir(options)
}
return options
}
/**
* Execute esbuild with all the configurations we pass
*/
async function executeEsBuild(options: BuildOptions) {
return [options, await esbuild.build(omit(options, ['name', 'emitTypes']))] as const
}
/**
* A blank esbuild run to do an analysis of our deps
*/
async function dependencyCheck(options: BuildOptions) {
// we only check our dependencies for a full build
if (process.env.DEV === 'true') return undefined
// Only run on test and publish pipelines on Buildkite
// Meaning we skip on GitHub Actions
// Because it's slow and runs for each job, during setup, making each job slower
if (process.env.CI && !process.env.BUILDKITE) return undefined
// we need to bundle everything to do the analysis
const buildPromise = esbuild.build({
entryPoints: glob.sync('**/*.{j,t}s', {
// We don't check dependencies in ecosystem tests because tests are isolated from the build.
ignore: ['./src/__tests__/**/*', './tests/e2e/**/*'],
gitignore: true,
}),
logLevel: 'silent', // there will be errors
bundle: true, // we bundle to get everything
write: false, // no need to write for analysis
outdir: 'out',
plugins: [depCheckPlugin(options.bundle)],
})
// we absolutely don't care if it has any errors
await buildPromise.catch(() => {})
return undefined
}
/**
* Execution pipeline that applies a set of actions
* @param options
*/
export async function build(options: BuildOptions[]) {
void transduce.async(options, dependencyCheck)
return transduce.async(
createBuildOptions(options),
pipe.async(computeOptions, addExtensionFormat, addDefaultOutDir, executeEsBuild, watch(options)),
)
}
/**
* Executes the build and rebuilds what is necessary
* @param builds
*/
const watch =
(allOptions: BuildOptions[]) =>
([options, result]: readonly [BuildOptions, esbuild.BuildResult | esbuild.BuildIncremental]) => {
if (process.env.WATCH !== 'true') return result
// common chokidar options for the watchers
const config = { ignoreInitial: true, useFsEvents: true, ignored: ['./src/__tests__/**/*'] }
// prepare the incremental builds watcher
const watched = getWatchedFiles(result)
const changeWatcher = createWatcher(watched, config)
// watcher for restarting a full rebuild
const restartWatcher = createWatcher(['./src/**/*'], config)
// triggers quick rebuild on file change
const fastRebuild = debounce(async () => {
const timeBefore = Date.now()
// we handle possible rebuild exceptions
const rebuildResult = await handle.async(() => {
return result?.rebuild?.()
})
if (rebuildResult instanceof Error) {
console.error(rebuildResult.message)
}
console.log(`${Date.now() - timeBefore}ms [${options.name ?? ''}]`)
}, 10)
// triggers a full rebuild on added file
const fullRebuild = debounce(async () => {
void changeWatcher.close() // stop all
// only one watcher will do this task
if (watchLock === false) {
watchLock = true
await build(allOptions)
}
}, 10)
changeWatcher.on('change', fastRebuild)
restartWatcher.once('add', fullRebuild)
restartWatcher.once('unlink', fullRebuild)
return undefined
}
// Utils ::::::::::::::::::::::::::::::::::::::::::::::::::
// so that only one watcher restarts a full build
let watchLock = false
// get a default directory if needed (no outfile)
function getOutDir(options: BuildOptions) {
if (options.outfile !== undefined) {
return path.dirname(options.outfile)
}
return options.outdir ?? 'dist'
}
// get the output file from an original path
function getOutFile(options: BuildOptions) {
if (options.outfile !== undefined) {
const dirname = getOutDir(options)
const filename = path.basename(options.outfile)
return `${dirname}/${filename}`
}
return undefined
}
// gets the files to be watched from esbuild
function getWatchedFiles(build: esbuild.BuildIncremental | esbuild.BuildResult | undefined) {
return Object.keys(build?.metafile?.inputs ?? {})
}