Skip to content

Commit

Permalink
fix: support multi entry for dev
Browse files Browse the repository at this point in the history
  • Loading branch information
underfin committed May 16, 2020
1 parent fcf250e commit 72cd992
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 5 deletions.
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
12 changes: 7 additions & 5 deletions src/node/server/serverPluginModuleRewrite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,9 @@ 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'
const devFlag = hasInjectedDevFlag ? `` : devInjectionCode
return (
devFlag +
Expand Down Expand Up @@ -99,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)
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

0 comments on commit 72cd992

Please sign in to comment.