Skip to content

feat(typescript): write declaration files in configured directory for output.file - #1378

Merged
shellscape merged 5 commits into
rollup:masterfrom
susnux:fix/typescript-decldir
Apr 4, 2023
Merged

shellscape merged 5 commits into
rollup:masterfrom
susnux:fix/typescript-decldir

Conversation

@susnux

@susnux susnux commented Dec 15, 2022 •

Copy link
Copy Markdown
Contributor

Rollup Plugin Name: typescript

This PR contains:

  • bugfix
  • feature
  • refactor
  • documentation
  • other

Are tests included?

  • yes (bugfixes and features will not be merged without tests)
  • no

Breaking Changes?

  • yes (breaking changes will not be merged unless absolutely necessary)
  • no

If yes, then include "BREAKING CHANGES:" in the first commit message body, followed by a description of what is breaking.

List any relevant issue numbers:

Description

If you configure rollup with output.file = 'dist/main.js' and typscript with 'declarationDir: "dist"
Instead of writing the declarations into dist/ they are written into dist/dist, and the workaround from the README does not work anymore, if you set 'declarationDir: "."` no declarations are written at all.

So this is fixes by only enforcing declarations inside output.dir but not inside dirname(output.file) (see added test case).

Additionally:

  • I had to add the types package for resolve to get the package build.
  • I added a new test helper that returns the output file names as rollup would write the files (emulating handleGenerateWrite and writeOutputFile)

@susnux
susnux requested a review from shellscape as a code owner December 15, 2022 14:30
@shellscape shellscape changed the title @rollup/plugin-typescript: Write declaration files in configured directory for output.file feat(typescript): write declaration files in configured directory for output.file Dec 17, 2022
@shellscape

Copy link
Copy Markdown
Collaborator

Please see the Validate Monorepo workflow result. In a monorepo you cannot add a dependency manually without also running pnpm install at the root.

@susnux

susnux commented Dec 19, 2022

Copy link
Copy Markdown
Contributor Author

Please see the Validate Monorepo workflow result. In a monorepo you cannot add a dependency manually without also running pnpm install at the root.

Oh thank you! Should be fixed now :)

Comment thread packages/typescript/src/index.ts Resolved
Comment thread packages/typescript/test/test.js Resolved
test.serial('supports emitting declarations in correct directory for output.file', async (t) => {
// Ensure even when no `output.dir` is configured, declarations are emitted to configured `declarationDir`
process.chdir('fixtures/basic');
const outputOpts = { format: 'es', file: 'dist/main.esm.js' };

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'd like to also see a test where the output file doesn't match the declaration dir

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

What do you mean? Something like this?:

output.file = 'dist/main.js'
declarationDir = 'types'

This will always result in dist/types/main.d.ts, because rollup does not allow files outside the output.dir or the base directory of the output.file, see this rollups source.

Comment thread packages/typescript/test/test.js Resolved
Comment thread util/test.js Resolved
@shellscape

Copy link
Copy Markdown
Collaborator

Please note that we'll close this as stale if the PR hasn't received any attention from the author in 60 days.

@susnux

susnux commented Jan 21, 2023

Copy link
Copy Markdown
Contributor Author

My problem is: I do not have a Windows machine to test this PR so I will have to blind guess how to fix the windows node tests

@shellscape

Copy link
Copy Markdown
Collaborator

I understand. This looks like path differences between windows and nix. I'll see if we can't get you some help for this.

vighnesh153 referenced this pull request in vighnesh153/vighnesh153-monorepo Feb 12, 2023
@halfmatthalfcat

halfmatthalfcat commented Mar 3, 2023 •

Copy link
Copy Markdown

So I took these changes for a test drive in my Vite library project and was still having problems. What I also changed to fix things was to replace baseDir with outputDir on L181. My base dir was resolving to . which was causing the eventual fileName to still include the dist in my output path resulting in the nested dist/dist/... we've been experiencing.

My tsconfig for reference:

{
  "compilerOptions": {
    "target": "es2020",
    "module": "esnext",
    "moduleResolution": "node",
    "esModuleInterop": true,
    "removeComments": true,
    "declaration": true,
    "emitDeclarationOnly": true,
    "baseDir": ".",
    "outDir": "dist",
    "declarationDir": "dist",
    "jsx": "react",
    "strict": true,
    "sourceMap": true,
    "declarationMap": true,
    "paths": {
      "*": ["./src/*"],
      "components/*": ["./src/components/*"]
    }
  },
  "include": [
    "src/**/*"
  ],
  "exclude": [
    "dist"
  ]
}

and Vite config:

export default defineConfig({
  build: {
    lib: {
      entry: resolve(__dirname, 'src/index.ts'),
      name: '[redacted]'
    },
    rollupOptions: {
      plugins: [
        typescript({
          tsconfig: "./tsconfig.json",
        }),
      ],
    },
  },
});

Signed-off-by: Ferdinand Thiessen <rpm@fthiessen.de>
@susnux
susnux requested review from NotWoods and removed request for lukastaegert and shellscape March 9, 2023 23:52
@susnux

susnux commented Mar 9, 2023 •

Copy link
Copy Markdown
Contributor Author

@shellscape @NotWoods I think I fixed the issues, can you trigger the CI?

BTW: The last commit I pushed is for more readability (at least I think it is more self explaining code), if accepted I will squash it.

susnux added 4 commits March 20, 2023 11:56
This function returns the file names and content as
rollup would write to filesystem.
Emulating `handleGenerateWrite` and `writeOutputFile`.

Signed-off-by: Ferdinand Thiessen <rpm@fthiessen.de>
This fixes writing declarations into the configured `declarationDir`
when configured rollup build with `output.file` instead of `output.dir`.

Signed-off-by: Ferdinand Thiessen <rpm@fthiessen.de>
… workaround

Signed-off-by: Ferdinand Thiessen <rpm@fthiessen.de>
Signed-off-by: Ferdinand Thiessen <rpm@fthiessen.de>
@shellscape

Copy link
Copy Markdown
Collaborator

@halfmatthalfcat if you have a small reproduction repo (or repl.it or stackblitz) for @susnux to take a look at, that would be helpful

@susnux

susnux commented Mar 20, 2023

Copy link
Copy Markdown
Contributor Author

Yes that would be really helpful, but I might also have fixed that issue as a rewrote the code while fixing the issues.

@shellscape

Copy link
Copy Markdown
Collaborator

I think we're good to go. Thanks for working through this with the team.

@shellscape
shellscape merged commit 96b0338 into rollup:master Apr 4, 2023
@susnux
susnux deleted the fix/typescript-decldir branch April 4, 2023 21:43
@rm116

rm116 commented May 5, 2023

Copy link
Copy Markdown

Hello @susnux ,

If I have this rollup config :

export default [
  {
    input: "src/index.ts",
    output: [
      {
        file: packageJson.main,
        format: "cjs",
        sourcemap: true,
      },
      {
        file: packageJson.module,
        format: "esm",
        sourcemap: true,
      },
    ],
    plugins: [
      peerDepsExternal(),
      resolve({
        browser: true
      }),
      commonjs(),
      typescript({ tsconfig: "./tsconfig.json" }),
      json(),
      postcss(),
      terser(),
    ],
  },
  {
    input: "lib/esm/types/index.d.ts",
    output: [{ file: "lib/index.d.ts", format: "esm" }],
    plugins: [dts()],
    external: ['react', 'react-dom', /\.css$/]
  },
];

And this package.json :

{
  "name": "test",
  "version": "0.0.1",
  "description": "",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "rollup": "rollup -c"
  },
  "main": "lib/cjs/index.js",
  "module": "lib/esm/index.js",
  "files": [
    "lib"
  ],
  "types": "lib/index.d.ts",
  "devDependencies": {
    "@rollup/plugin-commonjs": "^22.0.1",
    "@rollup/plugin-json": "^4.1.0",
    "@rollup/plugin-node-resolve": "^13.3.0",
    "@rollup/plugin-typescript": "^8.3.3",
    "@types/react": "^18.0.15",
    "postcss": "^8.4.14",
    "rollup": "^2.77.0",
    "rollup-plugin-dts": "^4.2.2",
    "rollup-plugin-ignore": "^1.0.10",
    "rollup-plugin-peer-deps-external": "^2.2.4",
    "rollup-plugin-postcss": "^4.0.2",
    "rollup-plugin-terser": "^7.0.2",
    "tslib": "^2.4.0",
    "typescript": "^4.7.4"
  },
  "dependencies": {
    "axios": "^0.27.2"
  },
  "peerDependencies": {
    "react": "^17.0.2",
    "react-dom": "^17.0.2"
  }
}

And this tsconfig.json :

{
  "compilerOptions": {
    "target": "es5", 
    "esModuleInterop": true, 
    "forceConsistentCasingInFileNames": true,
    "strict": true, 
    "skipLibCheck": true,
    "baseUrl": "./src",
    "paths": {
      "@services/*": ["services/*"],
      "@root/*": ["./*"]
    },
    "jsx": "react", 
    "module": "ESNext",  
    "declaration": true,
    "declarationDir": "types",
    "sourceMap": true,
    "outDir": "lib",
    "moduleResolution": "node",
    "allowSyntheticDefaultImports": true,
    "emitDeclarationOnly": true,
  }
}

The rollup command is failing because it didn't find the file : "lib/esm/types/index.d.ts"

image

@rm116

rm116 commented May 5, 2023

Copy link
Copy Markdown

cc @NotWoods

@susnux

susnux commented May 5, 2023

Copy link
Copy Markdown
Contributor Author

@rm116 You should upgrade @rollup/plugin-typescrip as you still use version 8 and this PR is only included in version 11.1.0+.

Using the latest version everything from your example works for me.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants