-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.config.ts
203 lines (180 loc) · 6.47 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
/**
* @file Configuration - Build
* @module config/build
* @see https://github.com/flex-development/mkbuild
*/
import { EXT_DTS_REGEX } from '@flex-development/ext-regex'
import {
defineBuildConfig,
type Config,
type OutputMetadata
} from '@flex-development/mkbuild'
import * as mlly from '@flex-development/mlly'
import pathe from '@flex-development/pathe'
import type { BuildResult, OutputFile, PluginBuild } from 'esbuild'
import util from 'node:util'
import pkg from './package.json' assert { type: 'json' }
import tsconfig from './tsconfig.build.json' assert { type: 'json' }
/**
* Build configuration options.
*
* @const {Config} config
*/
const config: Config = defineBuildConfig({
charset: 'utf8',
entries: [
{},
{
format: 'cjs',
plugins: [
{
name: 'named-exports',
setup({ initialOptions, onEnd }: PluginBuild): void {
const { absWorkingDir = process.cwd(), format } = initialOptions
// do nothing if format is not commonjs
if (format !== 'cjs') return void format
// add named exports
return void onEnd(
async (
result: BuildResult<{ metafile: true; write: false }>
): Promise<void> => {
/**
* Named exports.
*
* @const {Set<string>} names
*/
const names: Set<string> = new Set<string>()
/**
* Output file objects.
*
* @const {OutputFile[]} outputFiles
*/
const outputFiles: OutputFile[] = []
/**
* Adds named exports to the given `output` file content.
*
* @param {string} output - Output file content
* @param {string[]} exports - Named exports
* @return {string} Output file content with named exports
*/
const nameExports = (
output: string,
exports: string[]
): string => {
if (exports.length > 0) {
// get sourceMappingURL comment
const [sourcemap = ''] = /\/\/#.+\n/.exec(output) ?? []
/**
* Output file content.
*
* @var {string} text
*/
let text: string = output.replace(sourcemap, '')
// add named exports
for (const name of exports) {
names.add(name)
text += `exports.${name} = module.exports.${name};\n`
}
// alias default export
text += 'exports = module.exports;\n'
// re-add sourceMappingURL comment
return (text += sourcemap)
}
return output
}
// add named exports to output file content
for (const output of result.outputFiles) {
// skip declaration files
if (EXT_DTS_REGEX.test(output.path)) {
outputFiles.push(output)
continue
}
// skip interface and type definition files
if (/(?:interfaces|types)\/.*$/.test(output.path)) {
outputFiles.push(output)
continue
}
/**
* Relative path to output file.
*
* **Note**: Relative to {@linkcode absWorkingDir}.
*
* @const {string} outfile
*/
const outfile: string = output.path
.replace(absWorkingDir, '')
.replace(/^\//, '')
/**
* {@linkcode output} metadata.
*
* @const {OutputMetadata} metadata
*/
const metadata: OutputMetadata =
result.metafile.outputs[outfile]!
// skip output files without entry points
if (!metadata.entryPoint) {
outputFiles.push(output)
continue
}
/**
* TypeScript source code for current output file.
*
* @const {string} code
*/
const code: string = (await mlly.getSource(
pathe.resolve(absWorkingDir, metadata.entryPoint)
)) as string
/**
* Output file content.
*
* @const {string} text
*/
const text: string = nameExports(
output.text,
mlly
.findExports(code)
.filter(s => s.syntax === mlly.StatementSyntaxKind.NAMED)
.flatMap(statement => statement.exports)
.map(name => name.replace(/^default as /, ''))
.filter(name => name !== 'default')
)
// add output file with named exports
outputFiles.push({
...output,
contents: new util.TextEncoder().encode(text),
text
})
}
return void (result.outputFiles = outputFiles.map(output => {
// add named exports to package entry point
if (output.path.endsWith('dist/index.cjs')) {
/**
* Output file content.
*
* @const {string} text
*/
const text: string = nameExports(output.text, [...names])
return {
...output,
contents: new util.TextEncoder().encode(text),
text
}
}
return output
}))
}
)
}
}
]
}
],
sourcemap: true,
sourcesContent: false,
target: [
pkg.engines.node.replace(/^\D+/, 'node'),
tsconfig.compilerOptions.target
],
tsconfig: 'tsconfig.build.json'
})
export default config