Skip to content

Commit 7e8812d

Browse files
committed
feat(typescript): auto-include JS when allowJs is set; use temp outDir and clean up\n\n- Use brace expansion in default include glob\n- Create per-build temp outDir under os.tmpdir(), exclude it from filter, and remove on build end\n- Document behavior in README
1 parent 41d677e commit 7e8812d

3 files changed

Lines changed: 24 additions & 5 deletions

File tree

packages/typescript/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ Default: `null`
7979

8080
A [picomatch pattern](https://github.com/micromatch/picomatch), or array of patterns, which specifies the files in the build the plugin should operate on. By default all `.ts` and `.tsx` files are targeted. If your `tsconfig.json` (or plugin `compilerOptions`) sets `allowJs: true`, the default include expands to also cover `.js`, `.jsx`, `.mjs`, and `.cjs` files so that JavaScript sources are downleveled by TypeScript as well.
8181

82+
> Note: When `allowJs` is enabled and no `outDir` is configured, this plugin creates a temporary output directory for TypeScript emit to avoid TS5055 (overwriting input files). The directory is excluded from processing and removed after the build.
83+
8284
### `filterRoot`
8385

8486
Type: `String` | `Boolean`<br>

packages/typescript/src/index.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import * as path from 'path';
2+
import * as fs from 'fs';
3+
import * as os from 'os';
24

35
import { createFilter } from '@rollup/pluginutils';
46

@@ -40,25 +42,32 @@ export default function typescript(options: RollupTypescriptOptions = {}): Plugi
4042
const tsCache = new TSCache(cacheDir);
4143
const emittedFiles = new Map<string, string>();
4244
const watchProgramHelper = new WatchProgramHelper();
45+
let autoOutDir: string | null = null;
4346

4447
const parsedOptions = parseTypescriptConfig(ts, tsconfig, compilerOptions, noForceEmit);
4548

4649
// When processing JS via allowJs, redirect emit output away from source files
4750
// to avoid TS5055 (cannot write file because it would overwrite input file).
4851
// We only set a temp outDir if the user did not configure one.
4952
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;
5156
}
5257

5358
// Determine default include pattern. By default we only process TS files.
5459
// When the consumer enables `allowJs` in their tsconfig/compiler options,
5560
// also include common JS extensions so modern JS syntax in .js files is
5661
// downleveled by TypeScript as expected.
5762
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}';
6065

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, {
6271
resolve: filterRoot ?? parsedOptions.options.rootDir
6372
});
6473
parsedOptions.fileNames = parsedOptions.fileNames.filter(filter);
@@ -120,6 +129,14 @@ export default function typescript(options: RollupTypescriptOptions = {}): Plugi
120129
// eslint-disable-next-line
121130
program?.close();
122131
}
132+
if (autoOutDir) {
133+
try {
134+
fs.rmSync(autoOutDir, { recursive: true, force: true });
135+
} catch {
136+
// ignore cleanup failures
137+
}
138+
autoOutDir = null;
139+
}
123140
},
124141

125142
renderStart(outputOptions) {

0 commit comments

Comments
 (0)