rollup / plugins

🍣 The one-stop shop for official Rollup plugins
MIT License
3.57k stars 568 forks source link

[TypeScript] Module resolution doesn't work in rollup.config.ts when using modern project settings #1662

Open jdharrisnz opened 5 months ago

jdharrisnz commented 5 months ago

Expected Behavior

Compile as usual. Use npm run bug in my repl to see what happens.

Actual Behavior

Produces errors when compiling:

npx rollup --config rollup.config.ts --configPlugin typescript
loaded rollup.config.ts with warnings
(!) Plugin typescript: @rollup/plugin-typescript TS2349: This expression is not callable.
  Type 'typeof import("/home/runner/rollup-plugin-repro-typescript-import/node_modules/@rollup/plugin-typescript/types/index")' has no call signatures.

Additional Information

Key ingredients are:

The workaround is to use a JavaScript config file, so this is really the smallest of inconveniences, but it's something to fix nonetheless.

07akioni commented 3 months ago

Same issue encountered.

thisisanto commented 2 months ago

Alternatively

// @ts-expect-error see https://github.com/rollup/plugins/issues/1662
commonjs(),

Which again isn't nice, but allows you to keep using a TS config file.

Arnesfield commented 2 months ago

Might be off-topic, but here is another workaround to keep the typings:

// rollup.config.ts
import _eslint from '@rollup/plugin-eslint';
import _typescript from '@rollup/plugin-typescript';

// NOTE: remove once import errors are fixed for their respective packages
const eslint = _eslint as unknown as typeof _eslint.default;
const typescript = _typescript as unknown as typeof _typescript.default;

// ...
export default {
  // ...
  plugins: [eslint(), typescript()]
};
Silic0nS0ldier commented 1 week ago

This issue sounds like https://github.com/microsoft/TypeScript/issues/58890

The cause appears to be related to how TypeScript classifies imports. https://github.com/microsoft/TypeScript/issues/58890#issuecomment-2177318634 concluded that if the .d.ts file is in a CJS scope, the import will be treated CJS.

In the case of @rollup/plugin-commonjs the types live at types/index.d.ts which is outside the ESM scope (dist/es/).

This issue is fixable, but it will mean duplicating .d.ts in the published package.