Swatinem / rollup-plugin-dts

A rollup plugin to generate .d.ts rollup files for your typescript project
GNU Lesser General Public License v3.0
816 stars 71 forks source link

Support resolving subpath pattern imports #273

Closed RebeccaStevens closed 1 year ago

RebeccaStevens commented 1 year ago

Checklist

Code Snipped

// package.json
{
  "imports": {
    "#foo": "./src/foo.ts"
  },
}
// some-file.ts
import { type Foo } from "#foo";

Issue

#foo doesn't get resolved.

Additional

When using this plugin in combination with [rollup-plugin-typescript](), the following error occurs:

[!] RollupError: "Foo" is not exported by "src/foo.ts", imported by "some-file.ts".
https://rollupjs.org/troubleshooting/#error-name-is-not-exported-by-module
some-file.ts
    [[Stack Trace]]

Workaround

Use compilerOptions.paths:

// rollup.config.mjs
export default {
  plugins: [
    dts({
      compilerOptions: {
        paths: {
          "#foo": ["./src/foo.ts"],
        },
      },
    }),
  ]
}

Or without two sources of truth:

// rollup.config.mjs
import pkg from "./package.json" assert { type: "json" };

export default {
  plugins: [
    dts({
      compilerOptions: {
        paths: Object.fromEntries(
          Object.entries(pkg.imports).map(([id, path]) => [id, [path]]),
        ),
      },
    }),
  ]
}

Additional Request

It'd be nice if there was some way to have submodules import from one another in the distribution files. For example, if #bar imports a type from #foo, that type won't be bundled into #bar. This is similar to respectExternal but for internal externals?? I don't think there is currently a way to get respectExternal to do this.