Skip to content
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

fix(typescript): fix declaration file generation for single file outputs #1201

Merged
merged 1 commit into from Jul 28, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/typescript/README.md
Expand Up @@ -304,7 +304,7 @@ export default {
output: {
file: 'dist/index.mjs'
},
plugins: [typescript({ tsconfig: './tsconfig.json' })]
plugins: [typescript()]
};
```

Expand All @@ -320,7 +320,7 @@ And accompanying `tsconfig.json` file:
}
```

This setup will produce `dist/index.mjs` and `dist/dist/index.d.ts`. To correctly place the declaration file, add an `exclude` setting in `tsconfig` and modify the `declarationDir` setting in `compilerOptions` to resemble:
This setup will produce `dist/index.mjs` and `dist/src/index.d.ts`. To correctly place the declaration file, add an `exclude` setting in `tsconfig` and modify the `declarationDir` setting in `compilerOptions` to resemble:

```json
{
Expand Down
6 changes: 5 additions & 1 deletion packages/typescript/src/index.ts
Expand Up @@ -137,7 +137,11 @@ export default function typescript(options: RollupTypescriptOptions = {}): Plugi
const output = findTypescriptOutput(ts, parsedOptions, fileName, emittedFiles, tsCache);
output.declarations.forEach((id) => {
const code = getEmittedFile(id, emittedFiles, tsCache);
let baseDir = outputOptions.dir;
let baseDir =
outputOptions.dir ||
(parsedOptions.options.declaration
? parsedOptions.options.declarationDir || parsedOptions.options.outDir
: null);
if (!baseDir && tsconfig) {
baseDir = tsconfig.substring(0, tsconfig.lastIndexOf('/'));
}
Expand Down
24 changes: 24 additions & 0 deletions packages/typescript/test/test.js
Expand Up @@ -122,6 +122,30 @@ test.serial('ensures multiple outputs can be built', async (t) => {
]);
});

test.serial('supports emitting types also for single file output', async (t) => {
// Navigate to folder and use default local tsconfig instead of specifying tsconfig via file path
// as that would have the side effect that the tsconfig's path would be used as fallback path for
// the here unspecified outputOptions.dir, in which case the original issue wouldn't show.
process.chdir('fixtures/basic');

const warnings = [];
const bundle = await rollup({
input: 'main.ts',
plugins: [typescript({ declaration: true, declarationDir: 'dist' })],
onwarn(warning) {
warnings.push(warning);
}
});
// generate a single output bundle, in which case, declaration files were not correctly emitted
const output = await getCode(bundle, { format: 'esm', file: 'dist/main.js' }, true);

t.deepEqual(
output.map((out) => out.fileName),
['main.js', 'main.d.ts']
);
t.is(warnings.length, 0);
});

test.serial('relative paths in tsconfig.json are resolved relative to the file', async (t) => {
const bundle = await rollup({
input: 'fixtures/relative-dir/main.ts',
Expand Down