Skip to content

Commit

Permalink
feat: add filename/dirname to import.meta
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Jan 18, 2024
1 parent 9a4a743 commit 2e0f89e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const name = 'basic'

export const url = import.meta.url
export const meta = import.meta
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { existsSync, readdirSync } from 'node:fs'
import { posix, win32 } from 'node:path'
import { describe, expect } from 'vitest'
import { isWindows } from '../utils'
import { createViteRuntimeTester } from './utils'

describe('vite-runtime initialization', async () => {
Expand Down Expand Up @@ -107,11 +109,34 @@ describe('vite-runtime initialization', async () => {
expect(mod.existsSync).toBe(existsSync)
})

it('correctly resolves module url', async ({ runtime }) => {
const mod = await runtime.executeUrl('/fixtures/basic')
it('correctly resolves module url', async ({ runtime, server }) => {
const { meta } =
await runtime.executeUrl<typeof import('./fixtures/basic')>(
'/fixtures/basic',
)
// so it isn't transformed by Vitest
const _URL = URL
const basicUrl = new _URL('./fixtures/basic.js', import.meta.url)
expect(mod.url).toBe(basicUrl.toString())
const basicUrl = new _URL('./fixtures/basic.js', import.meta.url).toString()
expect(meta.url).toBe(basicUrl)

const filename = meta.filename!
const dirname = meta.dirname!

if (isWindows) {
const cwd = process.cwd()
const drive = `${cwd[0].toUpperCase()}:\\`
const root = server.config.root.replace(/\\/g, '/')

expect(filename.startsWith(drive)).toBe(true)
expect(dirname.startsWith(drive)).toBe(true)

expect(filename).toBe(win32.join(root, '.\\fixtures\\basic.js'))
expect(dirname).toBe(win32.join(root, '.\\fixtures'))
} else {
const root = server.config.root

expect(posix.join(root, './fixtures/basic.js')).toBe(filename)
expect(posix.join(root, './fixtures')).toBe(dirname)
}
})
})
6 changes: 6 additions & 0 deletions packages/vite/src/node/ssr/runtime/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ import {
cleanUrl,
createImportMetaEnvProxy,
isPrimitive,
isWindows,
posixDirname,
posixPathToFileHref,
posixResolve,
toWindowsPath,
unwrapId,
} from './utils'
import {
Expand Down Expand Up @@ -244,7 +246,11 @@ export class ViteRuntime {
const modulePath = cleanUrl(file || moduleId)
// disambiguate the `<UNIT>:/` on windows: see nodejs/node#31710
const href = posixPathToFileHref(modulePath)
const filename = modulePath
const dirname = posixDirname(modulePath)
const meta: ViteRuntimeImportMeta = {
filename: isWindows ? toWindowsPath(filename) : filename,
dirname: isWindows ? toWindowsPath(dirname) : dirname,
url: href,
env: this.envProxy,
resolve(id, parent) {
Expand Down
4 changes: 4 additions & 0 deletions packages/vite/src/node/ssr/runtime/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ export function posixDirname(filepath: string): string {
return normalizedPath.substring(0, normalizedPath.lastIndexOf('/')) || '/'
}

export function toWindowsPath(path: string): string {
return path.replace(/\//g, '\\')
}

// inlined from pathe to support environments without access to node:path
function cwd(): string {
if (typeof process !== 'undefined' && typeof process.cwd === 'function') {
Expand Down

0 comments on commit 2e0f89e

Please sign in to comment.