Skip to content

Commit f599ab4

Browse files
fix: adjust import analysis behavior to match Node (#16738)
1 parent 48edfcd commit f599ab4

File tree

3 files changed

+17
-37
lines changed

3 files changed

+17
-37
lines changed

packages/vite/src/node/ssr/ssrModuleLoader.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
import {
1717
type SSRImportBaseMetadata,
1818
analyzeImportedModDifference,
19-
proxyGuardOnlyEsm,
2019
} from '../../shared/ssrTransform'
2120
import { SOURCEMAPPING_URL } from '../../shared/constants'
2221
import {
@@ -317,7 +316,7 @@ async function nodeImport(
317316
: undefined,
318317
metadata,
319318
)
320-
return proxyGuardOnlyEsm(mod, id)
319+
return mod
321320
} else {
322321
return mod
323322
}

packages/vite/src/runtime/runtime.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ import {
88
unwrapId,
99
wrapId,
1010
} from '../shared/utils'
11-
import {
12-
analyzeImportedModDifference,
13-
proxyGuardOnlyEsm,
14-
} from '../shared/ssrTransform'
11+
import { analyzeImportedModDifference } from '../shared/ssrTransform'
1512
import { ModuleCacheMap } from './moduleCache'
1613
import type {
1714
FetchResult,
@@ -193,7 +190,7 @@ export class ViteRuntime {
193190
const { id, type } = fetchResult
194191
if (type !== 'module' && type !== 'commonjs') return exports
195192
analyzeImportedModDifference(exports, id, type, metadata)
196-
return proxyGuardOnlyEsm(exports, id, metadata)
193+
return exports
197194
}
198195

199196
private async cachedRequest(

packages/vite/src/shared/ssrTransform.ts

+14-30
Original file line numberDiff line numberDiff line change
@@ -28,48 +28,32 @@ export function analyzeImportedModDifference(
2828
): void {
2929
// No normalization needed if the user already dynamic imports this module
3030
if (metadata?.isDynamicImport) return
31-
// If file path is ESM, everything should be fine
32-
if (moduleType === 'module') return
3331

34-
// For non-ESM, named imports is done via static analysis with cjs-module-lexer in Node.js.
3532
// If the user named imports a specifier that can't be analyzed, error.
33+
// If the module doesn't import anything explicitly, e.g. `import 'foo'` or
34+
// `import * as foo from 'foo'`, we can skip.
3635
if (metadata?.importedNames?.length) {
3736
const missingBindings = metadata.importedNames.filter((s) => !(s in mod))
3837
if (missingBindings.length) {
3938
const lastBinding = missingBindings[missingBindings.length - 1]
40-
// Copied from Node.js
41-
throw new SyntaxError(`\
39+
40+
// For invalid named exports only, similar to how Node.js errors for top-level imports.
41+
// But since we transform as dynamic imports, we need to emulate the error manually.
42+
if (moduleType === 'module') {
43+
throw new SyntaxError(
44+
`[vite] The requested module '${rawId}' does not provide an export named '${lastBinding}'`,
45+
)
46+
} else {
47+
// For non-ESM, named imports is done via static analysis with cjs-module-lexer in Node.js.
48+
// Copied from Node.js
49+
throw new SyntaxError(`\
4250
[vite] Named export '${lastBinding}' not found. The requested module '${rawId}' is a CommonJS module, which may not support all module.exports as named exports.
4351
CommonJS modules can always be imported via the default export, for example using:
4452
4553
import pkg from '${rawId}';
4654
const {${missingBindings.join(', ')}} = pkg;
4755
`)
56+
}
4857
}
4958
}
5059
}
51-
52-
/**
53-
* Guard invalid named exports only, similar to how Node.js errors for top-level imports.
54-
* But since we transform as dynamic imports, we need to emulate the error manually.
55-
*/
56-
export function proxyGuardOnlyEsm(
57-
mod: any,
58-
rawId: string,
59-
metadata?: SSRImportBaseMetadata,
60-
): any {
61-
// If the module doesn't import anything explicitly, e.g. `import 'foo'` or
62-
// `import * as foo from 'foo'`, we can skip the proxy guard.
63-
if (!metadata?.importedNames?.length) return mod
64-
65-
return new Proxy(mod, {
66-
get(mod, prop) {
67-
if (prop !== 'then' && !(prop in mod)) {
68-
throw new SyntaxError(
69-
`[vite] The requested module '${rawId}' does not provide an export named '${prop.toString()}'`,
70-
)
71-
}
72-
return mod[prop]
73-
},
74-
})
75-
}

0 commit comments

Comments
 (0)