egoist / tsup

The simplest and fastest way to bundle your TypeScript libraries.
https://tsup.egoist.dev
MIT License
8.99k stars 217 forks source link

How to generate .d.ts.map #564

Closed eric-burel closed 2 years ago

eric-burel commented 2 years ago

Hi,

I am able to generate .d.ts, however in a monorepo it's also a requirement to produce .d.ts.map files to allow jumping between files (see https://stackoverflow.com/questions/55318663/how-to-generate-d-ts-and-d-ts-map-files-using-webpack).

My tsconfig enables declarationMap, however it doesn't seem to be respected by tsup. Do you have an idea of how to force this parameter to be taken into account? I guess you use tsc under the hood so that's just a matter of passing the right config.

My tsup.config.ts:

import { defineConfig } from "tsup";

const commonConfig = {
  clean: true,
  splitting: false,
  dts: true,
  sourcemap: true,
};
export default defineConfig([
  {
    entry: ["index.ts"],
    ...commonConfig,
    format: ["cjs", "esm", "iife"],
    outDir: "dist",
  },

Adding: tsconfig: path.resolve(__dirname, "./tsconfig.build.json"), (with the config enabling declarationMap) doesn't seem to work.

My tsconfig.json:

{
  "compilerOptions": {
    "allowJs": false,
    "alwaysStrict": true,
    "declaration": true,
    "declarationMap": true, // Allow navigation to definition in VS Code https://stackoverflow.com/questions/54459847/vscode-lerna-typescript-monorepo-code-navigation
    "emitDecoratorMetadata": true,
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "forceConsistentCasingInFileNames": true,
    "isolatedModules": true,
    "jsx": "react",
    "lib": [
      "es6",
      "dom"
    ],
    "module": "commonjs",
    "moduleResolution": "node",
    "noFallthroughCasesInSwitch": true,
    "noImplicitAny": false,
    "noLib": false,
    "noUnusedLocals": false, // won't fail on unused variables
    "noUnusedParameters": false,
    "removeComments": true,
    "resolveJsonModule": true,
    "skipLibCheck": true,
    "strictNullChecks": true,
    "strictBindCallApply": true, // @see https://github.com/storybookjs/storybook/issues/11916
    "sourceMap": true,
    "strict": false,
    "target": "es6",
    "typeRoots": [
      "./types",
      "./node_modules/@types"
    ]
  },
  "exclude": [
    "node_modules",
    "**/*.d.ts",
    "**/dist/*",
    "**/webpack.config.js",
    // @see https://github.com/facebook/docusaurus/issues/3424
    "packages/docusaurus"
  ]
}
sachinraja commented 2 years ago

tsup uses rollup-plugin-dts to bundle the dts files, which doesn't support generating declaration maps. You could run tsc onSuccess.

eric-burel commented 2 years ago

Thanks for the tip with onSuccess. I've eventually disabled dts generation from Tsup and instead run tsc afterward: tsc --emitDeclarationOnly --declaration. I'll close the issue since it's a minor use case, but at least Googlers can find it if needed.