Skip to content

Commit 0e93f58

Browse files
rsesapphi-red
andauthored
fix: scripts and styles were missing from built HTML on Windows (#16421)
Co-authored-by: sapphi-red <49056869+sapphi-red@users.noreply.github.com>
1 parent bb79c9b commit 0e93f58

File tree

5 files changed

+29
-10
lines changed

5 files changed

+29
-10
lines changed

packages/vite/src/node/plugins/html.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -774,11 +774,8 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
774774
return tags
775775
}
776776

777-
for (const [id, html] of processedHtml) {
778-
const relativeUrlPath = path.posix.relative(
779-
config.root,
780-
normalizePath(id),
781-
)
777+
for (const [normalizedId, html] of processedHtml) {
778+
const relativeUrlPath = path.posix.relative(config.root, normalizedId)
782779
const assetsBase = getBaseInHTML(relativeUrlPath, config)
783780
const toOutputFilePath = (
784781
filename: string,
@@ -804,7 +801,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
804801
const toOutputPublicAssetFilePath = (filename: string) =>
805802
toOutputFilePath(filename, 'public')
806803

807-
const isAsync = isAsyncScriptMap.get(config)!.get(id)!
804+
const isAsync = isAsyncScriptMap.get(config)!.get(normalizedId)!
808805

809806
let result = html
810807

@@ -813,7 +810,8 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
813810
(chunk) =>
814811
chunk.type === 'chunk' &&
815812
chunk.isEntry &&
816-
chunk.facadeModuleId === id,
813+
chunk.facadeModuleId &&
814+
normalizePath(chunk.facadeModuleId) === normalizedId,
817815
) as OutputChunk | undefined
818816

819817
let canInlineEntry = false
@@ -898,7 +896,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
898896
[...normalHooks, ...postHooks],
899897
{
900898
path: '/' + relativeUrlPath,
901-
filename: id,
899+
filename: normalizedId,
902900
bundle,
903901
chunk,
904902
},
@@ -928,7 +926,9 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
928926
inlineEntryChunk.add(chunk.fileName)
929927
}
930928

931-
const shortEmitName = normalizePath(path.relative(config.root, id))
929+
const shortEmitName = normalizePath(
930+
path.relative(config.root, normalizedId),
931+
)
932932
this.emitFile({
933933
type: 'asset',
934934
fileName: shortEmitName,

playground/html/__tests__/html.spec.ts

+11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { hasWindowsUnicodeFsBug } from '../../hasWindowsUnicodeFsBug'
33
import {
44
browserLogs,
55
editFile,
6+
expectWithRetry,
67
getColor,
78
isBuild,
89
isServe,
@@ -375,6 +376,16 @@ describe('special character', () => {
375376
})
376377
})
377378

379+
describe('relative input', () => {
380+
beforeAll(async () => {
381+
await page.goto(viteTestUrl + '/relative-input.html')
382+
})
383+
384+
test('passing relative path to rollupOptions.input works', async () => {
385+
await expectWithRetry(() => page.textContent('.relative-input')).toBe('OK')
386+
})
387+
})
388+
378389
describe.runIf(isServe)('warmup', () => {
379390
test('should warmup /warmup/warm.js', async () => {
380391
// warmup transform files async during server startup, so the module check

playground/html/relative-input.html

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<script type="module" src="./relative-input/main.js"></script>
2+
3+
<p class="relative-input"></p>
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
document.querySelector('.relative-input').textContent = 'OK'

playground/html/vite.config.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { resolve } from 'node:path'
1+
import { relative, resolve } from 'node:path'
22
import { defineConfig } from 'vite'
33
import { hasWindowsUnicodeFsBug } from '../hasWindowsUnicodeFsBug'
44

@@ -40,6 +40,10 @@ export default defineConfig({
4040
serveBothFile: resolve(__dirname, 'serve/both.html'),
4141
serveBothFolder: resolve(__dirname, 'serve/both/index.html'),
4242
write: resolve(__dirname, 'write.html'),
43+
relativeInput: relative(
44+
process.cwd(),
45+
resolve(__dirname, 'relative-input.html'),
46+
),
4347
},
4448
},
4549
},

0 commit comments

Comments
 (0)