Skip to content

Commit cbfd779

Browse files
authored
feat(typescript): error when no tsconfig and no rootDir (#794)
* feat(typescript): error when no tsconfig and no rootDir * fix: only emit when declaration: true
1 parent 031b566 commit cbfd779

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

packages/typescript/src/preflight.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ ${pluginName}: Rollup requires that TypeScript produces ES Modules. Unfortunatel
1717
"module" other than "esnext". Unless you know what you're doing, please change "module" to "esnext"
1818
in the target tsconfig.json file or plugin options.`.replace(/\n/g, '');
1919

20+
const rootDirErrorMessage = `${pluginName}: The "rootDir" or "rootDirs" option is required when the "tsconfig" option is not specified and "declaration" is "true".`;
21+
2022
const tsLibErrorMessage = `${pluginName}: Could not find module 'tslib', which is required by this plugin. Is it installed?`;
2123

2224
let undef;
@@ -28,6 +30,11 @@ export const preflight = ({ config, context, rollupOptions, tslib }: PreflightOp
2830
context.warn(moduleErrorMessage);
2931
}
3032

33+
const { options } = config;
34+
if (options.declaration && !options.configFilePath && !options.rootDir && !options.rootDirs) {
35+
context.error(rootDirErrorMessage);
36+
}
37+
3138
if (!rollupOptions.preserveModules && tslib === null) {
3239
context.error(tsLibErrorMessage);
3340
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Snapshot report for `test/tsconfig.ts`
2+
3+
The actual snapshot is saved in `tsconfig.ts.snap`.
4+
5+
Generated by [AVA](https://avajs.dev).
6+
7+
## inline config without tsconfig + rootDir
8+
9+
> Snapshot 1
10+
11+
[
12+
'main.js',
13+
'types/main.d.ts',
14+
]
15+
16+
## inline config without tsconfig without rootDir fails
17+
18+
> Snapshot 1
19+
20+
Error {
21+
code: 'PLUGIN_ERROR',
22+
hook: 'buildStart',
23+
plugin: 'typescript',
24+
message: '@rollup/plugin-typescript: The "rootDir" or "rootDirs" option is required when the "tsconfig" option is not specified and "declaration" is "true".',
25+
}
409 Bytes
Binary file not shown.

packages/typescript/test/tsconfig.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import test from 'ava';
2+
import { rollup } from 'rollup';
3+
4+
import typescript from '..';
5+
6+
import { getCode, onwarn } from '../../../util/test';
7+
8+
test.beforeEach(() => process.chdir(__dirname));
9+
10+
test.serial('inline config without tsconfig + rootDir', async (t) => {
11+
const bundle = await rollup({
12+
input: 'fixtures/basic/main.ts',
13+
plugins: [
14+
typescript({
15+
declaration: true,
16+
declarationDir: 'fixtures/basic/dist/types',
17+
exclude: 'fixtures/basic/dist/types',
18+
include: 'fixtures/basic/*.ts',
19+
tsconfig: false,
20+
rootDir: 'fixtures/basic'
21+
})
22+
],
23+
onwarn
24+
});
25+
const files = await getCode(bundle, { format: 'esm', dir: 'fixtures/basic/dist' }, true);
26+
const [, { source }] = files;
27+
28+
t.snapshot(files.map(({ fileName }) => fileName));
29+
t.true((source as string)?.includes('declare const answer = 42;'));
30+
});
31+
32+
test.serial('inline config without tsconfig without rootDir fails', async (t) => {
33+
const fail = () =>
34+
rollup({
35+
input: 'fixtures/basic/main.ts',
36+
plugins: [
37+
typescript({
38+
declaration: true,
39+
declarationDir: 'fixtures/basic/dist/types',
40+
exclude: 'fixtures/basic/dist/types',
41+
include: 'fixtures/basic/*.ts',
42+
tsconfig: false
43+
})
44+
],
45+
onwarn
46+
});
47+
48+
const error = await t.throwsAsync(fail);
49+
t.snapshot(error);
50+
});

0 commit comments

Comments
 (0)