Skip to content

Commit

Permalink
feat(typescript): error when no tsconfig and no rootDir (#794)
Browse files Browse the repository at this point in the history
* feat(typescript): error when no tsconfig and no rootDir

* fix: only emit when declaration: true
  • Loading branch information
shellscape committed Feb 6, 2021
1 parent 031b566 commit cbfd779
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/typescript/src/preflight.ts
Expand Up @@ -17,6 +17,8 @@ ${pluginName}: Rollup requires that TypeScript produces ES Modules. Unfortunatel
"module" other than "esnext". Unless you know what you're doing, please change "module" to "esnext"
in the target tsconfig.json file or plugin options.`.replace(/\n/g, '');

const rootDirErrorMessage = `${pluginName}: The "rootDir" or "rootDirs" option is required when the "tsconfig" option is not specified and "declaration" is "true".`;

const tsLibErrorMessage = `${pluginName}: Could not find module 'tslib', which is required by this plugin. Is it installed?`;

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

const { options } = config;
if (options.declaration && !options.configFilePath && !options.rootDir && !options.rootDirs) {
context.error(rootDirErrorMessage);
}

if (!rollupOptions.preserveModules && tslib === null) {
context.error(tsLibErrorMessage);
}
Expand Down
25 changes: 25 additions & 0 deletions packages/typescript/test/snapshots/tsconfig.ts.md
@@ -0,0 +1,25 @@
# Snapshot report for `test/tsconfig.ts`

The actual snapshot is saved in `tsconfig.ts.snap`.

Generated by [AVA](https://avajs.dev).

## inline config without tsconfig + rootDir

> Snapshot 1
[
'main.js',
'types/main.d.ts',
]

## inline config without tsconfig without rootDir fails

> Snapshot 1
Error {
code: 'PLUGIN_ERROR',
hook: 'buildStart',
plugin: 'typescript',
message: '@rollup/plugin-typescript: The "rootDir" or "rootDirs" option is required when the "tsconfig" option is not specified and "declaration" is "true".',
}
Binary file added packages/typescript/test/snapshots/tsconfig.ts.snap
Binary file not shown.
50 changes: 50 additions & 0 deletions packages/typescript/test/tsconfig.ts
@@ -0,0 +1,50 @@
import test from 'ava';
import { rollup } from 'rollup';

import typescript from '..';

import { getCode, onwarn } from '../../../util/test';

test.beforeEach(() => process.chdir(__dirname));

test.serial('inline config without tsconfig + rootDir', async (t) => {
const bundle = await rollup({
input: 'fixtures/basic/main.ts',
plugins: [
typescript({
declaration: true,
declarationDir: 'fixtures/basic/dist/types',
exclude: 'fixtures/basic/dist/types',
include: 'fixtures/basic/*.ts',
tsconfig: false,
rootDir: 'fixtures/basic'
})
],
onwarn
});
const files = await getCode(bundle, { format: 'esm', dir: 'fixtures/basic/dist' }, true);
const [, { source }] = files;

t.snapshot(files.map(({ fileName }) => fileName));
t.true((source as string)?.includes('declare const answer = 42;'));
});

test.serial('inline config without tsconfig without rootDir fails', async (t) => {
const fail = () =>
rollup({
input: 'fixtures/basic/main.ts',
plugins: [
typescript({
declaration: true,
declarationDir: 'fixtures/basic/dist/types',
exclude: 'fixtures/basic/dist/types',
include: 'fixtures/basic/*.ts',
tsconfig: false
})
],
onwarn
});

const error = await t.throwsAsync(fail);
t.snapshot(error);
});

0 comments on commit cbfd779

Please sign in to comment.