Skip to content

Commit

Permalink
fix(hmr): fix nested import.meta.hot/env detection
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jul 2, 2020
1 parent 2b87b1d commit bc4ae27
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 44 deletions.
26 changes: 6 additions & 20 deletions src/node/server/serverPluginHmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,7 @@ import { InternalResolver } from '../resolver'
import LRUCache from 'lru-cache'
import slash from 'slash'
import { isCSSRequest } from '../utils/cssUtils'
import {
Node,
StringLiteral,
Statement,
Expression,
IfStatement
} from '@babel/types'
import { Node, StringLiteral, Statement, Expression } from '@babel/types'
import { resolveCompiler } from '../utils'
import { HMRPayload } from '../../hmrPayload'

Expand Down Expand Up @@ -279,7 +273,6 @@ export function rewriteFileWithHMR(
s: MagicString
) {
let hasDeclined = false
let importMetaConditional: IfStatement | undefined

const registerDep = (e: StringLiteral) => {
const deps = ensureMapEntry(hmrAcceptanceMap, importer)
Expand Down Expand Up @@ -404,10 +397,6 @@ export function rewriteFileWithHMR(
// if (import.meta.hot) ...
if (node.type === 'IfStatement') {
const isDevBlock = isMetaHot(node.test)
if (isDevBlock && !importMetaConditional) {
// remember the first occurrence of `if (import.meta.hot)`
importMetaConditional = node
}
if (node.consequent.type === 'BlockStatement') {
node.consequent.body.forEach((s) =>
checkStatements(s, false, isDevBlock)
Expand All @@ -422,14 +411,11 @@ export function rewriteFileWithHMR(
const ast = parse(source)
ast.forEach((s) => checkStatements(s, true, false))

if (importMetaConditional) {
// inject import.meta.hot
s.prependLeft(
importMetaConditional.start!,
`import { createHotContext } from "${hmrClientPublicPath}"; ` +
`import.meta.hot = createHotContext(${JSON.stringify(importer)}); `
)
}
// inject import.meta.hot
s.prepend(
`import { createHotContext } from "${hmrClientPublicPath}"; ` +
`import.meta.hot = createHotContext(${JSON.stringify(importer)}); `
)

// clear decline state
if (!hasDeclined) {
Expand Down
46 changes: 22 additions & 24 deletions src/node/server/serverPluginModuleRewrite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,13 @@ export function rewriteImports(
)
}

if (imports.length) {
const hasHMR = source.includes('import.meta.hot')
const hasEnv = source.includes('import.meta.env')

if (imports.length || hasHMR || hasEnv) {
debug(`${importer}: rewriting`)
const s = new MagicString(source)
let hasReplaced = false
let hasInjectedEnv = false
let hasRewrittenForHMR = false

const prevImportees = importeeMap.get(importer)
const currentImportees = new Set<string>()
Expand Down Expand Up @@ -178,29 +179,26 @@ export function rewriteImports(
debugHmr(` ${importer} imports ${importee}`)
ensureMapEntry(importerMap, importee).add(importer)
}
} else {
if (id === 'import.meta') {
if (!hasRewrittenForHMR && source.slice(end, end + 4) === '.hot') {
debugHmr(`rewriting ${importer} for HMR.`)
rewriteFileWithHMR(root, source, importer, resolver, s)
hasRewrittenForHMR = true
hasReplaced = true
}

if (!hasInjectedEnv && source.slice(end, end + 4) === '.env') {
s.prepend(
`import __VITE_ENV__ from "${envPublicPath}";\n` +
`import.meta.env = __VITE_ENV__;\n`
)
hasInjectedEnv = true
hasReplaced = true
}
} else {
debug(`[vite] ignored dynamic import(${id})`)
}
} else if (id !== 'import.meta') {
debug(`[vite] ignored dynamic import(${id})`)
}
}

if (hasHMR) {
debugHmr(`rewriting ${importer} for HMR.`)
rewriteFileWithHMR(root, source, importer, resolver, s)
hasReplaced = true
}

if (hasEnv) {
debug(` injecting import.meta.env for ${importer}`)
s.prepend(
`import __VITE_ENV__ from "${envPublicPath}"; ` +
`import.meta.env = __VITE_ENV__; `
)
hasReplaced = true
}

// since the importees may have changed due to edits,
// check if we need to remove this importer from certain importees
if (prevImportees) {
Expand All @@ -215,7 +213,7 @@ export function rewriteImports(
}

if (!hasReplaced) {
debug(` no imports rewritten.`)
debug(` nothing needs rewriting.`)
}

return hasReplaced ? s.toString() : source
Expand Down

0 comments on commit bc4ae27

Please sign in to comment.