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

[@rollup/plugin-typescript] tsconfig declarationDir is ignored when using single file output #1230

Closed
CarrotB opened this issue Aug 1, 2022 · 16 comments

Comments

@CarrotB
Copy link

CarrotB commented Aug 1, 2022

Expected Behavior

Declarations generated at the specified location

Actual Behavior

Declarations generated at the same directory as the output file.

Additional Information

I'm using api-extractor to rollup d.ts files into a single one, so the declarations are output at a place that will be removed after
processing.
This works at version 8.3.1 with a problem that it generates file at relative location under output directory, that is when declarationDir is types and output.file is dist/index.js, declarations will be under dist/types. But after my collegue updated to 8.3.4 (by using ^8.3.1 in package.json), the behavior changes to that declarations generated at dist/, and the build progress breaks since api-extractor can't find the desired entry for d.ts rollup.

@dominikbulaj
Copy link

Also confirmed.

In tsconfig.json I have: "declaration": false and in rollup.config.js I got

    typescript({
      compilerOptions: { declaration: true, declarationDir: './types' }
    }),

When rollup runs declaration file(s) are placed in the output directory and not specified ('./types')

@himanshuchawla009

This comment was marked as spam.

@shellscape
Copy link
Collaborator

@himanshuchawla009 please do not post "me too," "+1" type replies. They spam anyone who has notifications on for issues (e.g. all maintainers). Please use the reaction buttons on the original post in the issue instead.

@Alpha0723
Copy link

I got the same issue with 8.3.4. Now I have to create new tsconfig.types.json with declaration config
"compilerOptions": {
"declaration": true,
"declarationDir": "./dist/types",
"emitDeclarationOnly": true,
}
then use tsc command to generate d.ts to specific folder.
It works but it's ugly. Hope it could be fixed asap.

dominikbulaj added a commit to dominikbulaj/react-scroll-into-view that referenced this issue Aug 3, 2022
@Basssiiie
Copy link

I got the issue as well. Reverting back to version 8.3.3 fixes the issue, so it seems it was introduced in the 8.3.4 release. The only change in that release is this; #1201, which may very well be the cause (also there's other people talking about problems there as well).

Easiest temporary fix for now: revert back to 8.3.3 and pin this version in your package.json.

Basssiiie added a commit to Basssiiie/OpenRCT2-FlexUI that referenced this issue Aug 12, 2022
@IronGeek
Copy link

IronGeek commented Sep 1, 2022

I'm also having this problem with the latest version. And I think I found the problem with the recently merged PR #1201.
Please correct me if my analysis is wrong.

let baseDir =
outputOptions.dir ||
(parsedOptions.options.declaration
? parsedOptions.options.declarationDir || parsedOptions.options.outDir
: null);
if (!baseDir && tsconfig) {
baseDir = tsconfig.substring(0, tsconfig.lastIndexOf('/'));
}
if (!code || !baseDir) return;
this.emitFile({
type: 'asset',
fileName: normalizePath(path.relative(baseDir, id)),
source: code
});

Asumming the parsedOptions.options.declaration is true and we're using single file output.
The outputOptions.dir will be undefined, and the baseDir will be set to the parsedOptions.options.declarationDir value or fallback to parsedOptions.options.outDir if its not set (line 141).
And since baseDir is now defined, the code generation will not be skipped and output files will be generated (line 149).
This seems to be a solid logic, however on line 153 the filename for the file being generated is normalized and make relative to the baseDir.

fileName: normalizePath(path.relative(baseDir, id)),

So for example, if user set declarationDir to types, and foo/bar.d.ts is to be generated then:
the expected filename relative to the output will be types/foo/bar.d.ts
but the actual filename instead foo/bar.d.ts this is because:

baseDir = '/some/absolute/path/to/output/types'
id = '/some/absolute/path/to/output/types/foo/bar.d.ts'
filename = relative(baseDir, id) => 'foo/bar.d.ts'

we can see, that whatever value the declarationDir holds will be useless in single file output since the final filename will always be made relative to the declarationDir itself.

@rpearce
Copy link

rpearce commented Oct 11, 2022

This appears to still be an issue as of the latest version, v9.0.0. Locking the version to 8.3.3 seems to be the only way to keep the original behavior working.

@yangliguo7
Copy link

I found that there will be similar problems in the case of multi-file output

rollup.config.js

  output: [
    {
      dir: 'dist/cjs',
      format: 'cjs'
    },
    {
      dir: 'dist/es',
      format: 'es'
    },
  ],

tsconfig.json

 compilerOptions: {
    declaration: true,
    declarationDir: "dist/types"
  },

the error log

@rollup/plugin-typescript: Path of Typescript compiler option 'declarationDir' must be located inside Rollup 'dir' option.

Is this caused by the same problem?

@stale stale bot added the x⁷ ⋅ stale label Jan 22, 2023
@stale
Copy link

stale bot commented Feb 2, 2023

Hey folks. This issue hasn't received any traction for 60 days, so we're going to close this for housekeeping. If this is still an ongoing issue, please do consider contributing a Pull Request to resolve it. Further discussion is always welcome even with the issue closed. If anything actionable is posted in the comments, we'll consider reopening it.

@stale stale bot closed this as completed Feb 2, 2023
@melissarh57
Copy link

Did this ever get fixed? Or still just sticking with old rollup version for now?

@rpearce
Copy link

rpearce commented Feb 18, 2023

I'm sticking with the old one at present. I don't believe I saw anything about this getting fixed...

@yangliguo7
Copy link

Did this ever get fixed? Or still just sticking with old rollup version for now?

I use a a hack way to avoid error . use ts to generate .d.ts . use rollup to build ......

@stepanjakl
Copy link

+1 to get this fixed in the latest Rollup version 🙏

@deansimcox
Copy link

After some debugging, I've noticed that this issue comes up if you have a subfolder for the dir property.

E.g this is the config we had

  output: [
    {
      file: pkg.main,
      format: 'cjs',
      exports: 'named',
      sourcemap: true,
    },
    {
      dir: 'build/esm',
      format: 'esm',
      exports: 'named',
      sourcemap: true,
      preserveModules: true,
      preserveModulesRoot: 'src',
    },
  ],

The error went away when I changed build/esm to just build

@soeasyjx
Copy link

Has this problem been solved yet

@rudevich
Copy link

rudevich commented May 5, 2024

The problem is still here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests