Skip to content

Commit

Permalink
fix: fix direct index script src hmr
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed May 6, 2020
1 parent 5b5bced commit 73d94b9
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/node/serverPluginModuleRewrite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export const moduleRewritePlugin: Plugin = ({ app, watcher, resolver }) => {
`</script>\n`

const scriptRE = /(<script\b[^>]*>)([\s\S]*?)<\/script>/gm
const srcRE = /\bsrc=(?:"([^"]+)"|'([^']+)'|([^'"\s]+)\b)/

app.use(async (ctx, next) => {
await next()
Expand All @@ -60,16 +61,26 @@ export const moduleRewritePlugin: Plugin = ({ app, watcher, resolver }) => {
} else if (ctx.body) {
await initLexer
let hasInjectedDevFlag = false
ctx.body = html!.replace(scriptRE, (_, openTag, script) => {
// also inject __DEV__ flag
const importer = '/index.html'
ctx.body = html!.replace(scriptRE, (matched, openTag, script) => {
const devFlag = hasInjectedDevFlag ? `` : devInjectionCode
hasInjectedDevFlag = true
const ret = `${devFlag}${openTag}${rewriteImports(
script,
'/index.html',
resolver
)}</script>`
return ret
if (script) {
return `${devFlag}${openTag}${rewriteImports(
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])))
debugHmr(` ${importer} imports ${importee}`)
ensureMapEntry(importerMap, importee).add(importer)
}
return `${devFlag}${matched}`
}
})
rewriteCache.set(html, ctx.body)
return
Expand Down

0 comments on commit 73d94b9

Please sign in to comment.