| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,6 +9,7 @@ import { extractSourceMap } from './source-map' | |||
| 9 | 9 | import { | |
| 10 | 10 | cleanUrl, | |
| 11 | 11 | createImportMetaEnvProxy, | |
| 12 | + isBareImport, | ||
| 12 | 13 | isInternalRequest, | |
| 13 | 14 | isNodeBuiltin, | |
| 14 | 15 | isPrimitive, | |
@@ -325,6 +326,28 @@ export class ViteNodeRunner { | |||
| 325 | 326 | return await this.cachedRequest(id, fsPath, callstack) | |
| 326 | 327 | } | |
| 327 | 328 | ||
| 329 | + private async _fetchModule(id: string, importer?: string) { | ||
| 330 | + try { | ||
| 331 | + return await this.options.fetchModule(id) | ||
| 332 | + } | ||
| 333 | + catch (cause: any) { | ||
| 334 | + // rethrow vite error if it cannot load the module because it's not resolved | ||
| 335 | + if ( | ||
| 336 | + (typeof cause === 'object' && cause.code === 'ERR_LOAD_URL') | ||
| 337 | + || (typeof cause?.message === 'string' && cause.message.includes('Failed to load url')) | ||
| 338 | + ) { | ||
| 339 | + const error = new Error( | ||
| 340 | + `Cannot find ${isBareImport(id) ? 'package' : 'module'} '${id}'${importer ? ` imported from '${importer}'` : ''}`, | ||
| 341 | + { cause }, | ||
| 342 | + ) as Error & { code: string } | ||
| 343 | + error.code = 'ERR_MODULE_NOT_FOUND' | ||
| 344 | + throw error | ||
| 345 | + } | ||
| 346 | + | ||
| 347 | + throw cause | ||
| 348 | + } | ||
| 349 | + } | ||
| 350 | + | ||
| 328 | 351 | /** @internal */ | |
| 329 | 352 | async directRequest(id: string, fsPath: string, _callstack: string[]) { | |
| 330 | 353 | const moduleId = normalizeModuleId(fsPath) | |
@@ -345,7 +368,10 @@ export class ViteNodeRunner { | |||
| 345 | 368 | if (id in requestStubs) { | |
| 346 | 369 | return requestStubs[id] | |
| 347 | 370 | } | |
| 348 | - let { code: transformed, externalize } = await this.options.fetchModule(id) | ||
| 371 | + let { code: transformed, externalize } = await this._fetchModule( | ||
| 372 | + id, | ||
| 373 | + callstack[callstack.length - 2], | ||
| 374 | + ) | ||
| 349 | 375 | ||
| 350 | 376 | if (externalize) { | |
| 351 | 377 | debugNative(externalize) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -21,6 +21,12 @@ export function slash(str: string): string { | |||
| 21 | 21 | return str.replace(/\\/g, '/') | |
| 22 | 22 | } | |
| 23 | 23 | ||
| 24 | + const bareImportRE = /^(?![a-z]:)[\w@](?!.*:\/\/)/i | ||
| 25 | + | ||
| 26 | + export function isBareImport(id: string): boolean { | ||
| 27 | + return bareImportRE.test(id) | ||
| 28 | + } | ||
| 29 | + | ||
| 24 | 30 | export const VALID_ID_PREFIX = '/@id/' | |
| 25 | 31 | ||
| 26 | 32 | export function normalizeRequestId(id: string, base?: string): string { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,7 +6,7 @@ import fs from 'node:fs' | |||
| 6 | 6 | import { dirname } from 'node:path' | |
| 7 | 7 | import { fileURLToPath, pathToFileURL } from 'node:url' | |
| 8 | 8 | import { extname, join, normalize } from 'pathe' | |
| 9 | - import { getCachedData, isNodeBuiltin, setCacheData } from 'vite-node/utils' | ||
| 9 | + import { getCachedData, isBareImport, isNodeBuiltin, setCacheData } from 'vite-node/utils' | ||
| 10 | 10 | import { CommonjsExecutor } from './vm/commonjs-executor' | |
| 11 | 11 | import { EsmExecutor } from './vm/esm-executor' | |
| 12 | 12 | import { ViteExecutor } from './vm/vite-executor' | |
@@ -209,7 +209,7 @@ export class ExternalModulesExecutor { | |||
| 209 | 209 | (type === 'module' || type === 'commonjs' || type === 'wasm') | |
| 210 | 210 | && !existsSync(path) | |
| 211 | 211 | ) { | |
| 212 | - const error = new Error(`Cannot find module '${path}'`); | ||
| 212 | + const error = new Error(`Cannot find ${isBareImport(path) ? 'package' : 'module'} '${path}'`); | ||
| 213 | 213 | (error as any).code = 'ERR_MODULE_NOT_FOUND' | |
| 214 | 214 | throw error | |
| 215 | 215 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -63,13 +63,30 @@ export class ViteExecutor { | |||
| 63 | 63 | return cached | |
| 64 | 64 | } | |
| 65 | 65 | return this.esm.createEsModule(fileUrl, async () => { | |
| 66 | - const result = await this.options.transform(fileUrl, 'web') | ||
| 67 | - if (!result.code) { | ||
| 68 | - throw new Error( | ||
| 69 | - `[vitest] Failed to transform ${fileUrl}. Does the file exist?`, | ||
| 70 | - ) | ||
| 66 | + try { | ||
| 67 | + const result = await this.options.transform(fileUrl, 'web') | ||
| 68 | + if (result.code) { | ||
| 69 | + return result.code | ||
| 70 | + } | ||
| 71 | 71 | } | |
| 72 | - return result.code | ||
| 72 | + catch (cause: any) { | ||
| 73 | + // rethrow vite error if it cannot load the module because it's not resolved | ||
| 74 | + if ( | ||
| 75 | + (typeof cause === 'object' && cause.code === 'ERR_LOAD_URL') | ||
| 76 | + || (typeof cause?.message === 'string' && cause.message.includes('Failed to load url')) | ||
| 77 | + ) { | ||
| 78 | + const error = new Error( | ||
| 79 | + `Cannot find module '${fileUrl}'`, | ||
| 80 | + { cause }, | ||
| 81 | + ) as Error & { code: string } | ||
| 82 | + error.code = 'ERR_MODULE_NOT_FOUND' | ||
| 83 | + throw error | ||
| 84 | + } | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | + throw new Error( | ||
| 88 | + `[vitest] Failed to transform ${fileUrl}. Does the file exist?`, | ||
| 89 | + ) | ||
| 73 | 90 | }) | |
| 74 | 91 | } | |
| 75 | 92 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -22,7 +22,7 @@ it('package', async () => { | |||
| 22 | 22 | it('builtin', async () => { | |
| 23 | 23 | await expect(() => notFound.importBuiltin()).rejects.toMatchObject({ | |
| 24 | 24 | code: 'ERR_MODULE_NOT_FOUND', | |
| 25 | - message: 'Cannot find module \'node:non-existing-builtin\'', | ||
| 25 | + message: 'Cannot find package \'node:non-existing-builtin\'', | ||
| 26 | 26 | }) | |
| 27 | 27 | }) | |
| 28 | 28 | ||
@@ -31,6 +31,6 @@ it('builtin', async () => { | |||
| 31 | 31 | it('namespace', async () => { | |
| 32 | 32 | await expect(() => notFound.importNamespace()).rejects.toMatchObject({ | |
| 33 | 33 | code: 'ERR_MODULE_NOT_FOUND', | |
| 34 | - message: 'Cannot find module \'non-existing-namespace:xyz\'', | ||
| 34 | + message: 'Cannot find package \'non-existing-namespace:xyz\'', | ||
| 35 | 35 | }) | |
| 36 | 36 | }) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,14 @@ | |||
| 1 | + import { expect, test } from 'vitest' | ||
| 2 | + | ||
| 3 | + test('dynamic import', async () => { | ||
| 4 | + try { | ||
| 5 | + await import('non-existing-module' as any) | ||
| 6 | + expect.unreachable() | ||
| 7 | + } | ||
| 8 | + catch (err: any) { | ||
| 9 | + expect(err.message).toBe( | ||
| 10 | + `Cannot find package 'non-existing-module' imported from '${import.meta.filename}'`, | ||
| 11 | + ) | ||
| 12 | + expect(err.code).toBe('ERR_MODULE_NOT_FOUND') | ||
| 13 | + } | ||
| 14 | + }) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -82,9 +82,9 @@ test('dynamic import has null prototype', async () => { | |||
| 82 | 82 | test('dynamic import throws an error', async () => { | |
| 83 | 83 | const path = './some-unknown-path' | |
| 84 | 84 | const imported = import(path) | |
| 85 | - await expect(imported).rejects.toThrowError(/Failed to load url \.\/some-unknown-path/) | ||
| 85 | + await expect(imported).rejects.toThrowError(/Cannot find module '\.\/some-unknown-path' imported/) | ||
| 86 | 86 | // @ts-expect-error path does not exist | |
| 87 | - await expect(() => import('./some-unknown-path')).rejects.toThrowError(/Failed to load/) | ||
| 87 | + await expect(() => import('./some-unknown-path')).rejects.toThrowError(/Cannot find module/) | ||
| 88 | 88 | }) | |
| 89 | 89 | ||
| 90 | 90 | test('can import @vite/client', async () => { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -18,7 +18,7 @@ it('worker with invalid url throws an error', async () => { | |||
| 18 | 18 | if (!import.meta.env.VITEST_VM_POOL) { | |
| 19 | 19 | expect(event.error).toBeInstanceOf(Error) | |
| 20 | 20 | } | |
| 21 | - expect(event.error.message).toContain('Failed to load') | ||
| 21 | + expect(event.error.message).toContain('Cannot find module') | ||
| 22 | 22 | }) | |
| 23 | 23 | ||
| 24 | 24 | it('throws an error on invalid path', async () => { | |
@@ -34,7 +34,7 @@ it('throws an error on invalid path', async () => { | |||
| 34 | 34 | if (!import.meta.env.VITEST_VM_POOL) { | |
| 35 | 35 | expect(event.error).toBeInstanceOf(Error) | |
| 36 | 36 | } | |
| 37 | - expect(event.error.message).toContain('Failed to load') | ||
| 37 | + expect(event.error.message).toContain('Cannot find module') | ||
| 38 | 38 | }) | |
| 39 | 39 | ||
| 40 | 40 | it('returns globals on self correctly', async () => { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments