Skip to content

Commit 0cbb07b

Browse files
authoredJun 15, 2023
fix(vite-node): correctly resolve virtual modules (#3544)
1 parent 02196f9 commit 0cbb07b

File tree

17 files changed

+475
-47
lines changed

17 files changed

+475
-47
lines changed
 

‎examples/sveltekit/.gitignore

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.DS_Store
2+
node_modules
3+
/build
4+
/.svelte-kit
5+
/package
6+
.env
7+
.env.*
8+
!.env.example
9+
vite.config.js.timestamp-*
10+
vite.config.ts.timestamp-*

‎examples/sveltekit/package.json

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "vitest-0.32.0-sveltekit",
3+
"type": "module",
4+
"version": "0.0.1",
5+
"private": true,
6+
"scripts": {
7+
"dev": "vite dev",
8+
"build": "vite build",
9+
"preview": "vite preview",
10+
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
11+
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
12+
"test:unit": "vitest",
13+
"lint": "prettier --plugin-search-dir . --check .",
14+
"format": "prettier --plugin-search-dir . --write ."
15+
},
16+
"devDependencies": {
17+
"@sveltejs/adapter-auto": "^2.1.0",
18+
"@sveltejs/kit": "^1.20.2",
19+
"prettier": "^2.8.8",
20+
"prettier-plugin-svelte": "^2.10.1",
21+
"svelte": "^3.59.1",
22+
"svelte-check": "^3.4.3",
23+
"tslib": "^2.5.3",
24+
"typescript": "^5.1.3",
25+
"vite": "^4.3.9",
26+
"vitest": "latest"
27+
}
28+
}

‎examples/sveltekit/src/app.d.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// See https://kit.svelte.dev/docs/types#app
2+
// for information about these interfaces
3+
declare global {
4+
namespace App {
5+
// interface Error {}
6+
// interface Locals {}
7+
// interface PageData {}
8+
// interface Platform {}
9+
}
10+
}
11+
12+
export {};

‎examples/sveltekit/src/app.html

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<link rel="icon" href="%sveltekit.assets%/favicon.png" />
6+
<meta name="viewport" content="width=device-width" />
7+
%sveltekit.head%
8+
</head>
9+
<body data-sveltekit-preload-data="hover">
10+
<div style="display: contents">%sveltekit.body%</div>
11+
</body>
12+
</html>
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { add } from './add'
3+
4+
describe('sum test', () => {
5+
it('adds 1 + 2 to equal 3', () => {
6+
expect(add(1, 2)).toBe(3)
7+
})
8+
})

‎examples/sveltekit/src/lib/add.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { dev } from '$app/environment'
2+
3+
export function add(a: number, b: number) {
4+
if (dev)
5+
console.warn(`Adding ${a} and ${b}`)
6+
7+
return a + b
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<h1>Welcome to SvelteKit</h1>
2+
<p>Visit <a href="https://kit.svelte.dev">kit.svelte.dev</a> to read the documentation</p>

‎examples/sveltekit/static/favicon.png

1.53 KB
Loading

‎examples/sveltekit/svelte.config.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import adapter from '@sveltejs/adapter-auto'
2+
import { vitePreprocess } from '@sveltejs/kit/vite'
3+
4+
/** @type {import('@sveltejs/kit').Config} */
5+
const config = {
6+
// Consult https://kit.svelte.dev/docs/integrations#preprocessors
7+
// for more information about preprocessors
8+
preprocess: vitePreprocess(),
9+
10+
kit: {
11+
// adapter-auto only supports some environments, see https://kit.svelte.dev/docs/adapter-auto for a list.
12+
// If your environment is not supported or you settled on a specific environment, switch out the adapter.
13+
// See https://kit.svelte.dev/docs/adapters for more information about adapters.
14+
adapter: adapter(),
15+
},
16+
}
17+
18+
export default config

‎examples/sveltekit/tsconfig.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"extends": "./.svelte-kit/tsconfig.json",
3+
"compilerOptions": {
4+
"allowJs": true,
5+
"checkJs": true,
6+
"esModuleInterop": true,
7+
"forceConsistentCasingInFileNames": true,
8+
"resolveJsonModule": true,
9+
"skipLibCheck": true,
10+
"sourceMap": true,
11+
"strict": true
12+
}
13+
// Path aliases are handled by https://kit.svelte.dev/docs/configuration#alias
14+
//
15+
// If you want to overwrite includes/excludes, make sure to copy over the relevant includes/excludes
16+
// from the referenced tsconfig.json - TypeScript does not merge them in
17+
}

‎examples/sveltekit/vite.config.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { sveltekit } from '@sveltejs/kit/vite'
2+
import { defineConfig } from 'vitest/config'
3+
4+
export default defineConfig({
5+
plugins: [sveltekit()],
6+
})

‎packages/vite-node/src/client.ts

+20-18
Original file line numberDiff line numberDiff line change
@@ -234,26 +234,28 @@ export class ViteNodeRunner {
234234
private async _resolveUrl(id: string, importer?: string): Promise<[url: string, fsPath: string]> {
235235
// we don't pass down importee here, because otherwise Vite doesn't resolve it correctly
236236
// should be checked before normalization, because it removes this prefix
237+
// TODO: this is a hack, we should find a better way to handle this
237238
if (importer && id.startsWith(VALID_ID_PREFIX))
238239
importer = undefined
239-
id = normalizeRequestId(id, this.options.base)
240-
if (!this.shouldResolveId(id))
241-
return [id, id]
242-
const { path, exists } = toFilePath(id, this.root)
240+
const dep = normalizeRequestId(id, this.options.base)
241+
if (!this.shouldResolveId(dep))
242+
return [dep, dep]
243+
const { path, exists } = toFilePath(dep, this.root)
243244
if (!this.options.resolveId || exists)
244-
return [id, path]
245-
const resolved = await this.options.resolveId(id, importer)
246-
if (!resolved) {
247-
const error = new Error(
248-
`Cannot find module '${id}'${importer ? ` imported from '${importer}'` : ''}.`
249-
+ '\n\n- If you rely on tsconfig.json to resolve modules, please install "vite-tsconfig-paths" plugin to handle module resolution.'
250-
+ '\n - Make sure you don\'t have relative aliases in your Vitest config. Use absolute paths instead. Read more: https://vitest.dev/guide/common-errors',
251-
)
252-
Object.defineProperty(error, 'code', { value: 'ERR_MODULE_NOT_FOUND', enumerable: true })
253-
Object.defineProperty(error, Symbol.for('vitest.error.not_found.data'), { value: { id, importer }, enumerable: false })
254-
throw error
255-
}
256-
const resolvedId = normalizeRequestId(resolved.id, this.options.base)
245+
return [dep, path]
246+
const resolved = await this.options.resolveId(dep, importer)
247+
// TODO: we need to better handle module resolution when different urls point to the same module
248+
// if (!resolved) {
249+
// const error = new Error(
250+
// `Cannot find module '${id}'${importer ? ` imported from '${importer}'` : ''}.`
251+
// + '\n\n- If you rely on tsconfig.json\'s "paths" to resolve modules, please install "vite-tsconfig-paths" plugin to handle module resolution.'
252+
// + '\n- Make sure you don\'t have relative aliases in your Vitest config. Use absolute paths instead. Read more: https://vitest.dev/guide/common-errors',
253+
// )
254+
// Object.defineProperty(error, 'code', { value: 'ERR_MODULE_NOT_FOUND', enumerable: true })
255+
// Object.defineProperty(error, Symbol.for('vitest.error.not_found.data'), { value: { id: dep, importer }, enumerable: false })
256+
// throw error
257+
// }
258+
const resolvedId = resolved ? normalizeRequestId(resolved.id, this.options.base) : dep
257259
return [resolvedId, resolvedId]
258260
}
259261

@@ -282,7 +284,7 @@ export class ViteNodeRunner {
282284
const mod = this.moduleCache.getByModuleId(moduleId)
283285

284286
const request = async (dep: string) => {
285-
const [id, depFsPath] = await this.resolveUrl(`${dep}`, fsPath)
287+
const [id, depFsPath] = await this.resolveUrl(String(dep), fsPath)
286288
const depMod = this.moduleCache.getByModuleId(depFsPath)
287289
depMod.importers.add(moduleId)
288290
mod.imports.add(depFsPath)

0 commit comments

Comments
 (0)
Please sign in to comment.