Skip to content
Merged
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
9 changes: 9 additions & 0 deletions packages/vitest/src/node/environments/fetchModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { isExternalUrl, unwrapId } from '@vitest/utils/helpers'
import { join } from 'pathe'
import { fetchModule } from 'vite'
import { hash } from '../hash'
import { normalizeResolvedIdToUrl } from './normalizeUrl'

const saveCachePromises = new Map<string, Promise<FetchResult>>()
const readFilePromises = new Map<string, Promise<string | null>>()
Expand Down Expand Up @@ -54,6 +55,14 @@ class ModuleFetcher {
return { externalize: url, type: 'network' }
}

// handle unresolved id of dynamic import skipped by Vite import analysis
if (url[0] !== '/') {
const resolved = await environment.pluginContainer.resolveId(url, importer)
if (resolved) {
url = normalizeResolvedIdToUrl(environment, resolved.id)
}
}

const moduleGraphModule = await environment.moduleGraph.ensureEntryFromUrl(unwrapId(url))
const cached = !!moduleGraphModule.transformResult

Expand Down
12 changes: 1 addition & 11 deletions packages/vitest/src/runtime/moduleRunner/moduleEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,21 +322,11 @@ export class VitestModuleEvaluator implements ModuleEvaluator {
? vm.runInContext(wrappedCode, this.vm.context, options)
: vm.runInThisContext(wrappedCode, options)

const dynamicRequest = async (dep: string, options: ImportCallOptions) => {
dep = String(dep)
// TODO: support more edge cases?
// vite doesn't support dynamic modules by design, but we have to
if (dep[0] === '#') {
return context[ssrDynamicImportKey](wrapId(dep), options)
}
return context[ssrDynamicImportKey](dep, options)
}

await initModule(
context[ssrModuleExportsKey],
context[ssrImportMetaKey],
context[ssrImportKey],
dynamicRequest,
context[ssrDynamicImportKey],
context[ssrExportAllKey],
// vite 7 support, remove when vite 7+ is supported
(context as any).__vite_ssr_exportName__
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/config/deps/test-dep-virtual/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from 'virtual:test'
6 changes: 6 additions & 0 deletions test/config/deps/test-dep-virtual/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "@vitejs/test-dep-virtual",
"type": "module",
"private": true,
"exports": "./index.js"
}
7 changes: 7 additions & 0 deletions test/config/fixtures/external/dynamic/basic.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { expect, it } from "vitest";

it("basic", async () => {
const getId = () => "@vitejs/test-dep-virtual";
const mod = await import(getId());
expect(mod.test).toBe("ok");
});
22 changes: 22 additions & 0 deletions test/config/fixtures/external/dynamic/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { defineConfig } from "vitest/config";

export default defineConfig({
ssr: {
noExternal: ["@vitejs/test-dep-virtual"],
},
plugins: [
{
name: 'test-virtual',
resolveId(source) {
if (source === 'virtual:test') {
return '\0' + source;
}
},
load(id) {
if (id === '\0virtual:test') {
return `export const test = "ok";`;
}
}
}
],
});
1 change: 1 addition & 0 deletions test/config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
},
"devDependencies": {
"@test/test-dep-config": "link:./deps/test-dep-config",
"@vitejs/test-dep-virtual": "file:./deps/test-dep-virtual",
"@vitest/browser-playwright": "workspace:*",
"@vitest/browser-preview": "workspace:*",
"@vitest/browser-webdriverio": "workspace:*",
Expand Down
15 changes: 15 additions & 0 deletions test/config/test/external.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { expect, it } from 'vitest'
import { runVitest } from '../../test-utils'

it('can inline fully dynamic import', async () => {
const { errorTree } = await runVitest({
root: 'fixtures/external/dynamic',
})
expect(errorTree()).toMatchInlineSnapshot(`
{
"basic.test.ts": {
"basic": "passed",
},
}
`)
})
Loading