Skip to content

Commit

Permalink
fix: don't provide importer only for /@id/
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Dec 15, 2022
1 parent 21faa5b commit 7d85574
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
7 changes: 4 additions & 3 deletions packages/vite-node/src/client.ts
Expand Up @@ -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'

Expand Down Expand Up @@ -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)
Expand Down
9 changes: 8 additions & 1 deletion packages/vite-node/src/utils.ts
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions test/core/src/aliased-mod.ts
@@ -1 +1,7 @@
export const isAliased = true

export const getPaths = () => ({
__filename,
__dirname,
url: import.meta.url,
})
20 changes: 20 additions & 0 deletions 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', () => {
Expand Down
1 change: 1 addition & 0 deletions test/core/vitest.config.ts
Expand Up @@ -34,6 +34,7 @@ export default defineConfig({
resolve: {
alias: [
{ find: '@', replacement: resolve(__dirname, 'src') },
{ find: '$', replacement: 'src' },
],
},
test: {
Expand Down

0 comments on commit 7d85574

Please sign in to comment.