Skip to content

Commit 933f6cd

Browse files
committed
refactor(typescript): unify program close + temp outDir cleanup via helper
- Add local closeProgramAndCleanup() that always cleans in finally - Use in buildEnd (non-watch) and closeWatcher (watch) Refs #1920
1 parent 9012668 commit 933f6cd

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

packages/typescript/src/index.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ export default function typescript(options: RollupTypescriptOptions = {}): Plugi
5353
}
5454
autoOutDir = null;
5555
};
56+
// Ensure the TypeScript watch program is closed and temp outDir is cleaned
57+
// even if closing throws. Call this from lifecycle hooks that need teardown.
58+
const closeProgramAndCleanup = () => {
59+
try {
60+
// ESLint doesn't understand optional chaining
61+
// eslint-disable-next-line
62+
program?.close();
63+
} finally {
64+
cleanupAutoOutDir();
65+
}
66+
};
5667

5768
const parsedOptions = parseTypescriptConfig(ts, tsconfig, compilerOptions, noForceEmit);
5869

@@ -182,18 +193,13 @@ export default function typescript(options: RollupTypescriptOptions = {}): Plugi
182193

183194
buildEnd() {
184195
if (this.meta.watchMode !== true) {
185-
// ESLint doesn't understand optional chaining
186-
// eslint-disable-next-line
187-
program?.close();
188-
cleanupAutoOutDir();
196+
closeProgramAndCleanup();
189197
}
190198
},
191199

192200
// Ensure program is closed and temp outDir is removed exactly once when watch stops
193201
closeWatcher() {
194-
// eslint-disable-next-line
195-
program?.close();
196-
cleanupAutoOutDir();
202+
closeProgramAndCleanup();
197203
},
198204

199205
renderStart(outputOptions) {

0 commit comments

Comments
 (0)