fix(vm): fix external module resolve error with deps optimizer query … · vitest-dev/vitest@9dbf477 (original) (raw)
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -65,6 +65,14 @@ export function cleanUrl(url: string): string { | ||
| 65 | 65 | return url.replace(postfixRE, '') |
| 66 | 66 | } |
| 67 | 67 | |
| 68 | +export function splitFileAndPostfix(path: string): { | |
| 69 | +file: string | |
| 70 | +postfix: string | |
| 71 | +} { | |
| 72 | +const file = cleanUrl(path) | |
| 73 | +return { file, postfix: path.slice(file.length) } | |
| 74 | +} | |
| 75 | + | |
| 68 | 76 | const externalRE = /^(?:[a-z]+:)?\/\// |
| 69 | 77 | export const isExternalUrl = (url: string): boolean => externalRE.test(url) |
| 70 | 78 |
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -5,7 +5,7 @@ import type { VMModule } from './vm/types' | ||||
| 5 | 5 | import fs from 'node:fs' | ||
| 6 | 6 | import { isBuiltin } from 'node:module' | ||
| 7 | 7 | import { fileURLToPath, pathToFileURL } from 'node:url' | ||
| 8 | -import { isBareImport } from '@vitest/utils/helpers' | |||
| 8 | +import { isBareImport, splitFileAndPostfix } from '@vitest/utils/helpers' | |||
| 9 | 9 | import { findNearestPackageData } from '@vitest/utils/resolver' | ||
| 10 | 10 | import { extname, normalize } from 'pathe' | ||
| 11 | 11 | import { CommonjsExecutor } from './vm/commonjs-executor' | ||
| @@ -125,7 +125,8 @@ export class ExternalModulesExecutor { | ||||
| 125 | 125 | return { type: 'data', url: identifier, path: identifier } | ||
| 126 | 126 | } | ||
| 127 | 127 | |||
| 128 | -const extension = extname(identifier) | |||
| 128 | +const { file, postfix } = splitFileAndPostfix(identifier) | |||
| 129 | +const extension = extname(file) | |||
| 129 | 130 | if (extension === '.node' | | isBuiltin(identifier)) { | |
| 130 | 131 | return { type: 'builtin', url: identifier, path: identifier } | ||
| 131 | 132 | } | ||
| @@ -138,10 +139,8 @@ export class ExternalModulesExecutor { | ||||
| 138 | 139 | } | ||
| 139 | 140 | |||
| 140 | 141 | const isFileUrl = identifier.startsWith('file://') | ||
| 141 | -const pathUrl = isFileUrl | |||
| 142 | - ? fileURLToPath(identifier.split('?')[0]) | |||
| 143 | - : identifier | |||
| 144 | -const fileUrl = isFileUrl ? identifier : pathToFileURL(pathUrl).toString() | |||
| 142 | +const pathUrl = isFileUrl ? fileURLToPath(file) : file | |||
| 143 | +const fileUrl = isFileUrl ? identifier : `${pathToFileURL(file)}${postfix}` | |||
| 145 | 144 | |||
| 146 | 145 | let type: 'module' | 'commonjs' | 'vite' | 'wasm' |
| 147 | 146 | if (this.vite.canResolve(fileUrl)) { |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| 1 | +import { defineConfig } from "vitest/config"; | |
| 2 | + | |
| 3 | +export default defineConfig({ | |
| 4 | +optimizeDeps: { | |
| 5 | +include: ["@test/test-dep-url"], | |
| 6 | +}, | |
| 7 | +ssr: { | |
| 8 | +optimizeDeps: { | |
| 9 | +include: ["@test/test-dep-url"], | |
| 10 | +}, | |
| 11 | +}, | |
| 12 | +test: { | |
| 13 | +deps: { | |
| 14 | +optimizer: { | |
| 15 | +client: { | |
| 16 | +enabled: true, | |
| 17 | +}, | |
| 18 | +ssr: { | |
| 19 | +enabled: true, | |
| 20 | +}, | |
| 21 | +}, | |
| 22 | +}, | |
| 23 | +}, | |
| 24 | +}); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,33 +1,16 @@ | ||
| 1 | 1 | import { expect, test } from 'vitest' |
| 2 | 2 | import { runVitest } from '../../test-utils' |
| 3 | 3 | |
| 4 | -test('optimize deps optimizes them into node_modules/.vite', async () => { | |
| 5 | -const { errorTree, stderr } = await runVitest({ | |
| 6 | -root: './fixtures/optimize-deps', | |
| 7 | -deps: { | |
| 8 | -optimizer: { | |
| 9 | -client: { | |
| 10 | -enabled: true, | |
| 11 | -}, | |
| 12 | -ssr: { | |
| 13 | -enabled: true, | |
| 14 | -}, | |
| 15 | -}, | |
| 16 | -}, | |
| 17 | -$viteConfig: { | |
| 18 | -optimizeDeps: { | |
| 19 | -include: ['@test/test-dep-url'], | |
| 20 | -}, | |
| 21 | -ssr: { | |
| 22 | -optimizeDeps: { | |
| 23 | -include: ['@test/test-dep-url'], | |
| 24 | -}, | |
| 25 | -}, | |
| 26 | -}, | |
| 27 | -}) | |
| 4 | +test.for(['forks', 'threads', 'vmThreads', 'vmForks'])( | |
| 5 | +'optimize deps optimizes them into node_modules/.vite - %s', | |
| 6 | +async (pool) => { | |
| 7 | +const { errorTree, stderr } = await runVitest({ | |
| 8 | +root: './fixtures/optimize-deps', | |
| 9 | + pool, | |
| 10 | +}) | |
| 28 | 11 | |
| 29 | -expect(stderr).toBe('') | |
| 30 | -expect(errorTree()).toMatchInlineSnapshot(` | |
| 12 | + expect(stderr).toBe('') | |
| 13 | + expect(errorTree()).toMatchInlineSnapshot(` | |
| 31 | 14 | { |
| 32 | 15 | "ssr.test.ts": { |
| 33 | 16 | "import.meta.url": "passed", |
| @@ -37,4 +20,5 @@ test('optimize deps optimizes them into node_modules/.vite', async () => { | ||
| 37 | 20 | }, |
| 38 | 21 | } |
| 39 | 22 | `) |
| 40 | -}) | |
| 23 | +}, | |
| 24 | +) |