@@ -2,7 +2,7 @@ import * as path from 'path';
2
2
3
3
import { createFilter } from '@rollup/pluginutils' ;
4
4
5
- import type { Plugin , SourceDescription } from 'rollup' ;
5
+ import type { Plugin , PluginContext , SourceDescription } from 'rollup' ;
6
6
import type { Watch } from 'typescript' ;
7
7
8
8
import type { RollupTypescriptOptions } from '../types' ;
@@ -37,6 +37,23 @@ export default function typescript(options: RollupTypescriptOptions = {}): Plugi
37
37
tslib,
38
38
typescript : ts
39
39
} = getPluginOptions ( options ) ;
40
+ const createProgram = ( context : PluginContext ) =>
41
+ createWatchProgram ( ts , context , {
42
+ formatHost,
43
+ resolveModule,
44
+ parsedOptions,
45
+ writeFile ( fileName , data ) {
46
+ if ( parsedOptions . options . composite || parsedOptions . options . incremental ) {
47
+ tsCache . cacheCode ( fileName , data ) ;
48
+ }
49
+ emittedFiles . set ( fileName , data ) ;
50
+ } ,
51
+ status ( diagnostic ) {
52
+ watchProgramHelper . handleStatus ( diagnostic ) ;
53
+ } ,
54
+ transformers
55
+ } ) ;
56
+
40
57
const tsCache = new TSCache ( cacheDir ) ;
41
58
const emittedFiles = new Map < string , string > ( ) ;
42
59
const watchProgramHelper = new WatchProgramHelper ( ) ;
@@ -56,6 +73,14 @@ export default function typescript(options: RollupTypescriptOptions = {}): Plugi
56
73
name : 'typescript' ,
57
74
58
75
buildStart ( rollupOptions ) {
76
+ if ( typeof rollupOptions . input === 'string' ) {
77
+ parsedOptions . fileNames = [ path . resolve ( rollupOptions . input ) ] ;
78
+ }
79
+
80
+ if ( Array . isArray ( rollupOptions . input ) ) {
81
+ parsedOptions . fileNames = rollupOptions . input . map ( ( fileName ) => path . resolve ( fileName ) ) ;
82
+ }
83
+
59
84
emitParsedOptionsErrors ( ts , this , parsedOptions ) ;
60
85
61
86
preflight ( {
@@ -74,21 +99,7 @@ export default function typescript(options: RollupTypescriptOptions = {}): Plugi
74
99
program = null ;
75
100
}
76
101
if ( ! program ) {
77
- program = createWatchProgram ( ts , this , {
78
- formatHost,
79
- resolveModule,
80
- parsedOptions,
81
- writeFile ( fileName , data ) {
82
- if ( parsedOptions . options . composite || parsedOptions . options . incremental ) {
83
- tsCache . cacheCode ( fileName , data ) ;
84
- }
85
- emittedFiles . set ( fileName , data ) ;
86
- } ,
87
- status ( diagnostic ) {
88
- watchProgramHelper . handleStatus ( diagnostic ) ;
89
- } ,
90
- transformers
91
- } ) ;
102
+ program = createProgram ( this ) ;
92
103
}
93
104
} ,
94
105
@@ -139,7 +150,6 @@ export default function typescript(options: RollupTypescriptOptions = {}): Plugi
139
150
140
151
if ( resolved ) {
141
152
if ( / \. d \. [ c m ] ? t s / . test ( resolved . extension ) ) return null ;
142
- if ( ! filter ( resolved . resolvedFileName ) ) return null ;
143
153
return path . normalize ( resolved . resolvedFileName ) ;
144
154
}
145
155
@@ -149,16 +159,20 @@ export default function typescript(options: RollupTypescriptOptions = {}): Plugi
149
159
async load ( id ) {
150
160
if ( ! filter ( id ) ) return null ;
151
161
152
- this . addWatchFile ( id ) ;
162
+ const resolvedId = path . resolve ( id ) ;
163
+
164
+ this . addWatchFile ( resolvedId ) ;
153
165
await watchProgramHelper . wait ( ) ;
154
166
155
- const fileName = normalizePath ( id ) ;
167
+ const fileName = normalizePath ( resolvedId ) ;
156
168
if ( ! parsedOptions . fileNames . includes ( fileName ) ) {
157
169
// Discovered new file that was not known when originally parsing the TypeScript config
158
- parsedOptions . fileNames . push ( fileName ) ;
170
+ parsedOptions . fileNames . push ( path . resolve ( fileName ) ) ;
171
+
172
+ createProgram ( this ) . close ( ) ;
159
173
}
160
174
161
- const output = findTypescriptOutput ( ts , parsedOptions , id , emittedFiles , tsCache ) ;
175
+ const output = findTypescriptOutput ( ts , parsedOptions , resolvedId , emittedFiles , tsCache ) ;
162
176
163
177
return output . code != null ? ( output as SourceDescription ) : null ;
164
178
} ,
0 commit comments