(options: RollupTypescriptOptions = {})
| 26 | import TSCache from './tscache'; |
| 27 | |
| 28 | export default function typescript(options: RollupTypescriptOptions = {}): Plugin { |
| 29 | const { |
| 30 | cacheDir, |
| 31 | compilerOptions, |
| 32 | exclude, |
| 33 | filterRoot, |
| 34 | include, |
| 35 | outputToFilesystem, |
| 36 | noForceEmit, |
| 37 | transformers, |
| 38 | recreateTransformersOnRebuild, |
| 39 | tsconfig, |
| 40 | tslib, |
| 41 | typescript: ts |
| 42 | } = getPluginOptions(options); |
| 43 | const tsCache = new TSCache(cacheDir); |
| 44 | const emittedFiles = new Map<string, string>(); |
| 45 | const watchProgramHelper = new WatchProgramHelper(); |
| 46 | let autoOutDir: string | null = null; |
| 47 | // Centralize temp outDir cleanup to avoid duplication/drift across hooks |
| 48 | const cleanupAutoOutDir = () => { |
| 49 | if (!autoOutDir) return; |
| 50 | try { |
| 51 | fs.rmSync(autoOutDir, { recursive: true, force: true }); |
| 52 | } catch { |
| 53 | // ignore cleanup failures |
| 54 | } |
| 55 | autoOutDir = null; |
| 56 | }; |
| 57 | // Ensure the TypeScript watch program is closed and temp outDir is cleaned |
| 58 | // even if closing throws. Call this from lifecycle hooks that need teardown. |
| 59 | const closeProgramAndCleanup = () => { |
| 60 | try { |
| 61 | // ESLint doesn't understand optional chaining |
| 62 | // eslint-disable-next-line |
| 63 | program?.close(); |
| 64 | } finally { |
| 65 | cleanupAutoOutDir(); |
| 66 | } |
| 67 | }; |
| 68 | |
| 69 | const parsedOptions = parseTypescriptConfig(ts, tsconfig, compilerOptions, noForceEmit); |
| 70 | |
| 71 | // When processing JS via allowJs, redirect emit output away from source files |
| 72 | // to avoid TS5055 (cannot write file because it would overwrite input file). |
| 73 | // We only set a temp outDir if the user did not configure one. |
| 74 | if (parsedOptions.options.allowJs && !parsedOptions.options.outDir) { |
| 75 | // Create a unique temporary outDir to avoid TS5055 when emitting JS |
| 76 | autoOutDir = fs.mkdtempSync(path.join(os.tmpdir(), 'rollup-plugin-typescript-allowjs-')); |
| 77 | parsedOptions.options.outDir = autoOutDir; |
| 78 | } |
| 79 | |
| 80 | // Determine default include pattern. By default we only process TS files. |
| 81 | // When the consumer enables `allowJs` in their tsconfig/compiler options, |
| 82 | // also include common JS extensions so modern JS syntax in .js files is |
| 83 | // downleveled by TypeScript as expected. |
| 84 | const defaultInclude = parsedOptions.options.allowJs |
| 85 | ? '{,**/}*.{cts,mts,ts,tsx,js,jsx,mjs,cjs}' |
no test coverage detected