Skip to content

Variable: dtsComplex()

ts
const dtsComplex: (options?: Options) => Plugin<any> | Plugin<any>[];

Advanced Vite plugin that generates TypeScript declaration files for library builds with intelligent vendor dependency bundling and path alias resolution.

Key Differences from @nhtio/vite-plugins/dts

While the standard dts plugin runs the entire pipeline through Microsoft API Extractor, dtsComplex uses a hybrid approach:

  1. Native TypeScript Compilation: Your source files are compiled to .d.ts using TypeScript's native compiler, preserving original structure and comments.

  2. Selective API Extractor Usage: API Extractor is only used to bundle external vendor libraries into single declaration files, not for your entire codebase.

  3. Separate Vendor Bundles: External dependencies are bundled into separate files (e.g., bundled_vendor_name.d.ts) and referenced from your main declarations, rather than being inlined.

  4. Path Alias Resolution: All import/export paths are resolved to relative paths, eliminating the need for consumers to configure paths in their tsconfig.json.

Pre-Rolled vs Bundled Dependencies

  • Bundled Dependencies: Libraries that need to be rolled up using API Extractor because their declaration files are spread across multiple files.

  • Pre-Rolled Dependencies: Libraries that already provide a single rolled-up declaration file (or single declaration per subpath). These are extracted as-is without further processing.

Parameters

ParameterType
options?Options

Returns

Plugin<any> | Plugin<any>[]

Examples

Basic usage with bundled dependencies:

typescript
import { defineConfig } from "vite";
import { dtsComplex } from "@nhtio/vite-plugins/dts_complex";

export default defineConfig({
  build: {
    lib: {
      entry: "./src/index.ts",
      name: "MyLibrary",
    },
  },
  plugins: [
    dtsComplex({
      bundledDependencies: ["lodash-es", "date-fns"],
    }),
  ],
});

Advanced usage with pre-rolled dependencies:

typescript
import { defineConfig } from "vite";
import { dtsComplex } from "@nhtio/vite-plugins/dts_complex";

export default defineConfig({
  build: {
    lib: {
      entry: {
        index: "./src/index.ts",
        utils: "./src/utils/index.ts",
      },
      name: "MyLibrary",
    },
  },
  plugins: [
    dtsComplex({
      // Dependencies that need rollup
      bundledDependencies: ["lodash-es"],
      // Dependencies with pre-existing single declaration files
      preRolledDependencies: ["@nhtio/encoder"],
      // Custom paths
      tsConfigJsonPath: "./tsconfig.build.json",
      packageJsonPath: "./package.json",
    }),
  ],
});

With regex patterns for scoped packages:

typescript
dtsComplex({
  bundledDependencies: [
    /^@mycompany//,  // All @mycompany/* packages
    'specific-lib'
  ],
  preRolledDependencies: [
    /^@types//  // All @types/* packages (usually pre-rolled)
  ]
})