Skip to content

Commit

Permalink
fix: do not shim process with actual object
Browse files Browse the repository at this point in the history
otherwise it breaks conditional process check code
  • Loading branch information
yyx990803 committed Feb 3, 2021
1 parent da36547 commit 8ad7ecd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
5 changes: 1 addition & 4 deletions packages/vite/src/client/env.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
declare const __MODE__: string
declare const __DEFINES__: Record<string, any>

const that = new Function('return this')()
// shim process
;(that as any).process = (that as any).process || {}
;(that as any).process.env = (that as any).process.env || {}
;(that as any).process.env.NODE_ENV = __MODE__

// assign defines
const defines = __DEFINES__
Expand Down
9 changes: 8 additions & 1 deletion packages/vite/src/node/plugins/clientInjections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { normalizePath } from '../utils'

// ids in transform are normalized to unix style
const normalizedClientEntry = normalizePath(CLIENT_ENTRY)
const normalizedEnvEntry = normalizePath(ENV_ENTRY)

/**
* some values used by the client needs to be dynamically injected by the server
Expand All @@ -15,7 +16,7 @@ export function clientInjectionsPlugin(config: ResolvedConfig): Plugin {
return {
name: 'vite:client-inject',
transform(code, id) {
if (id === normalizedClientEntry || normalizePath(ENV_ENTRY)) {
if (id === normalizedClientEntry || id === normalizedEnvEntry) {
let options = config.server.hmr
options = options && typeof options !== 'boolean' ? options : {}
const host = options.host || null
Expand Down Expand Up @@ -49,6 +50,12 @@ export function clientInjectionsPlugin(config: ResolvedConfig): Plugin {
.replace(`__HMR_PORT__`, JSON.stringify(port))
.replace(`__HMR_TIMEOUT__`, JSON.stringify(timeout))
.replace(`__HMR_ENABLE_OVERLAY__`, JSON.stringify(overlay))
} else if (code.includes('process.env.NODE_ENV')) {
// replace process.env.NODE_ENV
return code.replace(
/\bprocess\.env\.NODE_ENV\b/g,
JSON.stringify(config.mode)
)
}
}
}
Expand Down

0 comments on commit 8ad7ecd

Please sign in to comment.