File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 = / \. (?: [ m c ] ? j s | j s x ) $ /
7+ const filter = createFilter ( / \. ( [ c m ] ? t s | [ j t ] s x ) $ / )
8+
9+ return {
10+ name : 'esbuild' ,
11+
12+ resolveId ( id , importer , options ) {
13+ if ( isJs . test ( id ) && importer ) {
14+ return this . resolve ( id . replace ( / j s ( 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 ( / \. [ c m ] t s / , '.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 ) ? t s x ? $ / . 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+ }
You can’t perform that action at this time.
0 commit comments