From 0e3148124fc6d3056514618ce354ad2609d41608 Mon Sep 17 00:00:00 2001 From: "Ralf S. Engelschall" Date: Sun, 14 Apr 2024 00:22:12 +0200 Subject: [PATCH 1/3] fix HTML entry file generation under Windows where different path separation characters exists than on POSIX filesystems (this fixes https://github.com/vitejs/vite/issues/15992) --- packages/vite/src/node/plugins/html.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/vite/src/node/plugins/html.ts b/packages/vite/src/node/plugins/html.ts index 232f9cc0e037a3..bfe5c9f21feae7 100644 --- a/packages/vite/src/node/plugins/html.ts +++ b/packages/vite/src/node/plugins/html.ts @@ -775,9 +775,10 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { } for (const [id, html] of processedHtml) { + const normalizedId = normalizePath(id) const relativeUrlPath = path.posix.relative( config.root, - normalizePath(id), + normalizedId, ) const assetsBase = getBaseInHTML(relativeUrlPath, config) const toOutputFilePath = ( @@ -813,7 +814,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { (chunk) => chunk.type === 'chunk' && chunk.isEntry && - chunk.facadeModuleId === id, + chunk.facadeModuleId === normalizedId, ) as OutputChunk | undefined let canInlineEntry = false From 5fc290cd3e34c0bf1f5a8855d73ff26539b7eada Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Tue, 30 Apr 2024 17:43:32 +0900 Subject: [PATCH 2/3] test: add test --- playground/html/__tests__/html.spec.ts | 11 +++++++++++ playground/html/relative-input.html | 3 +++ playground/html/relative-input/main.js | 1 + playground/html/vite.config.js | 6 +++++- 4 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 playground/html/relative-input.html create mode 100644 playground/html/relative-input/main.js 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'), + ), }, }, }, From 164f355e3e3a8a9c0e689bd2d9d1cb6a5217ad99 Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Tue, 30 Apr 2024 17:46:07 +0900 Subject: [PATCH 3/3] fix: correctly --- packages/vite/src/node/plugins/html.ts | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/packages/vite/src/node/plugins/html.ts b/packages/vite/src/node/plugins/html.ts index bfe5c9f21feae7..6dd53aa22d6898 100644 --- a/packages/vite/src/node/plugins/html.ts +++ b/packages/vite/src/node/plugins/html.ts @@ -774,12 +774,8 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { return tags } - for (const [id, html] of processedHtml) { - const normalizedId = normalizePath(id) - const relativeUrlPath = path.posix.relative( - config.root, - normalizedId, - ) + for (const [normalizedId, html] of processedHtml) { + const relativeUrlPath = path.posix.relative(config.root, normalizedId) const assetsBase = getBaseInHTML(relativeUrlPath, config) const toOutputFilePath = ( filename: string, @@ -805,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 @@ -814,7 +810,8 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { (chunk) => chunk.type === 'chunk' && chunk.isEntry && - chunk.facadeModuleId === normalizedId, + chunk.facadeModuleId && + normalizePath(chunk.facadeModuleId) === normalizedId, ) as OutputChunk | undefined let canInlineEntry = false @@ -899,7 +896,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { [...normalHooks, ...postHooks], { path: '/' + relativeUrlPath, - filename: id, + filename: normalizedId, bundle, chunk, }, @@ -929,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,