forked from vitejs/vite
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c81f3da
commit 452f8a8
Showing
12 changed files
with
212 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.direct { | ||
color: blue; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.from-js { | ||
color: blue; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<script type="module" src="./index.js"></script> | ||
<link rel="stylesheet" href="./direct.css" /> | ||
<p class="direct">direct</p> | ||
<p class="from-js">from-js</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import './from-js.css' | ||
console.log('foo') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "@vitejs/test-assets", | ||
"private": true, | ||
"version": "0.0.0", | ||
"type": "module", | ||
"scripts": { | ||
"debug": "node --inspect-brk ../../packages/vite/bin/vite", | ||
"dev": "vite", | ||
"build": "vite build", | ||
"preview": "vite preview" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import fs from 'node:fs/promises' | ||
import url from 'node:url' | ||
import path from 'node:path' | ||
import crypto from 'node:crypto' | ||
import { defineConfig } from 'vite' | ||
|
||
const __dirname = path.dirname(url.fileURLToPath(import.meta.url)) | ||
|
||
const createNonce = () => crypto.randomUUID().replaceAll('-', '') | ||
|
||
/** | ||
* @param {import('node:http').ServerResponse} res | ||
* @param {string} nonce | ||
*/ | ||
const setNonceHeader = (res, nonce) => { | ||
res.setHeader( | ||
'Content-Security-Policy', | ||
`default-src 'nonce-${nonce}'; connect-src 'self'`, | ||
) | ||
} | ||
|
||
/** | ||
* @param {string} htmlFile | ||
* @param {string} nonce | ||
*/ | ||
const getNonceInjectedHtml = async (htmlFile, nonce) => { | ||
const content = await fs.readFile(htmlFile, 'utf8') | ||
const tranformedContent = content | ||
.replace(/<script\s*/g, `$&nonce="${nonce}" `) | ||
.replace(/<link\s*/g, `$&nonce="${nonce}" `) | ||
return tranformedContent | ||
} | ||
|
||
export default defineConfig({ | ||
plugins: [ | ||
{ | ||
name: 'nonce-inject', | ||
config() { | ||
return { appType: 'custom' } | ||
}, | ||
configureServer({ transformIndexHtml, middlewares }) { | ||
return () => { | ||
middlewares.use(async (req, res) => { | ||
const nonce = createNonce() | ||
setNonceHeader(res, nonce) | ||
const content = await getNonceInjectedHtml( | ||
path.join(__dirname, './index.html'), | ||
nonce, | ||
) | ||
res.end(await transformIndexHtml(req.originalUrl, content)) | ||
}) | ||
} | ||
}, | ||
configurePreviewServer({ middlewares }) { | ||
middlewares.use(async (req, res, next) => { | ||
const { pathname } = new URL(req.url, 'http://example.com') | ||
const assetPath = path.join(__dirname, 'dist', `.${pathname}`) | ||
try { | ||
if ((await fs.stat(assetPath)).isFile()) { | ||
next() | ||
return | ||
} | ||
} catch {} | ||
|
||
const nonce = createNonce() | ||
setNonceHeader(res, nonce) | ||
const content = await getNonceInjectedHtml( | ||
path.join(__dirname, './dist/index.html'), | ||
nonce, | ||
) | ||
res.end(content) | ||
}) | ||
}, | ||
}, | ||
], | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.