Skip to content

Commit 767a685

Browse files
committed
refactor: add plugins
1 parent b2dabac commit 767a685

1 file changed

Lines changed: 51 additions & 0 deletions

File tree

src/utils/plugins/esbuild.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { createFilter } from '@rollup/pluginutils'
2+
import { transform, type TransformOptions } from 'esbuild'
3+
import type { Plugin } from 'rollup'
4+
5+
export function esbuild(options?: TransformOptions): Plugin {
6+
const isJs = /\.(?:[mc]?js|jsx)$/
7+
const filter = createFilter(/\.([cm]?ts|[jt]sx)$/)
8+
9+
return {
10+
name: 'esbuild',
11+
12+
resolveId(id, importer, options) {
13+
if (isJs.test(id) && importer) {
14+
return this.resolve(id.replace(/js(x?)$/, 'ts$1'), importer, options)
15+
}
16+
return null
17+
},
18+
19+
async transform(code, id) {
20+
if (!filter(id)) return null
21+
22+
const result = await transform(code, {
23+
loader: 'default',
24+
...options,
25+
sourcefile: id.replace(/\.[cm]ts/, '.ts'),
26+
})
27+
28+
return {
29+
code: result.code,
30+
map: result.map || null,
31+
}
32+
},
33+
34+
async renderChunk(code, { fileName }) {
35+
if (!options?.minify) return null
36+
37+
if (/\.d\.(c|m)?tsx?$/.test(fileName)) return null
38+
39+
const result = await transform(code, {
40+
...options,
41+
sourcefile: fileName,
42+
minify: true,
43+
})
44+
45+
return {
46+
code: result.code,
47+
map: result.map || null,
48+
}
49+
},
50+
}
51+
}

0 commit comments

Comments
 (0)