banner
meanc

meanc

twitter
github
discord server

How to package a TypeScript package to generate .d.ts files

  1. Notify ts
{
    "compilerOptions": {
      "declaration": true, // Generate .d.ts file
      "declarationDir": "dist/types", // Output directory
      "outDir": "dist", // Output directory
    }
}

  1. Adjust vite.config.ts
// Library mode packaging src/main.ts
import { defineConfig } from 'vite'
import path from 'path'
import dts from 'vite-plugin-dts';

export default defineConfig({
  plugins: [
    dts({
      outDir: 'dist/types',  // Declaration file output directory
      include: ['src/**/*.ts'], // Included files
    }),
  ],
  build: {
    lib: {
      entry: path.resolve(__dirname, 'src/main.ts'),
      name: 'main',
      fileName: (format) => `main.${format}.js`,
    },
  },
})

  1. Add package.json can be read when used
{
    "main": "dist/main.umd.js",
    "module": "dist/main.es.js",
    "types": "dist/types/main.d.ts",
}
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.