microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.4k stars 12.41k forks source link

`export {_default as default}` + `import x` results incorrect for `node16` resolution #50067

Closed JounQin closed 2 years ago

JounQin commented 2 years ago

Bug Report

🔎 Search Terms

🕗 Version & Regression Information

⏯ Playground Link

Playground link with relevant code

reproduction https://github.com/un-ts/lib-boilerplate/blob/main/vitest.config.ts#L8

💻 Code

import autoImport from 'unplugin-auto-import/vite'
import { defineConfig } from 'vitest/config'

export default defineConfig({
  plugins: [
    autoImport({
      imports: 'vitest',
    }),
  ],
  test: {
    coverage: {
      reporter: ['lcov', 'json'],
    },
  },
})

🙁 Actual behavior

vitest.config.ts:6:5 - error TS2349: This expression is not callable.
  Type 'typeof import("/Users/JounQin/Workspaces/GitHub/domiso/node_modules/.pnpm/unplugin-auto-import@0.10.1_2rke2oyl5nfm2lrwejsnq3sdfe_zhxiakfykinkigydwrmzev47um/node_modules/unplugin-auto-import/dist/vite")' has no call signatures.

6     autoImport({

🙂 Expected behavior

No error

weswigham commented 2 years ago

unplugin-auto-import has a broken export map. Their export map defines all their entrypoints as cjs only - they need a nested types entry for both the require and import conditions that points a different files with the correct module format. This is exactly the format confusion issue that arises when you use an incomplete export map.

Instead of

    "./vite": {
      "types": "./dist/vite.d.ts",
      "require": "./dist/vite.js",
      "import": "./dist/vite.mjs"
    },

it should be

    "./vite": {
      "require": { "types": "./dist/vite.d.ts", "default": "./dist/vite.js" },
      "import": { "types": "./dist/vite.d.mts", "default": "./dist/vite.mjs" }
    },

and, naturally, vite.d.mts needs to exist (it can just reexport stuff from vite.d.ts, though!).