Skip to content

Commit

Permalink
fix(typescript): fix declaration file generation for single file outp…
Browse files Browse the repository at this point in the history
…uts (#1201)

Previously, type declaration files were only emitted if outputOptions.dir was
used and not outputOptions.file or, as a side effect, if the tsconfig file was
specified via a qualified path. This commit fixes that, such that types are
also emitted for single file builds.
  • Loading branch information
danimoh authored Jul 28, 2022
1 parent 985cf4c commit 5f78173
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/typescript/README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

3 comments on commit 5f78173

@pdme
Copy link

@pdme pdme commented on 5f78173 Aug 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this broke our build! it might break others as well.

we had indeed typescript({ tsconfig: './tsconfig.json' }), and

{
    input: 'dist/esm/types/index.d.ts',
    output: [{ file: 'dist/index.d.ts', format: 'esm' }],
    plugins: [dts()],
    external: [/\.css$/],
  }

this was the error we got: [!] Error: Could not resolve entry module (dist/esm/types/index.d.ts).

@shellscape
Copy link
Collaborator

@shellscape shellscape commented on 5f78173 Aug 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a helpful comment. If there is a bug, please open a new bug issue and provide a reproduction given the instructions in the issue template.

@robertopuentesdiaz
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend use 8.3.3

Please sign in to comment.