Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions packages/vite/src/module-runner/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,24 @@ export class ModuleRunner {
exports: Record<string, any>,
fetchResult: ResolvedResult,
metadata?: SSRImportMetadata,
isCompleteModule = true,
) {
if (!('externalize' in fetchResult)) {
// Cyclic inlined graphs may return a partial exports object while the
// exporter is still evaluating. Named-export checks must wait until the
// module finished, matching Node (and avoiding false SyntaxErrors).
if (!isCompleteModule) {
return exports
}
const { url, type } = fetchResult
if (type !== 'module' && type !== 'commonjs') return exports
analyzeImportedModDifference(exports, url, type, metadata)
if ('externalize' in fetchResult) {
const { url, type } = fetchResult
if (type !== 'module' && type !== 'commonjs') return exports
analyzeImportedModDifference(exports, url, type, metadata)
return exports
}
// Vite-transformed modules are ESM. Previously this path skipped
// analyzeImportedModDifference entirely, so `import { missing }` from a
// local file did not throw (Node throws SyntaxError).
analyzeImportedModDifference(exports, fetchResult.url, 'module', metadata)
return exports
}

Expand Down Expand Up @@ -185,7 +196,7 @@ export class ModuleRunner {
mod.exports &&
(callstack.includes(moduleId) || this.isCircularRequest(mod, callstack))
) {
return this.processImport(mod.exports, meta, metadata)
return this.processImport(mod.exports, meta, metadata, false)
}
return this.processImport(await mod.promise, meta, metadata)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { test } from './simple.js'

export const result = test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { nonExisting } from './simple.js'

export const result = nonExisting
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,19 @@ describe('module runner initialization', async () => {
})
})

it('importing inlined esm module checks exports', async ({ runner }) => {
await expect(() =>
runner.import('/fixtures/esm-internal-non-existing.js'),
).rejects.toThrowError(
`[vite] The requested module '/fixtures/simple.js' does not provide an export named 'nonExisting'`,
)
await expect(
runner.import('/fixtures/esm-internal-existing.js'),
).resolves.toMatchObject({
result: 'I am initialized',
})
})

it("dynamic import doesn't produce duplicates", async ({ runner }) => {
const mod = await runner.import('/fixtures/dynamic-import.js')
const modules = await mod.initialize()
Expand Down
Loading