@@ -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,31 @@ 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 , _writeByteOrderMark , _onError , sourceFiles ) {
46
+ if ( sourceFiles ) {
47
+ for ( const sourceFile of sourceFiles ) {
48
+ if ( ! parsedOptions . fileNames . includes ( sourceFile . fileName ) ) {
49
+ parsedOptions . fileNames . push ( sourceFile . fileName ) ;
50
+ }
51
+ }
52
+ }
53
+
54
+ if ( parsedOptions . options . composite || parsedOptions . options . incremental ) {
55
+ tsCache . cacheCode ( fileName , data ) ;
56
+ }
57
+ emittedFiles . set ( fileName , data ) ;
58
+ } ,
59
+ status ( diagnostic ) {
60
+ watchProgramHelper . handleStatus ( diagnostic ) ;
61
+ } ,
62
+ transformers
63
+ } ) ;
64
+
40
65
const tsCache = new TSCache ( cacheDir ) ;
41
66
const emittedFiles = new Map < string , string > ( ) ;
42
67
const watchProgramHelper = new WatchProgramHelper ( ) ;
@@ -56,6 +81,14 @@ export default function typescript(options: RollupTypescriptOptions = {}): Plugi
56
81
name : 'typescript' ,
57
82
58
83
buildStart ( rollupOptions ) {
84
+ if ( typeof rollupOptions . input === 'string' ) {
85
+ rollupOptions . input = [ rollupOptions . input ] ;
86
+ }
87
+
88
+ if ( Array . isArray ( rollupOptions . input ) ) {
89
+ parsedOptions . fileNames = rollupOptions . input . map ( ( fileName ) => path . resolve ( fileName ) ) ;
90
+ }
91
+
59
92
emitParsedOptionsErrors ( ts , this , parsedOptions ) ;
60
93
61
94
preflight ( {
@@ -74,21 +107,7 @@ export default function typescript(options: RollupTypescriptOptions = {}): Plugi
74
107
program = null ;
75
108
}
76
109
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
- } ) ;
110
+ program = createProgram ( this ) ;
92
111
}
93
112
} ,
94
113
@@ -139,7 +158,6 @@ export default function typescript(options: RollupTypescriptOptions = {}): Plugi
139
158
140
159
if ( resolved ) {
141
160
if ( / \. d \. [ c m ] ? t s / . test ( resolved . extension ) ) return null ;
142
- if ( ! filter ( resolved . resolvedFileName ) ) return null ;
143
161
return path . normalize ( resolved . resolvedFileName ) ;
144
162
}
145
163
@@ -149,16 +167,20 @@ export default function typescript(options: RollupTypescriptOptions = {}): Plugi
149
167
async load ( id ) {
150
168
if ( ! filter ( id ) ) return null ;
151
169
152
- this . addWatchFile ( id ) ;
170
+ const resolvedId = path . resolve ( id ) ;
171
+
172
+ this . addWatchFile ( resolvedId ) ;
153
173
await watchProgramHelper . wait ( ) ;
154
174
155
- const fileName = normalizePath ( id ) ;
175
+ const fileName = normalizePath ( resolvedId ) ;
156
176
if ( ! parsedOptions . fileNames . includes ( fileName ) ) {
157
177
// Discovered new file that was not known when originally parsing the TypeScript config
158
- parsedOptions . fileNames . push ( fileName ) ;
178
+ parsedOptions . fileNames . push ( path . resolve ( fileName ) ) ;
179
+
180
+ createProgram ( this ) . close ( ) ;
159
181
}
160
182
161
- const output = findTypescriptOutput ( ts , parsedOptions , id , emittedFiles , tsCache ) ;
183
+ const output = findTypescriptOutput ( ts , parsedOptions , resolvedId , emittedFiles , tsCache ) ;
162
184
163
185
return output . code != null ? ( output as SourceDescription ) : null ;
164
186
} ,
0 commit comments