Skip to content

Commit

Permalink
fix: inject dev only script tags directly
Browse files Browse the repository at this point in the history
...to avoid injecting into commented out scripts, fix #161
  • Loading branch information
yyx990803 committed May 18, 2020
1 parent d16f567 commit 89ac245
Showing 1 changed file with 23 additions and 23 deletions.
46 changes: 23 additions & 23 deletions src/node/server/serverPluginModuleRewrite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,30 +60,30 @@ export const moduleRewritePlugin: ServerPlugin = ({

async function rewriteIndex(importer: string, html: string) {
await initLexer
let hasInjectedDevFlag = false
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)
return (
devInjectionCode +
html!.replace(scriptRE, (matched, openTag, script) => {
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 Down

0 comments on commit 89ac245

Please sign in to comment.