diff --git a/packages/vite/src/node/plugins/html.ts b/packages/vite/src/node/plugins/html.ts index 7d971375040b5c..d1b0e932df3167 100644 --- a/packages/vite/src/node/plugins/html.ts +++ b/packages/vite/src/node/plugins/html.ts @@ -774,11 +774,8 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { return tags } - for (const [id, html] of processedHtml) { - const relativeUrlPath = path.posix.relative( - config.root, - normalizePath(id), - ) + for (const [normalizedId, html] of processedHtml) { + const relativeUrlPath = path.posix.relative(config.root, normalizedId) const assetsBase = getBaseInHTML(relativeUrlPath, config) const toOutputFilePath = ( filename: string, @@ -804,7 +801,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { const toOutputPublicAssetFilePath = (filename: string) => toOutputFilePath(filename, 'public') - const isAsync = isAsyncScriptMap.get(config)!.get(id)! + const isAsync = isAsyncScriptMap.get(config)!.get(normalizedId)! let result = html @@ -813,7 +810,8 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { (chunk) => chunk.type === 'chunk' && chunk.isEntry && - chunk.facadeModuleId === id, + chunk.facadeModuleId && + normalizePath(chunk.facadeModuleId) === normalizedId, ) as OutputChunk | undefined let canInlineEntry = false @@ -898,7 +896,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { [...normalHooks, ...postHooks], { path: '/' + relativeUrlPath, - filename: id, + filename: normalizedId, bundle, chunk, }, @@ -928,7 +926,9 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { inlineEntryChunk.add(chunk.fileName) } - const shortEmitName = normalizePath(path.relative(config.root, id)) + const shortEmitName = normalizePath( + path.relative(config.root, normalizedId), + ) this.emitFile({ type: 'asset', fileName: shortEmitName, diff --git a/playground/html/__tests__/html.spec.ts b/playground/html/__tests__/html.spec.ts index ed06f730308a4d..687629d91055b5 100644 --- a/playground/html/__tests__/html.spec.ts +++ b/playground/html/__tests__/html.spec.ts @@ -3,6 +3,7 @@ import { hasWindowsUnicodeFsBug } from '../../hasWindowsUnicodeFsBug' import { browserLogs, editFile, + expectWithRetry, getColor, isBuild, isServe, @@ -375,6 +376,16 @@ describe('special character', () => { }) }) +describe('relative input', () => { + beforeAll(async () => { + await page.goto(viteTestUrl + '/relative-input.html') + }) + + test('passing relative path to rollupOptions.input works', async () => { + await expectWithRetry(() => page.textContent('.relative-input')).toBe('OK') + }) +}) + describe.runIf(isServe)('warmup', () => { test('should warmup /warmup/warm.js', async () => { // warmup transform files async during server startup, so the module check diff --git a/playground/html/relative-input.html b/playground/html/relative-input.html new file mode 100644 index 00000000000000..b18dcea836a1d4 --- /dev/null +++ b/playground/html/relative-input.html @@ -0,0 +1,3 @@ + + +

diff --git a/playground/html/relative-input/main.js b/playground/html/relative-input/main.js new file mode 100644 index 00000000000000..b60f93e96854ee --- /dev/null +++ b/playground/html/relative-input/main.js @@ -0,0 +1 @@ +document.querySelector('.relative-input').textContent = 'OK' diff --git a/playground/html/vite.config.js b/playground/html/vite.config.js index 003a591def4c1d..4452200a08aeeb 100644 --- a/playground/html/vite.config.js +++ b/playground/html/vite.config.js @@ -1,4 +1,4 @@ -import { resolve } from 'node:path' +import { relative, resolve } from 'node:path' import { defineConfig } from 'vite' import { hasWindowsUnicodeFsBug } from '../hasWindowsUnicodeFsBug' @@ -40,6 +40,10 @@ export default defineConfig({ serveBothFile: resolve(__dirname, 'serve/both.html'), serveBothFolder: resolve(__dirname, 'serve/both/index.html'), write: resolve(__dirname, 'write.html'), + relativeInput: relative( + process.cwd(), + resolve(__dirname, 'relative-input.html'), + ), }, }, },