From 7d8557488e41cf73bbb8609c7ab352a37a4fe567 Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Thu, 15 Dec 2022 15:59:51 +0100 Subject: [PATCH] fix: don't provide importer only for /@id/ --- packages/vite-node/src/client.ts | 7 ++++--- packages/vite-node/src/utils.ts | 9 ++++++++- test/core/src/aliased-mod.ts | 6 ++++++ test/core/test/file-path.test.ts | 20 ++++++++++++++++++++ test/core/vitest.config.ts | 1 + 5 files changed, 39 insertions(+), 4 deletions(-) diff --git a/packages/vite-node/src/client.ts b/packages/vite-node/src/client.ts index 3ecb8ba5012b..f2bb2361d68d 100644 --- a/packages/vite-node/src/client.ts +++ b/packages/vite-node/src/client.ts @@ -7,7 +7,7 @@ import vm from 'vm' import { extname, isAbsolute, resolve } from 'pathe' import { isNodeBuiltin } from 'mlly' import createDebug from 'debug' -import { cleanUrl, isInternalRequest, isPrimitive, normalizeModuleId, normalizeRequestId, slash, toFilePath } from './utils' +import { VALID_ID_PREFIX, cleanUrl, isInternalRequest, isPrimitive, normalizeModuleId, normalizeRequestId, slash, toFilePath, unwrapId } from './utils' import type { HotContext, ModuleCache, ViteNodeRunnerOptions } from './types' import { extractSourceMap } from './source-map' @@ -197,9 +197,10 @@ export class ViteNodeRunner { return url url = normalizeRequestId(url, this.options.base) if (!this.options.resolveId) - return toFilePath(url, this.root) - if (importee && url[0] !== '.') + return toFilePath(unwrapId(url), this.root) + if (importee && url.startsWith(VALID_ID_PREFIX)) importee = undefined + url = unwrapId(url) const resolved = await this.options.resolveId(url, importee) const resolvedId = resolved?.id || url return normalizeRequestId(resolvedId, this.options.base) diff --git a/packages/vite-node/src/utils.ts b/packages/vite-node/src/utils.ts index e02cd7822ace..551392a76b46 100644 --- a/packages/vite-node/src/utils.ts +++ b/packages/vite-node/src/utils.ts @@ -13,13 +13,20 @@ export function mergeSlashes(str: string) { return str.replace(/\/\//g, '/') } +export const VALID_ID_PREFIX = '/@id/' + +export function unwrapId(id: string) { + if (id.startsWith(VALID_ID_PREFIX)) + return id.slice(5) + return id +} + export function normalizeRequestId(id: string, base?: string): string { if (base && id.startsWith(base)) id = `/${id.slice(base.length)}` return id .replace(/^\/@id\/__x00__/, '\0') // virtual modules start with `\0` - .replace(/^\/@id\//, '') .replace(/^__vite-browser-external:/, '') .replace(/^(node|file):/, '') .replace(/^\/+/, '/') // remove duplicate leading slashes diff --git a/test/core/src/aliased-mod.ts b/test/core/src/aliased-mod.ts index 2f33bf831eb7..e1b171255c0c 100644 --- a/test/core/src/aliased-mod.ts +++ b/test/core/src/aliased-mod.ts @@ -1 +1,7 @@ export const isAliased = true + +export const getPaths = () => ({ + __filename, + __dirname, + url: import.meta.url, +}) diff --git a/test/core/test/file-path.test.ts b/test/core/test/file-path.test.ts index c7343a657c05..44ba815d8503 100644 --- a/test/core/test/file-path.test.ts +++ b/test/core/test/file-path.test.ts @@ -1,9 +1,29 @@ import { existsSync } from 'fs' import { describe, expect, it, vi } from 'vitest' import { isWindows, slash, toFilePath } from '../../../packages/vite-node/src/utils' +// @ts-expect-error aliased to ../src/aliased-mod.ts +import { getPaths as getAbsoluteAliasedPaths } from '$/aliased-mod' +// @ts-expect-error aliased to ../src/aliased-mod.ts +import { getPaths as getRelativeAliasedPath } from '@/aliased-mod' vi.mock('fs') +describe('test aliased paths', () => { + it('expect functions to be part of the same module', () => { + expect(getAbsoluteAliasedPaths).toBe(getRelativeAliasedPath) + }) + + it.each([ + { getPaths: getAbsoluteAliasedPaths, type: 'doesn\'t have dir in alias' }, + { getPaths: getRelativeAliasedPath, type: 'has dir in alias' }, + ])('when alias $type', ({ getPaths }) => { + const paths = getPaths() + expect(paths.url).toMatch(/\/aliased-mod.ts$/) + expect(paths.__filename).toMatch(/\/aliased-mod.ts$/) + expect(paths.__dirname).toMatch(/\/core\/src$/) + }) +}) + describe('current url', () => { describe.runIf(!isWindows)('unix', () => { it('__filename', () => { diff --git a/test/core/vitest.config.ts b/test/core/vitest.config.ts index 1b28a90cef9a..b5cc98d3ddb4 100644 --- a/test/core/vitest.config.ts +++ b/test/core/vitest.config.ts @@ -34,6 +34,7 @@ export default defineConfig({ resolve: { alias: [ { find: '@', replacement: resolve(__dirname, 'src') }, + { find: '$', replacement: 'src' }, ], }, test: {