Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat(typescript): error when no tsconfig and no rootDir #794

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/typescript/src/preflight.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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 not shown.
50 changes: 50 additions & 0 deletions packages/typescript/test/tsconfig.ts
Original file line number Diff line number Diff line change
@@ -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);
});