feat!: support file:// resolution (#18422)

This commit is contained in:
Hiroshi Ogawa 2024-10-28 21:49:57 +09:00 committed by GitHub
parent 1e5703bda1
commit 6a7e313754
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 122 additions and 0 deletions

View File

@ -0,0 +1 @@
export default 'ok'

View File

@ -0,0 +1,3 @@
{
"type": "module"
}

View File

@ -1,6 +1,9 @@
import { join } from 'node:path'
import { describe, expect, onTestFinished, test } from 'vitest'
import { createServer } from '../server'
import { createServerModuleRunner } from '../ssr/runtime/serverModuleRunner'
import type { InlineConfig } from '../config'
import { build } from '../build'
describe('import and resolveId', () => {
async function createTestServer() {
@ -50,3 +53,98 @@ describe('import and resolveId', () => {
])
})
})
describe('file url', () => {
const fileUrl = new URL('./fixtures/file-url/entry.js', import.meta.url)
function getConfig(): InlineConfig {
return {
configFile: false,
root: join(import.meta.dirname, 'fixtures/file-url'),
logLevel: 'error',
server: {
middlewareMode: true,
},
plugins: [
{
name: 'virtual-file-url',
resolveId(source) {
if (source.startsWith('virtual:test-dep/')) {
return '\0' + source
}
},
load(id) {
if (id === '\0virtual:test-dep/static') {
return `
import * as dep from ${JSON.stringify(fileUrl.href)};
export default dep;
`
}
if (id === '\0virtual:test-dep/non-static') {
return `
const dep = await import(/* @vite-ignore */ String(${JSON.stringify(fileUrl.href)}));
export default dep;
`
}
},
},
],
}
}
test('dev', async () => {
const server = await createServer(getConfig())
onTestFinished(() => server.close())
const runner = createServerModuleRunner(server.environments.ssr, {
hmr: {
logger: false,
},
sourcemapInterceptor: false,
})
const mod = await runner.import('/entry.js')
expect(mod.default).toEqual('ok')
const mod2 = await runner.import(fileUrl.href)
expect(mod2).toBe(mod)
const mod3 = await runner.import('virtual:test-dep/static')
expect(mod3.default).toBe(mod)
const mod4 = await runner.import('virtual:test-dep/non-static')
expect(mod4.default).toBe(mod)
})
test('build', async () => {
await build({
...getConfig(),
build: {
ssr: true,
outDir: 'dist/basic',
rollupOptions: {
input: { index: fileUrl.href },
},
},
})
const mod1 = await import(
join(import.meta.dirname, 'fixtures/file-url/dist/basic/index.js')
)
expect(mod1.default).toBe('ok')
await build({
...getConfig(),
build: {
ssr: true,
outDir: 'dist/virtual',
rollupOptions: {
input: { index: 'virtual:test-dep/static' },
},
},
})
const mod2 = await import(
join(import.meta.dirname, 'fixtures/file-url/dist/virtual/index.js')
)
expect(mod2.default.default).toBe('ok')
})
})

View File

@ -1,5 +1,6 @@
import fs from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import colors from 'picocolors'
import type { PartialResolvedId } from 'rollup'
import { exports, imports } from 'resolve.exports'
@ -364,6 +365,11 @@ export function resolvePlugin(
}
}
// file url as path
if (id.startsWith('file://')) {
id = fileURLToPath(id)
}
// drive relative fs paths (only windows)
if (isWindows && id[0] === '/') {
const basedir = importer ? path.dirname(importer) : process.cwd()

View File

@ -123,6 +123,10 @@ test('absolute path', async () => {
expect(await page.textContent('.absolute')).toMatch('[success]')
})
test('file url', async () => {
expect(await page.textContent('.file-url')).toMatch('[success]')
})
test('browser field', async () => {
expect(await page.textContent('.browser')).toMatch('[success]')
})

View File

@ -0,0 +1 @@
export default '[success] file-url'

View File

@ -122,6 +122,9 @@
<h2>Resolve absolute path</h2>
<p class="absolute">fail</p>
<h2>Resolve file url</h2>
<p class="file-url">fail</p>
<h2>Browser Field</h2>
<p class="browser">fail</p>

View File

@ -22,6 +22,10 @@ const generatedContentImports = [
specifier: normalizePath(path.resolve(__dirname, './absolute.js')),
elementQuery: '.absolute',
},
{
specifier: new URL('file-url.js', import.meta.url),
elementQuery: '.file-url',
},
]
export default defineConfig({

View File

@ -435,6 +435,8 @@ importers:
packages/vite/src/node/__tests__/fixtures/cjs-ssr-dep: {}
packages/vite/src/node/__tests__/fixtures/file-url: {}
packages/vite/src/node/__tests__/fixtures/test-dep-conditions: {}
packages/vite/src/node/__tests__/packages/module: {}