Skip to content

Commit

Permalink
fix: scripts and styles were missing from built HTML on Windows (#16421)
Browse files Browse the repository at this point in the history
Co-authored-by: sapphi-red <49056869+sapphi-red@users.noreply.github.com>
  • Loading branch information
rse and sapphi-red authored May 1, 2024
1 parent bb79c9b commit 0e93f58
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 10 deletions.
18 changes: 9 additions & 9 deletions packages/vite/src/node/plugins/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -898,7 +896,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
[...normalHooks, ...postHooks],
{
path: '/' + relativeUrlPath,
filename: id,
filename: normalizedId,
bundle,
chunk,
},
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions playground/html/__tests__/html.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { hasWindowsUnicodeFsBug } from '../../hasWindowsUnicodeFsBug'
import {
browserLogs,
editFile,
expectWithRetry,
getColor,
isBuild,
isServe,
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions playground/html/relative-input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<script type="module" src="./relative-input/main.js"></script>

<p class="relative-input"></p>
1 change: 1 addition & 0 deletions playground/html/relative-input/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
document.querySelector('.relative-input').textContent = 'OK'
6 changes: 5 additions & 1 deletion playground/html/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { resolve } from 'node:path'
import { relative, resolve } from 'node:path'
import { defineConfig } from 'vite'
import { hasWindowsUnicodeFsBug } from '../hasWindowsUnicodeFsBug'

Expand Down Expand Up @@ -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'),
),
},
},
},
Expand Down

0 comments on commit 0e93f58

Please sign in to comment.