|
1 | 1 | import * as path from 'path'; |
| 2 | +import * as fs from 'fs'; |
| 3 | +import * as os from 'os'; |
2 | 4 |
|
3 | 5 | import { createFilter } from '@rollup/pluginutils'; |
4 | 6 |
|
@@ -40,25 +42,32 @@ export default function typescript(options: RollupTypescriptOptions = {}): Plugi |
40 | 42 | const tsCache = new TSCache(cacheDir); |
41 | 43 | const emittedFiles = new Map<string, string>(); |
42 | 44 | const watchProgramHelper = new WatchProgramHelper(); |
| 45 | + let autoOutDir: string | null = null; |
43 | 46 |
|
44 | 47 | const parsedOptions = parseTypescriptConfig(ts, tsconfig, compilerOptions, noForceEmit); |
45 | 48 |
|
46 | 49 | // When processing JS via allowJs, redirect emit output away from source files |
47 | 50 | // to avoid TS5055 (cannot write file because it would overwrite input file). |
48 | 51 | // We only set a temp outDir if the user did not configure one. |
49 | 52 | if (parsedOptions.options.allowJs && !parsedOptions.options.outDir) { |
50 | | - parsedOptions.options.outDir = path.join(process.cwd(), '.rpt2_allowjs'); |
| 53 | + // Create a unique temporary outDir to avoid TS5055 when emitting JS |
| 54 | + autoOutDir = fs.mkdtempSync(path.join(os.tmpdir(), 'rollup-plugin-typescript-allowjs-')); |
| 55 | + parsedOptions.options.outDir = autoOutDir; |
51 | 56 | } |
52 | 57 |
|
53 | 58 | // Determine default include pattern. By default we only process TS files. |
54 | 59 | // When the consumer enables `allowJs` in their tsconfig/compiler options, |
55 | 60 | // also include common JS extensions so modern JS syntax in .js files is |
56 | 61 | // downleveled by TypeScript as expected. |
57 | 62 | const defaultInclude = parsedOptions.options.allowJs |
58 | | - ? '{,**/}*.(cts|mts|ts|tsx|js|jsx|mjs|cjs)' |
59 | | - : '{,**/}*.(cts|mts|ts|tsx)'; |
| 63 | + ? '{,**/}*.{cts,mts,ts,tsx,js,jsx,mjs,cjs}' |
| 64 | + : '{,**/}*.{cts,mts,ts,tsx}'; |
60 | 65 |
|
61 | | - const filter = createFilter(include || defaultInclude, exclude, { |
| 66 | + const filterExclude = Array.isArray(exclude) ? [...exclude] : exclude ? [exclude] : []; |
| 67 | + if (autoOutDir) { |
| 68 | + filterExclude.push(normalizePath(path.join(autoOutDir, '**'))); |
| 69 | + } |
| 70 | + const filter = createFilter(include || defaultInclude, filterExclude, { |
62 | 71 | resolve: filterRoot ?? parsedOptions.options.rootDir |
63 | 72 | }); |
64 | 73 | parsedOptions.fileNames = parsedOptions.fileNames.filter(filter); |
@@ -120,6 +129,14 @@ export default function typescript(options: RollupTypescriptOptions = {}): Plugi |
120 | 129 | // eslint-disable-next-line |
121 | 130 | program?.close(); |
122 | 131 | } |
| 132 | + if (autoOutDir) { |
| 133 | + try { |
| 134 | + fs.rmSync(autoOutDir, { recursive: true, force: true }); |
| 135 | + } catch { |
| 136 | + // ignore cleanup failures |
| 137 | + } |
| 138 | + autoOutDir = null; |
| 139 | + } |
123 | 140 | }, |
124 | 141 |
|
125 | 142 | renderStart(outputOptions) { |
|
0 commit comments