Skip to content

Commit

Permalink
refactor: change the logic to find CSS files referenced by the JS files
Browse files Browse the repository at this point in the history
Fix for the breaking change introduced by vitejs/vite#15161
  • Loading branch information
thetutlage committed Nov 28, 2023
1 parent b4f282c commit 8cf9538
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 24 deletions.
18 changes: 2 additions & 16 deletions src/backend/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,9 @@ export class Vite {
})

for (const css of chunk.css || []) {
const cssChunk = this.#chunkByFile(manifest, css)
tags.push({
path: cssChunk.file,
tag: this.#generateTag(cssChunk.file, { ...attributes, integrity: cssChunk.integrity }),
path: css,
tag: this.#generateTag(css),
})
}
}
Expand All @@ -208,19 +207,6 @@ export class Vite {
return chunk
}

/**
* Get a chunk from the manifest file for a given hashed file name
*/
#chunkByFile(manifest: Manifest, fileName: string) {
const chunk = Object.values(manifest).find((c) => c.file === fileName)

if (!chunk) {
throw new Error(`Cannot find "${fileName}" chunk in the manifest file`)
}

return chunk
}

/**
* Check if the given path is a CSS path
*/
Expand Down
10 changes: 2 additions & 8 deletions tests/backend/vite.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ test.group('Vite | manifest', () => {
)
})

test('add integrity attribute to script and style tags', async ({ assert, fs }) => {
test('add integrity attribute to entrypoint tags', async ({ assert, fs }) => {
const vite = new Vite(
defineConfig({
buildDirectory: join(fs.basePath, 'public/assets'),
Expand All @@ -517,11 +517,6 @@ test.group('Vite | manifest', () => {
await fs.create(
'public/assets/.vite/manifest.json',
JSON.stringify({
'app.css': {
file: 'app-12345.css',
src: 'app.css',
integrity: 'sha384-hNF0CSk1Cqwkjmpb374DXqtYJ/rDp5SqV6ttpKEnqyjT',
},
'test.js': {
file: 'test-12345.js',
src: 'test.js',
Expand All @@ -538,7 +533,6 @@ test.group('Vite | manifest', () => {
tag: 'link',
attributes: {
rel: 'stylesheet',
integrity: 'sha384-hNF0CSk1Cqwkjmpb374DXqtYJ/rDp5SqV6ttpKEnqyjT',
href: 'https://cdn.url.com/app-12345.css',
},
},
Expand All @@ -555,7 +549,7 @@ test.group('Vite | manifest', () => {
assert.deepEqual(
output.map((element) => String(element)),
[
'<link rel="stylesheet" integrity="sha384-hNF0CSk1Cqwkjmpb374DXqtYJ/rDp5SqV6ttpKEnqyjT" href="https://cdn.url.com/app-12345.css"/>',
'<link rel="stylesheet" href="https://cdn.url.com/app-12345.css"/>',
'<script type="module" integrity="sha384-hNF0CSk1Cqwkjmpb374DXqtYJ/rDp5SqV6ttpKEnqyjT/gDHGHuYsj3XzBcMke15" src="https://cdn.url.com/test-12345.js"></script>',
]
)
Expand Down

0 comments on commit 8cf9538

Please sign in to comment.