plugins icon indicating copy to clipboard operation
plugins copied to clipboard

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

Open jdharrisnz opened this issue 1 year ago • 5 comments

  • Rollup Plugin Name: @rollup/plugin-typescript
  • Rollup Plugin Version: 11.1.6
  • Rollup Version: 4.9.4
  • Operating System (or Browser): macOS
  • Node Version: 21.5.0
  • Link to reproduction: https://replit.com/@jdharrisnz/rollup-plugin-repro-typescript-import

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:

  • "type": "module" in package.json
  • "moduleResolution": "NodeNext" or "Node16" in tsconfig.json
  • A rollup config file written in ESM format and TypeScript

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

jdharrisnz avatar Jan 10 '24 21:01 jdharrisnz

Same issue encountered.

07akioni avatar Mar 25 '24 02:03 07akioni

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.

thisisanto avatar Apr 12 '24 05:04 thisisanto

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()]
};

Arnesfield avatar Apr 14 '24 07:04 Arnesfield

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.

EDIT: This also affects @rollup/plugin-json, and likely any other plugin in this collection that uses default exports.

Silic0nS0ldier avatar Jun 25 '24 12:06 Silic0nS0ldier