-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsup.config.ts
50 lines (43 loc) · 1.49 KB
/
tsup.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
/* eslint-disable no-await-in-loop */
import fsp from 'node:fs/promises'
import { resolve } from 'node:path'
import { basename, dirname } from 'node:path/posix'
import { fileURLToPath } from 'node:url'
import fg from 'fast-glob'
import { defineConfig } from 'tsup'
import { createLogger } from 'vite'
export default defineConfig({
format: [ 'cjs', 'esm' ], // generate cjs and esm files
entry: [ 'lib/**/*.ts' ],
// entryPoints: [ 'lib/index.ts' ],
clean: true, // rimraf dis
dts: true, // generate dts file for main module
skipNodeModulesBundle: true,
splitting: true,
target: 'es2020',
noExternal: [
// 'url-template' throws require() of ES Module
// bundle it into the build
'url-template',
],
async onSuccess() {
const logger = createLogger()
// Isomorphic `__dirname`
const _dirname = typeof __dirname !== 'undefined'
? __dirname
: dirname(fileURLToPath(import.meta.url))
// All CJS files
const files = await fg('**/*.cjs', {
ignore: [ 'chunk-*' ],
absolute: true,
cwd: resolve(_dirname, './dist'),
})
for (const file of files) {
logger.info(`PATCH_CJS ${basename(file)}`)
let code = await fsp.readFile(file, 'utf8')
code = code.replace('exports.default =', 'module.exports =')
code += 'exports.default = module.exports;'
await fsp.writeFile(file, code)
}
},
})