Open lazarljubenovic opened 1 year ago
We've never really had an issue with imports of plugins in TS. If you feel there's something actionable here, please feel free to open a PR that's backwards compatible with tests around your changes.
Managed to reproduce in StackBlitz: https://stackblitz.com/edit/rollup-repro-cf618b?file=src%2Fmain.ts
Run npx tsc
in the terminal and you'll see the compilation error. Not sure why the StackBlitz editor itself doesn't catch the error. My WebStorm shows it properly when I reproduce locally.
I'll try spinning up a PR later, I just need to find time to figure out the build process. It's a trivial fix apart from getting to know the project (I see some "pnpm" shenanigans from the package.json).
Are The Types Wrong will tell you when and why the types are wrong, if it helps.
Here is a small workaround in mjs:
import { defineConfig } from 'rollup'
import commonjs from '@rollup/plugin-commonjs'
import json from '@rollup/plugin-json'
import nodeResolve from '@rollup/plugin-node-resolve'
import resolve from '@rollup/plugin-replace'
const NODE_ENV =
process.env['NODE_ENV'] === 'development' ? 'development' : 'production'
/**
* @template T
* @param {{ default: T }} f
* @see {@link https://github.com/rollup/plugins/issues/1541}
*/
const fix = (f) => /** @type {T} */ (f)
export default [
defineConfig({
input: 'dist/app.js',
output: { format: 'iife', file: 'dist/app.bundle.js' },
plugins: [
fix(json)(),
fix(commonjs)(),
fix(nodeResolve)({ preferBuiltins: false, browser: true }),
fix(resolve)({
preventAssignment: true,
values: { 'process.env.NODE_ENV': NODE_ENV },
}),
],
}),
]
if i understood correctly, this PR will produce:
dist/cjs/index.js
dist/es/index.js
dist/cjs/index.d.ts
(copy of types/index.d.ts
)dist/es/index.d.ts
(copy of types/index.d.ts
)is that actually right? don't we actually need:
dist/cjs/index.js
dist/es/index.js
dist/cjs/index.d.ts
dist/es/index.d.mts
since we're in a CJS package, i would've assumed typescript would default to *.d.ts
being commonjs here
so ATTW would still complain about cjs masquerading
typescript
andnode-resolve
, probably allThe repo is really minimal. The important part is that it's a module:
https://github.com/lazarljubenovic/issue-repro-rollup-plugin/blob/097e003ae506342974740a58788e65f7a951ad40/package.json#L5
And that I'm just trying to import and invoke it.
https://github.com/lazarljubenovic/issue-repro-rollup-plugin/blob/097e003ae506342974740a58788e65f7a951ad40/index.ts#L1-2
Expected Behavior
It compiles.
Actual Behavior
It gives the following type error.
The generated codes works, though -- the function call is successfully performed and returns the correct value.
Additional Information
The issue is happening because the type of my import is treated as if it was written in CommonJS. In other words, even thought the runtime will perform
import typescript from '@rollup/plugin-typescript'
, types are resolved as if I've writtenimport typescript = require('@rollup/plugin-typescript')
. This explains the error, where TS believes thattypescript
is the whole module. In fact, if I change my code totypescript.default()
, it compiles but fails at runtime because the default export is a function without a propertydefault
.The root cause is simply the way TS works at the moment. It's a surprising behvaior, but it's documented:
Since the extension is
.d.ts
, and the rollup plugin package is not a module, TypeScript determines that.d.ts
is always a CJS module. Theexports.types
setting is correctly used, but what it points to is interpreted as CJS.Therefore, the package must be distributed with two distinct declaration files. For example, after making the following changes in my
node_modules
folder, everything works correctly.The contents of
index.d.mts
andindex.d.cts
are identical to the originalindex.d.ts
.This works with both
"type": "module"
and"type": "commonjs"
.