-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.config.ts
256 lines (228 loc) · 6.93 KB
/
build.config.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/**
* @file Configuration - Build
* @module config/build
* @see https://github.com/flex-development/mkbuild
*/
import { unassert } from '@flex-development/estree-util-unassert'
import {
defineBuildConfig,
type Config,
type OutputMetadata
} from '@flex-development/mkbuild'
import pathe from '@flex-development/pathe'
import {
DOT,
constant,
define,
entries,
get,
overwrite
} from '@flex-development/tutils'
import { ok } from 'devlop'
import { fromJs } from 'esast-util-from-js'
import type { BuildResult, Metafile, OutputFile, PluginBuild } from 'esbuild'
import type { Program } from 'estree'
import { attachComments } from 'estree-util-attach-comments'
import { toJs } from 'estree-util-to-js'
import { visit } from 'estree-util-visit'
import util from 'node:util'
import pkg from './package.json' assert { type: 'json' }
import tsconfig from './tsconfig.build.json' assert { type: 'json' }
declare module 'estree' {
interface BaseNode {
position?: import('unist').Position | undefined
}
}
/**
* Build configuration options.
*
* @const {Config} config
*/
const config: Config = defineBuildConfig({
charset: 'utf8',
entries: [
{
dts: 'only'
},
{
dts: false,
ignore: ['interfaces', 'types']
}
],
plugins: [
{
/**
* Plugin name.
*/
name: 'rollup-pluginutils-specifier',
/**
* Fix the `@rollup/pluginutils` module specifier.
*
* @this {void}
*
* @param {PluginBuild} build - esbuild plugin api
* @return {void} Nothing
*/
setup(build: PluginBuild): void {
/**
* Regular expression used to fix module specifiers.
*
* @const {RegExp} regex
*/
const regex: RegExp = /(["'])(@rollup\/pluginutils).+(["'])/
// fix module specifiers on build end
return void build.onEnd((result: BuildResult): void => {
ok(result.outputFiles, 'expected output files')
for (const output of result.outputFiles) {
if (/\.[cm]?[jt]s$/.test(output.path)) {
let { text } = output
text = text.replace(regex, '$1$2$1')
define(output, 'text', { get: constant(text) })
output.contents = new util.TextEncoder().encode(text)
}
}
})
}
},
{
/**
* Plugin name.
*/
name: unassert.name,
/**
* Remove assertions.
*
* @this {void}
*
* @param {PluginBuild} build - esbuild plugin api
* @return {void} Nothing
*/
setup(build: PluginBuild): void {
const {
absWorkingDir: cwd = process.cwd(),
format,
outdir = DOT
} = build.initialOptions
/**
* Directory to store development output files.
*
* @const {string} devdir
*/
const devdir: string = pathe.join(outdir, 'dev')
return void build.onEnd((result: BuildResult): void => {
ok(result.metafile, 'expected metafile')
ok(result.outputFiles, 'expected output files')
/**
* Output file filter.
*
* @const {RegExp} filter
*/
const filter: RegExp = /\.[cm]{0,1}js$/
/**
* Development output file metadata.
*
* @const {Metafile['outputs']} outputs
*/
const outputs: Metafile['outputs'] = {}
/**
* Development output files.
*
* @const {OutputFile[]} outputs
*/
const outputFiles: OutputFile[] = []
// get development output file metadata
for (const [path, output] of entries(result.metafile.outputs)) {
if (filter.test(path)) {
define(outputs, path.replace(outdir, devdir), { value: output })
}
}
// handle output files
for (const output of result.outputFiles) {
if (filter.test(output.path)) {
/**
* JavaScript syntax tree.
*
* @const {Program} tree
*/
const tree: Program = fromJs(output.text, {
module: format !== 'iife'
})
// attach comments
visit(tree, node => void (node.loc = node.position))
attachComments(tree, tree.comments)
delete tree.comments
// remove assertions
unassert(tree)
/**
* Relative path to output file.
*
* @const {string} outfile
*/
const outfile: string = output.path.replace(cwd + pathe.sep, '')
/**
* Path to development output file.
*
* @const {string} devpath
*/
const devpath: string = pathe.resolve(
cwd,
devdir,
outfile.replace(outdir + pathe.sep, '')
)
/**
* Output file text.
*
* @const {string} text
*/
const text: string = toJs(tree).value
/**
* Output file contents.
*
* @const {Uint8Array} contents
*/
const contents: Uint8Array = new util.TextEncoder().encode(text)
/**
* Output file metadata.
*
* @const {OutputMetadata} metadata
*/
const metadata: OutputMetadata = get(
result.metafile.outputs,
outfile
)
// assert output file metadata
ok(metadata, 'expected output file metadata')
// add development output file
outputFiles.push(
define({ contents: output.contents, path: devpath }, 'text', {
get: constant(output.text)
})
)
// update output file
define(output, 'text', { get: constant(text) })
output.contents = new util.TextEncoder().encode(output.text)
// update output file metadata
define(result.metafile.outputs, outfile, {
value: overwrite(metadata, {
bytes: contents.byteLength,
imports: metadata.imports.filter(({ path }) => {
return !unassert.MODULES_REGEX.test(path)
})
})
})
}
}
// update output files and metadata
result.outputFiles = [...outputFiles, ...result.outputFiles]
result.metafile.outputs = { ...outputs, ...result.metafile.outputs }
})
}
}
],
target: [
pkg.engines.node.replace(/^\D+/, 'node'),
tsconfig.compilerOptions.target
],
tsconfig: 'tsconfig.build.json'
})
export default config