-
Notifications
You must be signed in to change notification settings - Fork 138
/
tsup.config.ts
67 lines (57 loc) · 2.03 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { readFile, writeFile } from 'fs/promises'
import { defineConfig } from 'tsup'
export default defineConfig({
target: 'node18',
format: ['esm'],
dts: { only: true },
outExtension: () => ({ js: '.mjs' }),
platform: 'node',
external: ['prettier', 'fs'],
minify: process.argv.includes('--minify'),
esbuildPlugins: [
// Patch Recast to fix a bug with leading/trailing spaces in template literals
{
// https://github.com/benjamn/recast/issues/611
name: 'patch-recast',
setup(build) {
build.onLoad({ filter: /recast\/lib\/patcher\.js$/ }, async (args) => {
let original = await readFile(args.path, 'utf8')
return {
contents: original
.replace(
'var nls = needsLeadingSpace(lines, oldNode.loc, newLines);',
'var nls = oldNode.type !== "TemplateElement" && needsLeadingSpace(lines, oldNode.loc, newLines);',
)
.replace(
'var nts = needsTrailingSpace(lines, oldNode.loc, newLines)',
'var nts = oldNode.type !== "TemplateElement" && needsTrailingSpace(lines, oldNode.loc, newLines)',
),
}
})
},
},
// Prepend some CJS interop helpers
{
name: 'patch-cjs-interop',
setup(build) {
build.onEnd(async () => {
return
let outfile = './dist/index.mjs'
let content = await readFile(outfile, 'utf-8')
// Prepend `createRequire`
let code = [
`import {createRequire} from 'module'`,
`import {dirname as __global__dirname__} from 'path'`,
`import {fileURLToPath} from 'url'`,
// CJS interop fixes
`const require=createRequire(import.meta.url)`,
`const __filename=fileURLToPath(import.meta.url)`,
`const __dirname=__global__dirname__(__filename)`,
]
content = `${code.join('\n')}\n${content}`
writeFile(outfile, content, 'utf-8')
})
},
},
],
})