Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(legacy): add test case to ensure correct csp hashes in readme.md #13384

Merged
merged 5 commits into from Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/plugin-legacy/README.md
Expand Up @@ -159,7 +159,7 @@ The legacy plugin requires inline scripts for [Safari 10.1 `nomodule` fix](https

- `sha256-MS6/3FCg4WjP9gwgaBGwLpRCY6fZBgwmhVCdrPrNf3E=`
- `sha256-tQjf8gvb2ROOMapIxFvFAYBeUJ0v1HCbOcSmDNXGtDo=`
- `sha256-p7PoC97FO+Lu90RNjGWxhbm13yALSR4xzV8vaDhaQBo=`
- `sha256-4y/gEB2/KIwZFTfNqwXJq4olzvmQ0S214m9jwKgNXoc=`
- `sha256-+5XkZFazzJo8n0iOP4ti/cLCMUudTf//Mzkb7xNPXIc=`

<!--
Expand Down
4 changes: 4 additions & 0 deletions playground/legacy/__tests__/legacy.spec.ts
Expand Up @@ -89,6 +89,10 @@ test('asset url', async () => {
)
})

test('correct CSP hashes in README.md', async () => {
await untilUpdated(() => page.textContent('#csp-hashes'), 'true', true)
})

describe.runIf(isBuild)('build', () => {
test('should generate correct manifest', async () => {
const manifest = readManifest()
Expand Down
1 change: 1 addition & 0 deletions playground/legacy/index.html
Expand Up @@ -11,4 +11,5 @@ <h1 id="app"></h1>
<p>## worker message:</p>
<div class="worker-message"></div>
<div id="asset-path"></div>
<div id="csp-hashes"></div>
<script type="module" src="./main.js"></script>
2 changes: 2 additions & 0 deletions playground/legacy/main.js
Expand Up @@ -72,6 +72,8 @@ document

text('#asset-path', viteSvgPath)

text('#csp-hashes', __CORRECT_CSP_HASHES_IN_README__)

function text(el, text) {
document.querySelector(el).textContent = text
}
Expand Down
20 changes: 19 additions & 1 deletion playground/legacy/vite.config.js
@@ -1,8 +1,21 @@
import fs from 'node:fs'
import path from 'node:path'
import legacy from '@vitejs/plugin-legacy'
import legacy, { cspHashes } from '@vitejs/plugin-legacy'
import { defineConfig } from 'vite'

function isCorrectCspHashesInReadme() {
const readme = fs
.readFileSync(
path.resolve(__dirname, 'node_modules/@vitejs/plugin-legacy/README.md'),
)
.toString()
const hashesInDoc = [...readme.matchAll(/`sha256-(.+)`/g)].map(
(match) => match[1],
)

return cspHashes.every((hash, index) => hash === hashesInDoc[index])
}

export default defineConfig({
base: './',
plugins: [
Expand All @@ -11,6 +24,11 @@ export default defineConfig({
modernPolyfills: true,
}),
],
define: {
__CORRECT_CSP_HASHES_IN_README__: JSON.stringify(
isCorrectCspHashesInReadme(),
),
},

build: {
cssCodeSplit: false,
Expand Down