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

fix: just return content with devFlag + html content #164

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions playground/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vite App</title>
<link rel="icon" href="/public/icon.png" />
</head>
<body>
<div id="app"></div>
<script type="module" src="/main.js"></script>
</body>
</html>
3 changes: 3 additions & 0 deletions playground/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const config: UserConfig = {
alias: {
alias: '/aliased'
},
entry: {
test: './test.html'
},
jsx: 'preact',
minify: false,
serviceWorker: !!process.env.USE_SW,
Expand Down
5 changes: 5 additions & 0 deletions src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ export interface SharedConfig {
* @default process.cwd()
*/
root?: string
/**
* todo work for build
* Project entry. The `index.html` is default included.
* */
entry?: Record<string, string>
/**
* Import alias. Can only be exact mapping, does not support wildcard syntax.
*/
Expand Down
61 changes: 34 additions & 27 deletions src/node/server/serverPluginModuleRewrite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,33 +58,37 @@ export const moduleRewritePlugin: ServerPlugin = ({
const scriptRE = /(<script\b[^>]*>)([\s\S]*?)<\/script>/gm
const srcRE = /\bsrc=(?:"([^"]+)"|'([^']+)'|([^'"\s]+)\b)/

async function rewriteIndex(html: string) {
async function rewriteIndex(html: string, importer: string = '/index.html') {
await initLexer
let hasInjectedDevFlag = false
const importer = '/index.html'
return html!.replace(scriptRE, (matched, openTag, script) => {
const devFlag = hasInjectedDevFlag ? `` : devInjectionCode
hasInjectedDevFlag = true
if (script) {
return `${devFlag}${openTag}${rewriteImports(
root,
script,
importer,
resolver
)}</script>`
} else {
const srcAttr = openTag.match(srcRE)
if (srcAttr) {
// register script as a import dep for hmr
const importee = cleanUrl(
slash(path.resolve('/', srcAttr[1] || srcAttr[2]))
)
debugHmr(` ${importer} imports ${importee}`)
ensureMapEntry(importerMap, importee).add(importer)
const devFlag = hasInjectedDevFlag ? `` : devInjectionCode
return (
devFlag +
html!.replace(scriptRE, (matched, openTag, script) => {
debugHmr(matched, openTag, script)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For unmatched script tag.


hasInjectedDevFlag = true
if (script) {
return `${openTag}${rewriteImports(
root,
script,
importer,
resolver
)}</script>`
} else {
const srcAttr = openTag.match(srcRE)
if (srcAttr) {
// register script as a import dep for hmr
const importee = cleanUrl(
slash(path.resolve('/', srcAttr[1] || srcAttr[2]))
)
debugHmr(` ${importer} imports ${importee}`)
ensureMapEntry(importerMap, importee).add(importer)
}
return `${matched}`
}
return `${devFlag}${matched}`
}
})
})
)
}

app.use(async (ctx, next) => {
Expand All @@ -94,13 +98,16 @@ export const moduleRewritePlugin: ServerPlugin = ({
return
}

if (ctx.path === '/index.html') {
if (
ctx.path === '/index.html' ||
Object.values(config.entry as object).includes(ctx.path)
) {
let html = await readBody(ctx.body)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reslove multi entry.

if (html && rewriteCache.has(html)) {
debug('/index.html: serving from cache')
debug(`${ctx.path}: serving from cache`)
ctx.body = rewriteCache.get(html)
} else if (html) {
ctx.body = await rewriteIndex(html)
ctx.body = await rewriteIndex(html, ctx.path)
rewriteCache.set(html, ctx.body)
}
return
Expand Down
5 changes: 5 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,11 @@ describe('vite', () => {

declareTests(false)

test('multi entry', async () => {
await page.goto('http://localhost:3000/test.html')
expect(await getText('h1')).toMatch('Vite Playground')
})

// Assert that all edited files are reflected on page reload
// i.e. service-worker cache is correctly busted
test('sw cache busting', async () => {
Expand Down