fix: try to resolve id, if relative path is provided (#2461)

* fix: try to resolve id, if relative path is provided

* chore: update lockfile
This commit is contained in:
Vladimir 2022-12-08 11:46:19 +01:00 committed by GitHub
parent f02c982865
commit e9cb4136b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 77 additions and 67 deletions

View File

@ -58,7 +58,7 @@ export class C8CoverageProvider implements CoverageProvider {
if (!map)
return
const filepath = result.file || file.split('?')[0]
const filepath = file.split('?')[0]
const url = _url.pathToFileURL(filepath).href

View File

@ -4,7 +4,7 @@ import vm from 'vm'
import { dirname, extname, isAbsolute, resolve } from 'pathe'
import { isNodeBuiltin } from 'mlly'
import createDebug from 'debug'
import { isPrimitive, mergeSlashes, normalizeModuleId, normalizeRequestId, slash, toFilePath } from './utils'
import { cleanUrl, isPrimitive, normalizeModuleId, normalizeRequestId, slash, toFilePath } from './utils'
import type { HotContext, ModuleCache, ViteNodeRunnerOptions } from './types'
import { extractSourceMap } from './source-map'
@ -221,31 +221,35 @@ export class ViteNodeRunner {
Object.defineProperty(request, 'callstack', { get: () => callstack })
const resolveId = async (dep: string, callstackPosition = 1) => {
const resolveId = async (dep: string, callstackPosition = 1): Promise<[dep: string, id: string | undefined]> => {
if (this.options.resolveId && this.shouldResolveId(dep)) {
let importer = callstack[callstack.length - callstackPosition]
let importer: string | undefined = callstack[callstack.length - callstackPosition]
if (importer && !dep.startsWith('.'))
importer = undefined
if (importer && importer.startsWith('mock:'))
importer = importer.slice(5)
const { id } = await this.options.resolveId(dep, importer) || {}
dep = id && isAbsolute(id) ? mergeSlashes(`/@fs/${id}`) : id || dep
const resolved = await this.options.resolveId(normalizeRequestId(dep), importer)
return [dep, resolved?.id]
}
return dep
return [dep, undefined]
}
const [dep, resolvedId] = await resolveId(id, 2)
const requestStubs = this.options.requestStubs || DEFAULT_REQUEST_STUBS
if (id in requestStubs)
return requestStubs[id]
// eslint-disable-next-line prefer-const
let { code: transformed, externalize, file } = await this.options.fetchModule(id)
let { code: transformed, externalize } = await this.options.fetchModule(resolvedId || dep)
// in case we resolved fsPath incorrectly, Vite will return the correct file path
// in that case we need to update cache, so we don't have the same module as different exports
// but we ignore fsPath that has custom query, because it might need to be different
if (file && !fsPath.includes('?') && fsPath !== file) {
if (this.moduleCache.has(file)) {
mod = this.moduleCache.get(file)
if (resolvedId && !fsPath.includes('?') && fsPath !== resolvedId) {
if (this.moduleCache.has(resolvedId)) {
mod = this.moduleCache.get(resolvedId)
this.moduleCache.set(fsPath, mod)
if (mod.promise)
return mod.promise
@ -253,7 +257,7 @@ export class ViteNodeRunner {
return mod.exports
}
else {
this.moduleCache.set(file, mod)
this.moduleCache.set(resolvedId, mod)
}
}
@ -267,8 +271,10 @@ export class ViteNodeRunner {
if (transformed == null)
throw new Error(`[vite-node] Failed to load ${id}`)
const file = cleanUrl(resolvedId || fsPath)
// console.log('file', file)
// disambiguate the `<UNIT>:/` on windows: see nodejs/node#31710
const url = pathToFileURL(file || fsPath).href
const url = pathToFileURL(file).href
const meta = { url }
const exports = Object.create(null)
Object.defineProperty(exports, Symbol.toStringTag, {

View File

@ -4,7 +4,7 @@ import type { TransformResult, ViteDevServer } from 'vite'
import createDebug from 'debug'
import type { DebuggerOptions, FetchResult, RawSourceMap, ViteNodeResolveId, ViteNodeServerOptions } from './types'
import { shouldExternalize } from './externalize'
import { cleanUrl, normalizeModuleId, toArray, toFilePath } from './utils'
import { normalizeModuleId, toArray, toFilePath } from './utils'
import { Debugger } from './debug'
import { withInlineSourcemap } from './source-map'
@ -130,8 +130,6 @@ export class ViteNodeServer {
const module = this.server.moduleGraph.getModuleById(id)
const timestamp = module ? module.lastHMRTimestamp : null
const cache = this.fetchCache.get(filePath)
if (cache?.result.id)
id = cache.result.id
if (timestamp !== null && cache && cache.timestamp >= timestamp)
return cache.result
@ -143,16 +141,10 @@ export class ViteNodeServer {
this.debugger?.recordExternalize(id, externalize)
}
else {
let file = module?.file
if (!file) {
const [, resolvedId] = await this.server.moduleGraph.resolveUrl(id, true)
id = resolvedId
file = cleanUrl(resolvedId)
}
const start = performance.now()
const r = await this._transformRequest(id)
duration = performance.now() - start
result = { file, id, code: r?.code, map: r?.map as unknown as RawSourceMap }
result = { code: r?.code, map: r?.map as unknown as RawSourceMap }
}
this.fetchCache.set(filePath, {

View File

@ -31,8 +31,6 @@ export interface FetchResult {
code?: string
externalize?: string
map?: RawSourceMap
id?: string
file?: string
}
export type HotContext = Omit<ViteHotContext, 'acceptDeps' | 'decline'>

View File

@ -30,6 +30,10 @@ export class VitestRunner extends ViteNodeRunner {
prepareContext(context: Record<string, any>) {
const request = context.__vite_ssr_import__
const resolveId = context.__vitest_resolve_id__
const resolveUrl = async (dep: string) => {
const [id, resolvedId] = await resolveId(dep)
return resolvedId || id
}
const mocker = new VitestMocker(this.options, this.moduleCache, request)
@ -42,8 +46,8 @@ export class VitestRunner extends ViteNodeRunner {
}
return Object.assign(context, {
__vite_ssr_import__: async (dep: string) => mocker.requestWithMock(await resolveId(dep)),
__vite_ssr_dynamic_import__: async (dep: string) => mocker.requestWithMock(await resolveId(dep)),
__vite_ssr_import__: async (dep: string) => mocker.requestWithMock(await resolveUrl(dep)),
__vite_ssr_dynamic_import__: async (dep: string) => mocker.requestWithMock(await resolveUrl(dep)),
__vitest_mocker__: mocker,
})
}

83
pnpm-lock.yaml generated
View File

@ -236,7 +236,7 @@ importers:
react-dom: 18.0.0_react@18.0.0
devDependencies:
'@testing-library/react': 13.3.0_zpnidt7m3osuk7shl3s4oenomq
'@types/node': 18.11.10
'@types/node': 18.11.11
'@types/react': 18.0.26
'@vitejs/plugin-react': 2.2.0
jsdom: 20.0.3
@ -288,7 +288,7 @@ importers:
'@types/react-test-renderer': 17.0.2
'@vitejs/plugin-react': 2.2.0_vite@3.2.3
'@vitest/ui': link:../../packages/ui
happy-dom: 7.7.2
happy-dom: 8.1.0
jsdom: 20.0.3
react-test-renderer: 17.0.2_react@17.0.2
vite: 3.2.3
@ -978,7 +978,7 @@ importers:
'@vitejs/plugin-vue': 3.2.0_vite@3.2.3+vue@3.2.45
'@vue/test-utils': 2.2.6_vue@3.2.45
execa: 6.1.0
happy-dom: 7.7.2
happy-dom: 8.1.0
vite: 3.2.3
vitest: link:../../packages/vitest
vue: 3.2.45
@ -1076,10 +1076,12 @@ importers:
execa: ^6.1.0
pkg-reporter: ./reportPkg/
vitest: workspace:*
vitest-sonar-reporter: 0.3.3
devDependencies:
execa: 6.1.0
pkg-reporter: link:reportPkg
vitest: link:../../packages/vitest
vitest-sonar-reporter: 0.3.3_vitest@packages+vitest
test/resolve:
specifiers:
@ -4526,7 +4528,7 @@ packages:
dependencies:
'@types/istanbul-lib-coverage': 2.0.4
'@types/istanbul-reports': 3.0.1
'@types/node': 18.11.10
'@types/node': 18.11.11
'@types/yargs': 15.0.14
chalk: 4.1.2
dev: true
@ -4538,7 +4540,7 @@ packages:
'@jest/schemas': 29.0.0
'@types/istanbul-lib-coverage': 2.0.4
'@types/istanbul-reports': 3.0.1
'@types/node': 18.11.10
'@types/node': 18.11.11
'@types/yargs': 17.0.12
chalk: 4.1.2
dev: true
@ -5051,7 +5053,7 @@ packages:
engines: {node: '>=14'}
hasBin: true
dependencies:
'@types/node': 18.11.10
'@types/node': 18.11.11
playwright-core: 1.28.0
dev: true
@ -6867,7 +6869,7 @@ packages:
/@types/cheerio/0.22.31:
resolution: {integrity: sha512-Kt7Cdjjdi2XWSfrZ53v4Of0wG3ZcmaegFXjMmz9tfNrZSkzzo36G0AL1YqSdcIA78Etjt6E609pt5h1xnQkPUw==}
dependencies:
'@types/node': 18.11.10
'@types/node': 18.11.11
dev: true
/@types/codemirror/5.60.5:
@ -6879,7 +6881,7 @@ packages:
/@types/concat-stream/1.6.1:
resolution: {integrity: sha512-eHE4cQPoj6ngxBZMvVf6Hw7Mh4jMW4U9lpGmS5GBPB9RYxlFg+CHaVN7ErNY4W9XfLIEn20b4VDYaIrbq0q4uA==}
dependencies:
'@types/node': 18.11.10
'@types/node': 18.11.11
dev: true
/@types/cookie/0.4.1:
@ -6939,33 +6941,33 @@ packages:
/@types/form-data/0.0.33:
resolution: {integrity: sha512-8BSvG1kGm83cyJITQMZSulnl6QV8jqAGreJsc5tPu1Jq0vTSOiY/k24Wx82JRpWwZSqrala6sd5rWi6aNXvqcw==}
dependencies:
'@types/node': 18.11.10
'@types/node': 18.11.11
dev: true
/@types/fs-extra/9.0.13:
resolution: {integrity: sha512-nEnwB++1u5lVDM2UI4c1+5R+FYaKfaAzS4OococimjVm3nQw3TuzH5UNsocrcTBbhnerblyHj4A49qXbIiZdpA==}
dependencies:
'@types/node': 18.11.10
'@types/node': 18.11.11
dev: true
/@types/glob/7.2.0:
resolution: {integrity: sha512-ZUxbzKl0IfJILTS6t7ip5fQQM/J3TJYubDm3nMbgubNNYS62eXeUpoLUC8/7fJNiFYHTrGPQn7hspDUzIHX3UA==}
dependencies:
'@types/minimatch': 5.1.1
'@types/node': 18.11.10
'@types/node': 18.11.11
dev: true
/@types/glob/8.0.0:
resolution: {integrity: sha512-l6NQsDDyQUVeoTynNpC9uRvCUint/gSUXQA2euwmTuWGvPY5LSDUu6tkCtJB2SvGQlJQzLaKqcGZP4//7EDveA==}
dependencies:
'@types/minimatch': 5.1.1
'@types/node': 18.11.10
'@types/node': 18.11.11
dev: true
/@types/graceful-fs/4.1.5:
resolution: {integrity: sha512-anKkLmZZ+xm4p8JWBf4hElkM4XR+EZeA2M9BAkkTldmcyDY4mbdIJnRghDJH3Ov5ooY7/UAoENtmdMSkaAd7Cw==}
dependencies:
'@types/node': 18.11.10
'@types/node': 18.11.11
dev: true
/@types/hast/2.3.4:
@ -7026,7 +7028,7 @@ packages:
/@types/jsdom/20.0.0:
resolution: {integrity: sha512-YfAchFs0yM1QPDrLm2VHe+WHGtqms3NXnXAMolrgrVP6fgBHHXy1ozAbo/dFtPNtZC/m66bPiCTWYmqp1F14gA==}
dependencies:
'@types/node': 18.11.10
'@types/node': 18.11.11
'@types/tough-cookie': 4.0.2
parse5: 7.0.0
dev: true
@ -7070,7 +7072,7 @@ packages:
/@types/node-fetch/2.6.2:
resolution: {integrity: sha512-DHqhlq5jeESLy19TYhLakJ07kNumXWjcDdxXsLUMJZ6ue8VZJj4kLPQVE/2mdHh3xZziNF1xppu5lwmS53HR+A==}
dependencies:
'@types/node': 18.11.10
'@types/node': 18.11.11
form-data: 3.0.1
dev: true
@ -7086,8 +7088,8 @@ packages:
resolution: {integrity: sha512-aFcUkv7EddxxOa/9f74DINReQ/celqH8DiB3fRYgVDM2Xm5QJL8sl80QKuAnGvwAsMn+H3IFA6WCrQh1CY7m1A==}
dev: true
/@types/node/18.11.10:
resolution: {integrity: sha512-juG3RWMBOqcOuXC643OAdSA525V44cVgGV6dUDuiFtss+8Fk5x1hI93Rsld43VeJVIeqlP9I7Fn9/qaVqoEAuQ==}
/@types/node/18.11.11:
resolution: {integrity: sha512-KJ021B1nlQUBLopzZmPBVuGU9un7WJd/W4ya7Ih02B4Uwky5Nja0yGYav2EfYIk0RR2Q9oVhf60S2XR1BCWJ2g==}
dev: true
/@types/node/18.11.9:
@ -7128,7 +7130,7 @@ packages:
/@types/prompts/2.4.1:
resolution: {integrity: sha512-1Mqzhzi9W5KlooNE4o0JwSXGUDeQXKldbGn9NO4tpxwZbHXYd+WcKpCksG2lbhH7U9I9LigfsdVsP2QAY0lNPA==}
dependencies:
'@types/node': 18.11.10
'@types/node': 18.11.11
dev: true
/@types/prop-types/15.7.5:
@ -7200,7 +7202,7 @@ packages:
/@types/resolve/1.17.1:
resolution: {integrity: sha512-yy7HuzQhj0dhGpD8RLXSZWEkLsV9ibvxvi6EiJ3bkqLAO1RGo0WbkWQiwpRlSFymTJRz0d3k5LM3kkx8ArDbLw==}
dependencies:
'@types/node': 18.11.10
'@types/node': 18.11.11
dev: true
/@types/resolve/1.20.2:
@ -7217,7 +7219,7 @@ packages:
/@types/set-cookie-parser/2.4.2:
resolution: {integrity: sha512-fBZgytwhYAUkj/jC/FAV4RQ5EerRup1YQsXQCh8rZfiHkc4UahC192oH0smGwsXol3cL3A5oETuAHeQHmhXM4w==}
dependencies:
'@types/node': 18.11.10
'@types/node': 18.11.11
dev: true
/@types/sinonjs__fake-timers/8.1.1:
@ -7300,7 +7302,7 @@ packages:
/@types/webpack-sources/3.2.0:
resolution: {integrity: sha512-Ft7YH3lEVRQ6ls8k4Ff1oB4jN6oy/XmU6tQISKdhfh+1mR+viZFphS6WL0IrtDOzvefmJg5a0s7ZQoRXwqTEFg==}
dependencies:
'@types/node': 18.11.10
'@types/node': 18.11.11
'@types/source-list-map': 0.1.2
source-map: 0.7.4
dev: true
@ -7308,7 +7310,7 @@ packages:
/@types/webpack/4.41.32:
resolution: {integrity: sha512-cb+0ioil/7oz5//7tZUSwbrSAN/NWHrQylz5cW8G0dWTcF/g+/dSdMlKVZspBYuMAN1+WnwHrkxiRrLcwd0Heg==}
dependencies:
'@types/node': 18.11.10
'@types/node': 18.11.11
'@types/tapable': 1.0.8
'@types/uglify-js': 3.17.0
'@types/webpack-sources': 3.2.0
@ -7319,7 +7321,7 @@ packages:
/@types/ws/8.5.3:
resolution: {integrity: sha512-6YOoWjruKj1uLf3INHH7D3qTXwFfEsg1kf3c0uDdSBJwfa/llkwIjrAGV7j7mVgGNbzTQ3HiHKKDXl6bJPD97w==}
dependencies:
'@types/node': 18.11.10
'@types/node': 18.11.11
dev: true
/@types/yargs-parser/21.0.0:
@ -7342,7 +7344,7 @@ packages:
resolution: {integrity: sha512-Cn6WYCm0tXv8p6k+A8PvbDG763EDpBoTzHdA+Q/MF6H3sapGjCm9NzoaJncJS9tUKSuCoDs9XHxYYsQDgxR6kw==}
requiresBuild: true
dependencies:
'@types/node': 18.11.10
'@types/node': 18.11.11
dev: true
optional: true
@ -13584,13 +13586,12 @@ packages:
- encoding
dev: true
/happy-dom/7.7.2:
resolution: {integrity: sha512-xJhDLvS7jCie2sgU00HzyNFfdRSUOxm/ndE1gT++aNDo4ffXtn6/WI/Vf3IooDEC770AQ3J8fJvnakPZFEsLpg==}
/happy-dom/8.1.0:
resolution: {integrity: sha512-R9NVb5815gpMFsldZF0wVSHw2uSobx6yitSkCdQPda1kwfAmVw4Ut8ZspxGHXkK6OA93SznldkcvrbhKFr6JcA==}
dependencies:
css.escape: 1.5.1
he: 1.2.0
node-fetch: 2.6.7
sync-request: 6.1.0
webidl-conversions: 7.0.0
whatwg-encoding: 2.0.0
whatwg-mimetype: 3.0.0
@ -14732,7 +14733,7 @@ packages:
dependencies:
'@jest/types': 26.6.2
'@types/graceful-fs': 4.1.5
'@types/node': 18.11.10
'@types/node': 18.11.11
anymatch: 3.1.2
fb-watchman: 2.0.1
graceful-fs: 4.2.10
@ -14800,7 +14801,7 @@ packages:
resolution: {integrity: sha512-S5wqyz0DXnNJPd/xfIzZ5Xnp1HrJWBczg8mMfMpN78OJ5eDxXyf+Ygld9wX1DnUWbIbhM1YDY95NjR4CBXkb2g==}
engines: {node: '>= 10.14.2'}
dependencies:
'@types/node': 18.11.10
'@types/node': 18.11.11
graceful-fs: 4.2.10
dev: true
@ -14809,7 +14810,7 @@ packages:
engines: {node: '>= 10.14.2'}
dependencies:
'@jest/types': 26.6.2
'@types/node': 18.11.10
'@types/node': 18.11.11
chalk: 4.1.2
graceful-fs: 4.2.10
is-ci: 2.0.0
@ -14821,7 +14822,7 @@ packages:
engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0}
dependencies:
'@jest/types': 29.0.1
'@types/node': 18.11.10
'@types/node': 18.11.11
chalk: 4.1.2
ci-info: 3.5.0
graceful-fs: 4.2.10
@ -14832,7 +14833,7 @@ packages:
resolution: {integrity: sha512-KWYVV1c4i+jbMpaBC+U++4Va0cp8OisU185o73T1vo99hqi7w8tSJfUXYswwqqrjzwxa6KpRK54WhPvwf5w6PQ==}
engines: {node: '>= 10.13.0'}
dependencies:
'@types/node': 18.11.10
'@types/node': 18.11.11
merge-stream: 2.0.0
supports-color: 7.2.0
dev: true
@ -14841,7 +14842,7 @@ packages:
resolution: {integrity: sha512-7vuh85V5cdDofPyxn58nrPjBktZo0u9x1g8WtjQol+jZDaE+fhN+cIvTj11GndBnMnyfrUOG1sZQxCdjKh+DKg==}
engines: {node: '>= 10.13.0'}
dependencies:
'@types/node': 18.11.10
'@types/node': 18.11.11
merge-stream: 2.0.0
supports-color: 8.1.1
dev: true
@ -15933,7 +15934,7 @@ packages:
resolution: {integrity: sha512-LaJ8yuh4v0zEmge/g3c7jjFlhoCPfQn6RCjXgm9A0Qiuochq4BcuOxVfWmdnCoLTlg2MV+hqhOek+W2OhG0Lwg==}
dependencies:
acorn: 8.8.1
pathe: 0.3.8
pathe: 0.3.9
pkg-types: 0.3.6
ufo: 0.8.5
dev: true
@ -16906,10 +16907,6 @@ packages:
/pathe/0.2.0:
resolution: {integrity: sha512-sTitTPYnn23esFR3RlqYBWn4c45WGeLcsKzQiUpXJAyfcWkolvlYpV8FLo7JishK946oQwMFUCHXQ9AjGPKExw==}
/pathe/0.3.8:
resolution: {integrity: sha512-c71n61F1skhj/jzZe+fWE9XDoTYjWbUwIKVwFftZ5IOgiX44BVkTkD+/803YDgR50tqeO4eXWxLyVHBLWQAD1g==}
dev: true
/pathe/0.3.9:
resolution: {integrity: sha512-6Y6s0vT112P3jD8dGfuS6r+lpa0qqNrLyHPOwvXMnyNTQaYiwgau2DP3aNDsR13xqtGj7rrPo+jFUATpU6/s+g==}
dev: true
@ -20963,6 +20960,16 @@ packages:
- terser
dev: true
/vitest-sonar-reporter/0.3.3_vitest@packages+vitest:
resolution: {integrity: sha512-OvH+AYMgDFk3SK7sohn5KUHUOhB42tWgSTW7YzB/v/dN3x+7bgXD2L0FZpDYjC1hvBMJJ4HFqOsiO9LsETmgxg==}
engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0}
peerDependencies:
vitest: '>=0.18.0'
dependencies:
pathe: 0.3.9
vitest: link:packages/vitest
dev: true
/vm-browserify/1.1.2:
resolution: {integrity: sha512-2ham8XPWTONajOR0ohOKOHXkm3+gaBmGut3SRuu75xLd/RRaY6vqgh8NBYYk7+RW3u5AtzPQZG8F10LHkl0lAQ==}
dev: true

1
test/reporters/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
sonar-config.xml

View File

@ -3,6 +3,7 @@ import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
include: ['tests/reporters.spec.ts'],
reporters: ['pkg-reporter'],
reporters: ['pkg-reporter', 'vitest-sonar-reporter'],
outputFile: './sonar-config.xml',
},
})

View File

@ -7,6 +7,7 @@
"devDependencies": {
"execa": "^6.1.0",
"pkg-reporter": "./reportPkg/",
"vitest": "workspace:*"
"vitest": "workspace:*",
"vitest-sonar-reporter": "0.3.3"
}
}