typescript-go icon indicating copy to clipboard operation
typescript-go copied to clipboard

Not able to import defineConfig from Vite

Open mxschmitt opened this issue 7 months ago • 1 comments

➜  vite-import-tsc git:(main) npx tsc -p .                                                        
➜  vite-import-tsc git:(main) /Users/maxschmitt/Developer/tmp/typescript-go/built/local/tsgo  -p .
vite.config.ts:17:10 - error TS2305: Module '"/Users/maxschmitt/Developer/tmp/vite-import-tsc/node_modules/vite/index"' has no exported member 'defineConfig'.

17 import { defineConfig } from 'vite';
            ~~~~~~~~~~~~

Found 1 error in vite.config.ts:17

➜  vite-import-tsc git:(main) 

Reproduction: https://github.com/mxschmitt/typescript-go-issue-vite-defineConfig-types.git

tsc works while tsgo does not.

Commit: 5cae442617a524c0e4a2a16ad208a65df8e8c140

Extracted minimal reproduction from https://github.com/microsoft/typescript-go/issues/895.

mxschmitt avatar May 21 '25 21:05 mxschmitt

Possibly related to https://github.com/microsoft/typescript-go/issues/518

jrmyio avatar May 23 '25 10:05 jrmyio

Does the issue persist with the recent builds? I can't repro it

Andarist avatar Jun 19 '25 18:06 Andarist

It does not!

PS C:\server> yarn tsc
vitest.config.ts:9:18 - error TS7006: Parameter 'id' implicitly has an 'any' type.

9       async load(id) {
                   ~~

Found 1 error in 1 file.

Errors  Files
     1  vitest.config.ts:9

PS C:\server> yarn up @typescript/native-preview -R
➤ YN0000: · Yarn 4.9.1
➤ YN0000: ┌ Resolution step
➤ YN0085: │ + @typescript/native-preview@npm:7.0.0-dev.20250619.1, and 7 more.
➤ YN0085: │ - @typescript/native-preview-darwin-arm64@npm:7.0.0-dev.20250528.1, and 7 more.
➤ YN0000: └ Completed in 1s 967ms
➤ YN0000: ┌ Fetch step
➤ YN0013: │ 7 packages were added to the project, and 7 were removed (+ 11.24 MiB).
➤ YN0000: └ Completed in 2s 268ms
➤ YN0000: ┌ Link step
➤ YN0008: │ server@workspace:. must be rebuilt because its dependency tree changed
➤ YN0000: └ Completed in 1s 283ms
➤ YN0000: · Done in 5s 610ms
PS C:\server> yarn tsc
PS C:\server>

wojtekmaj avatar Jun 19 '25 20:06 wojtekmaj

Closing as fixed - Thanks!

mxschmitt avatar Jun 19 '25 21:06 mxschmitt

I'm personally still seeing it on 7.0.0-dev.20250620.1.

EDIT: it looks like it happens on project configured in commonjs or with commonjs set as module in tsconfig. It seems to only be fixed if the module system is set as ESM for both the project and tsconfig.

Marsup avatar Jun 20 '25 11:06 Marsup

Was fixed for me about a week ago and I've been routinely updating. Not sure what PR fixed it but has been good for me

smaccoun avatar Jun 20 '25 13:06 smaccoun

@Marsup please provide a repro case so somebody could investigate further

Andarist avatar Jun 20 '25 17:06 Andarist

Here it is: https://github.com/Marsup/typescript-go-issue-904 Just run npm ci && npm test

Marsup avatar Jun 20 '25 17:06 Marsup

It'd be appreciated if any new repros were filed as new issues; it's not easy to track problems on closed issues.

jakebailey avatar Jun 26 '25 20:06 jakebailey

Sorry for that, do you need a new one? When it's the same symptom, it's hard to know whether the issue should be reopened because of an imperfect fix or because it's a different issue leading to the same result.

Marsup avatar Jun 26 '25 21:06 Marsup

When I try your repro, the resolution fails and jumping to the resolved module brings up this file:

/**
 * @deprecated The CJS build of Vite's Node API is deprecated. See https://vite.dev/guide/troubleshooting.html#vite-cjs-node-api-deprecated for more details.
 */
declare const module: any

export = module

Which says that using vite with CJS isn't supported, and that's what your repro is doing.

jakebailey avatar Jun 26 '25 22:06 jakebailey

The only reason this would have worked in the old compiler is that your code is using the old node10 resolution mode (not recommended), which mistakenly resolves vite to the ESM definition instead of detecting that file.ts is a CJS file. Now, tsgo doesn't have node10 and instead remaps it to bundler, which does correctly map it.

jakebailey avatar Jun 26 '25 22:06 jakebailey

A fresh issue would be helpful, because the initial repro here was for bundler mode, which was broken for a different reason. (That's why I was asking for one 😄)

jakebailey avatar Jun 26 '25 22:06 jakebailey

Asked @andrewbranch and this the above is working as intended; this code would emit CJS (it sets module=commonjs) and therefore would simply not work when used with Vite anyway.

jakebailey avatar Jun 26 '25 22:06 jakebailey