-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.config.ts
80 lines (71 loc) · 2.36 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
/**
* @file Configuration - Build
* @module config/build
* @see https://github.com/flex-development/mkbuild
*/
import { defineBuildConfig, type Config } from '@flex-development/mkbuild'
import type { BuildResult, OutputFile, PluginBuild } from 'esbuild'
import util from 'node:util'
import pkg from './package.json' assert { type: 'json' }
/**
* Build configuration options.
*
* @const {Config} config
*/
const config: Config = defineBuildConfig({
conditions: ['import', 'require', 'node', 'default'],
entries: [
{},
{
format: 'cjs',
plugins: [
{
name: 'add-module-exports',
setup({ initialOptions, onEnd }: PluginBuild): void {
const { format } = initialOptions
// do nothing if format is not commonjs
if (format !== 'cjs') return void format
// add module exports
return void onEnd(
(result: BuildResult<{ metafile: true; write: false }>): void => {
/**
* Output file objects.
*
* @const {OutputFile[]} outputFiles
*/
const outputFiles: OutputFile[] = result.outputFiles
return void (result.outputFiles = outputFiles.map(output => {
if (/dist\/(?:index|ponyfill)\.cjs$/.test(output.path)) {
// get sourceMappingURL comment
const [sourcemap = ''] = /\/\/#.+\n/.exec(output.text) ?? []
/**
* Output file content.
*
* @const {string} text
*/
const text: string =
output.text.replace(sourcemap, '') +
'module.exports = module.exports.default;\n' +
'module.exports.default = module.exports;\n' +
sourcemap
return {
...output,
contents: new util.TextEncoder().encode(text),
text
}
}
return output
}))
}
)
}
}
]
}
],
sourcemap: true,
sourcesContent: false,
target: 'node' + pkg.engines.node.replace(/^\D+/, ''),
tsconfig: 'tsconfig.build.json'
})
export default config