1
1
import path from 'node:path'
2
2
import process from 'node:process'
3
3
import { ResolverFactory } from 'oxc-resolver'
4
- import { rollup } from 'rollup'
4
+ import { rollup , type Plugin } from 'rollup'
5
5
import DtsPlugin from 'rollup-plugin-dts'
6
6
import { fsExists , fsRemove } from '../utils/fs'
7
7
import { typeAsserts } from '../utils/general'
@@ -17,8 +17,6 @@ export function getTempDtsDir(format: NormalizedFormat) {
17
17
return `${ TEMP_DTS_DIR } -${ format } `
18
18
}
19
19
20
- let resolver : ResolverFactory | undefined
21
-
22
20
export async function bundleDts (
23
21
options : ResolvedOptions ,
24
22
jsExtension : OutputExtension ,
@@ -35,12 +33,6 @@ export async function bundleDts(
35
33
path . resolve ( dtsOutDir , `${ key } .d.${ ext } ` ) ,
36
34
] ) ,
37
35
)
38
- resolver ||= new ResolverFactory ( {
39
- mainFields : [ 'types' ] ,
40
- conditionNames : [ 'types' , 'typings' , 'import' , 'require' ] ,
41
- extensions : [ '.d.ts' , '.ts' ] ,
42
- modules : [ 'node_modules' , 'node_modules/@types' ] ,
43
- } )
44
36
const build = await rollup ( {
45
37
input : dtsEntry ,
46
38
external : options . external ,
@@ -51,25 +43,7 @@ export async function bundleDts(
51
43
} ,
52
44
plugins : [
53
45
ExternalPlugin ( options , pkg ) as any ,
54
- {
55
- name : 'resolve-dts' ,
56
- async resolveId ( id , importer ) {
57
- if ( id [ 0 ] === '.' || path . isAbsolute ( id ) ) return
58
- if ( / \0 / . test ( id ) ) return
59
-
60
- const directory = importer ? path . dirname ( importer ) : process . cwd ( )
61
- const { path : resolved } = await resolver ! . async ( directory , id )
62
- if ( ! resolved ) return
63
-
64
- // try to resolve same-name d.ts
65
- if ( / [ c m ] ? j s x ? $ / . test ( resolved ) ) {
66
- const dts = resolved . replace ( / \. ( [ c m ] ? ) j s x ? $ / , '.d.$1ts' )
67
- return ( await fsExists ( dts ) ) ? dts : undefined
68
- }
69
-
70
- return resolved
71
- } ,
72
- } ,
46
+ ResolveDtsPlugin ( ) ,
73
47
DtsPlugin ( ) ,
74
48
] ,
75
49
} )
@@ -87,3 +61,34 @@ export async function bundleDts(
87
61
} )
88
62
await fsRemove ( dtsOutDir )
89
63
}
64
+
65
+ let resolver : ResolverFactory | undefined
66
+ export function ResolveDtsPlugin ( ) : Plugin {
67
+ return {
68
+ name : 'resolve-dts' ,
69
+ buildStart ( ) {
70
+ resolver ||= new ResolverFactory ( {
71
+ mainFields : [ 'types' ] ,
72
+ conditionNames : [ 'types' , 'typings' , 'import' , 'require' ] ,
73
+ extensions : [ '.d.ts' , '.ts' ] ,
74
+ modules : [ 'node_modules' , 'node_modules/@types' ] ,
75
+ } )
76
+ } ,
77
+ async resolveId ( id , importer ) {
78
+ if ( id [ 0 ] === '.' || path . isAbsolute ( id ) ) return
79
+ if ( / \0 / . test ( id ) ) return
80
+
81
+ const directory = importer ? path . dirname ( importer ) : process . cwd ( )
82
+ const { path : resolved } = await resolver ! . async ( directory , id )
83
+ if ( ! resolved ) return
84
+
85
+ // try to resolve same-name d.ts
86
+ if ( / [ c m ] ? j s x ? $ / . test ( resolved ) ) {
87
+ const dts = resolved . replace ( / \. ( [ c m ] ? ) j s x ? $ / , '.d.$1ts' )
88
+ return ( await fsExists ( dts ) ) ? dts : undefined
89
+ }
90
+
91
+ return resolved
92
+ } ,
93
+ }
94
+ }
0 commit comments