Skip to content

Commit f6ae60d

Browse files
authored
fix: inline css hash (#7974)
1 parent a5bdb9f commit f6ae60d

File tree

5 files changed

+15
-11
lines changed

5 files changed

+15
-11
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>inline a</p>

packages/playground/html/vite.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ module.exports = {
1717
zeroJS: resolve(__dirname, 'zeroJS.html'),
1818
noHead: resolve(__dirname, 'noHead.html'),
1919
noBody: resolve(__dirname, 'noBody.html'),
20+
inlinea: resolve(__dirname, 'inline/shared_a.html'),
2021
inline1: resolve(__dirname, 'inline/shared-1.html'),
2122
inline2: resolve(__dirname, 'inline/shared-2.html'),
2223
inline3: resolve(__dirname, 'inline/unique.html'),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ async function fileToBuiltUrl(
337337
return url
338338
}
339339

340-
export function getAssetHash(content: Buffer): string {
340+
export function getAssetHash(content: Buffer | string): string {
341341
return createHash('sha256').update(content).digest('hex').slice(0, 8)
342342
}
343343

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ import {
3333
getAssetFilename,
3434
assetUrlRE,
3535
fileToUrl,
36-
checkPublicFile
36+
checkPublicFile,
37+
getAssetHash
3738
} from './asset'
3839
import MagicString from 'magic-string'
3940
import type * as PostCSS from 'postcss'
@@ -354,7 +355,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
354355
const query = parseRequest(id)
355356
if (inlineCSS && isHTMLProxy) {
356357
addToHTMLProxyTransformResult(
357-
`${cleanUrl(id)}_${Number.parseInt(query!.index)}`,
358+
`${getAssetHash(cleanUrl(id))}_${Number.parseInt(query!.index)}`,
358359
css
359360
)
360361
return `export default ''`

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import {
2323
checkPublicFile,
2424
assetUrlRE,
2525
urlToBuiltUrl,
26-
getAssetFilename
26+
getAssetFilename,
27+
getAssetHash
2728
} from './asset'
2829
import { isCSSRequest } from './css'
2930
import { modulePreloadPolyfillId } from './modulePreloadPolyfill'
@@ -44,7 +45,7 @@ interface ScriptAssetsUrl {
4445
}
4546

4647
const htmlProxyRE = /\?html-proxy=?[&inline\-css]*&index=(\d+)\.(js|css)$/
47-
const inlineCSSRE = /__VITE_INLINE_CSS__([^_]+_\d+)__/g
48+
const inlineCSSRE = /__VITE_INLINE_CSS__([a-z\d]{8}_\d+)__/g
4849
// Do not allow preceding '.', but do allow preceding '...' for spread operations
4950
const inlineImportRE =
5051
/(?<!(?<!\.\.)\.)\bimport\s*\(("([^"]|(?<=\\)")*"|'([^']|(?<=\\)')*')\)/g
@@ -62,8 +63,8 @@ export const htmlProxyMap = new WeakMap<
6263
>()
6364

6465
// HTML Proxy Transform result are stored by config
65-
// `${importer}_${query.index}` -> transformed css code
66-
// PS: key like `/vite/packages/playground/assets/index.html_1`
66+
// `${hash(importer)}_${query.index}` -> transformed css code
67+
// PS: key like `hash(/vite/packages/playground/assets/index.html)_1`)
6768
export const htmlProxyResult = new Map<string, string>()
6869

6970
export function htmlInlineProxyPlugin(config: ResolvedConfig): Plugin {
@@ -373,12 +374,12 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
373374
addToHTMLProxyCache(config, filePath, inlineModuleIndex, { code })
374375
// will transform with css plugin and cache result with css-post plugin
375376
js += `\nimport "${id}?html-proxy&inline-css&index=${inlineModuleIndex}.css"`
376-
377+
const hash = getAssetHash(cleanUrl(id))
377378
// will transform in `applyHtmlTransforms`
378379
s.overwrite(
379380
styleNode.loc.start.offset,
380381
styleNode.loc.end.offset,
381-
`"__VITE_INLINE_CSS__${cleanUrl(id)}_${inlineModuleIndex}__"`,
382+
`"__VITE_INLINE_CSS__${hash}_${inlineModuleIndex}__"`,
382383
{ contentOnly: true }
383384
)
384385
}
@@ -392,12 +393,12 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
392393
code: styleNode.content
393394
})
394395
js += `\nimport "${id}?html-proxy&inline-css&index=${inlineModuleIndex}.css"`
395-
396+
const hash = getAssetHash(cleanUrl(id))
396397
// will transform in `applyHtmlTransforms`
397398
s.overwrite(
398399
styleNode.loc.start.offset,
399400
styleNode.loc.end.offset,
400-
`__VITE_INLINE_CSS__${cleanUrl(id)}_${inlineModuleIndex}__`,
401+
`__VITE_INLINE_CSS__${hash}_${inlineModuleIndex}__`,
401402
{ contentOnly: true }
402403
)
403404
}

0 commit comments

Comments
 (0)