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

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

merged 1 commit into from Jul 28, 2022

Conversation

danimoh
Copy link
Contributor

@danimoh danimoh commented Jun 9, 2022

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

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.

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.
@danimoh danimoh requested a review from shellscape as a code owner June 9, 2022 17:19
Copy link
Collaborator

@shellscape shellscape left a comment

Choose a reason for hiding this comment

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

Thanks for the PR. This looks good to me, nothing stands out of concern. Test looks good.

@shellscape shellscape merged commit 5f78173 into rollup:master Jul 28, 2022
@danimoh danimoh deleted the plugin-typescript/declarations-for-single-file-output branch August 1, 2022 16:46
@trm217
Copy link

trm217 commented Aug 2, 2022

@shellscape FYI These changes seem to cause certain rollup-configs to break.
After the release of @rollup/plugin-typescript@8.3.4 the rollup-config below started failing.
The second compile task was no longer able to receive dist/types/index.d.ts which previous to v8.3.4 was emiited by the typescript-plugin in the first task.
From looking at the changes, I am unsure why this is happening, as declarationDir is explicitly defined in the tsconfig.

References

rollup.js config

import nodeResolve from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import peerDepsExternal from 'rollup-plugin-peer-deps-external'
import dts from 'rollup-plugin-dts'
import typescript from '@rollup/plugin-typescript'
import json from '@rollup/plugin-json'
import { terser } from 'rollup-plugin-terser'
import visualizer from 'rollup-plugin-visualizer'
import bundleSize from 'rollup-plugin-bundle-size'
import path from 'path'

const pkgJSON = require('./package.json')

export default [
  {
    input: 'src/index.ts',
    output: [
      {
        file: pkgJSON.module,
        format: 'esm',
      },
      {
        file: pkgJSON.main,
        format: 'cjs',
      },
    ],
    plugins: [
      peerDepsExternal(),
      nodeResolve(),
      json(),
      commonjs(),
      typescript({
        tsconfig: './tsconfig.json',
        outputToFilesystem: true,
      }),
      terser(),
      bundleSize(),
      process.env.ANALYZE === 'true' &&
        visualizer(() => ({
          filename: path.join(__dirname, 'bundle-analysis.html'),
        })),
    ],
    external: Object.keys(pkgJSON.peerDependencies || {}),
  },
  {
    input: 'dist/types/index.d.ts',
    output: [{ file: 'dist/index.d.ts', format: 'esm' }],
    plugins: [nodeResolve({ preferBuiltins: true }), dts()],
  },
]

tsconfig.json

{
  "compilerOptions": {
    "target": "ES5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "skipLibCheck": true,
    "strict": false,
    "forceConsistentCasingInFileNames": true,
    "esModuleInterop": true,
    "module": "ESNext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "react-jsx",
    "incremental": true,
    "declaration": true,
    "declarationDir": "types",
    "rootDir": "src"
  },
  "include": [
    "src/**/*.ts",
    "src/**/*.tsx"
  ],
  "exclude": [
    "node_modules",
    "dist"
  ]
}

Additionally here is the package in which the issue occurred.

@vgpena
Copy link

vgpena commented Aug 10, 2022

I've been experiencing the same issue the same issue as @trm217. Our types have always been output into dist/types but with upgrading from 8.3.3 -> 8.3.4, now the types appear right in dist/. We edited our TS and Rollup configs a bit and were not able to find a clear cause, so we're pinning to v8.3.3 for now. Guidance is appreciated! 🙌🏻

Relevant config

Rollup.config.js (partial):

export default {
  input: 'src/index.ts',
  output: [
    {
      file: pkg.main,
      format: 'umd',
      sourcemap: true,
      name: 'index',
      globals,
    },
    {
      file: pkg.module,
      format: 'esm',
      sourcemap: true,
      name: 'index',
      globals,
    },
  ],
  plugins: [
    externals({
      deps: false,
    }),
    resolve({
      extensions: ['.js', '.jsx', '.ts', '.tsx'],
    }),
    commonjs({
      requireReturnsDefault: 'auto',
    }),
    typescript({
      tsconfig: './tsconfig.json',
      outputToFilesystem: true,
    }),
    json(),
  ],
}

tsconfig.json (complete):

{
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "declaration": true,
    "declarationMap": false,
    "isolatedModules": true,
    "noFallthroughCasesInSwitch": true,
    "incremental": true,
    "baseUrl": "./src",
    "jsx": "react-jsx",
    "noEmit": false,
    "outDir": "dist",
    "declarationDir": "types",
  },
  "include": ["./src", "globals.d.ts"],
  "exclude": ["./**/*.js", "./**/*.jsx"]
}

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.

None yet

4 participants