-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesbuild.config.ts
101 lines (80 loc) · 2.23 KB
/
esbuild.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
import * as esbuild from 'esbuild'
import stripJsonComments from 'strip-json-comments'
import fs from 'fs'
import { rimrafSync } from 'rimraf'
import TsconfigPathsPlugin from './plugins/esbuild-plugin-tsconfig-paths'
import EsbuildPluginDtsBundleGenerator from 'esbuild-plugin-dts-bundle-generator'
import { findUpSync } from 'find-up'
const entryPoints = [
{
in: 'src/index.ts',
out: 'index'
}
]
const loadJSON = (p: string) => {
try {
const data = stripJsonComments(fs.readFileSync(p, 'utf-8').toString(), {
trailingCommas: true,
whitespace: true
})
return JSON.parse(data)
} catch (e) {
throw new Error(`Cannot load json for '${p}'`)
}
}
const defaultOutExtension = ({ format, pkgType }: { format: esbuild.Format, pkgType?: string }): { js: string, dts: string } => {
let jsExtension = '.js'
let dtsExtension = '.d.ts'
const isModule = pkgType === 'module'
if (isModule && 'cjs' === format) {
jsExtension = '.cjs'
dtsExtension = '.d.cts'
} else if (!isModule && 'esm' === format) {
jsExtension = '.mjs'
dtsExtension = '.d.mts'
} else if ('iife' === format) {
jsExtension = '.global.js'
}
return {
js: jsExtension,
dts: dtsExtension,
}
}
const toEsbuildOptions = (format: esbuild.Format): esbuild.BuildOptions => {
const pkg = findUpSync(['package.json'])
if (!pkg) throw new Error('Cannot find package.json')
const pkgJSON = loadJSON(pkg)
const extension = defaultOutExtension({ format, pkgType: pkgJSON.type })
return {
entryPoints,
platform: 'node',
bundle: true,
splitting: false,
minify: false,
sourcemap: false,
outdir: 'dist',
outExtension: {
'.js': extension.js,
},
format,
logLevel: 'debug',
charset: 'utf8',
plugins: [
TsconfigPathsPlugin({}),
EsbuildPluginDtsBundleGenerator({
entryPoints,
tsconfig: './tsconfig.json'
}),
]
}
}
(() => {
const rm = rimrafSync('dist')
console.log(`rm 'dist'`, rm)
const extensions: Array<esbuild.Format> = ['cjs', 'esm']
extensions.forEach(async ext => {
const res = await esbuild.build(toEsbuildOptions(ext))
const { errors } = res
console.log(`built ${ext} ${errors?.length ?? 0} errors`)
})
})()