Skip to content

Commit

Permalink
refactor: use utils.isObject insteads of typeof (#4330)
Browse files Browse the repository at this point in the history
  • Loading branch information
webfansplz committed Jul 20, 2021
1 parent a870584 commit 514e124
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 12 deletions.
4 changes: 2 additions & 2 deletions packages/vite/src/node/plugins/clientInjections.ts
Expand Up @@ -2,7 +2,7 @@ import path from 'path'
import { Plugin } from '../plugin'
import { ResolvedConfig } from '../config'
import { CLIENT_ENTRY, ENV_ENTRY } from '../constants'
import { normalizePath } from '../utils'
import { normalizePath, isObject } from '../utils'

// ids in transform are normalized to unix style
const normalizedClientEntry = normalizePath(CLIENT_ENTRY)
Expand All @@ -25,7 +25,7 @@ export function clientInjectionsPlugin(config: ResolvedConfig): Plugin {
const overlay = options.overlay !== false
let port: number | string | undefined
if (config.server.middlewareMode) {
if (typeof config.server.hmr === 'object') {
if (isObject(config.server.hmr)) {
port = config.server.hmr.clientPort || config.server.hmr.port
}
port = String(port || 24678)
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/server/http.ts
Expand Up @@ -3,6 +3,7 @@ import path from 'path'
import { Server as HttpServer } from 'http'
import { ServerOptions as HttpsServerOptions } from 'https'
import { ResolvedConfig, ServerOptions } from '..'
import { isObject } from '../utils'
import { Connect } from 'types/connect'
import { Logger } from '../logger'

Expand Down Expand Up @@ -34,8 +35,7 @@ export async function resolveHttpsConfig(
): Promise<HttpsServerOptions | undefined> {
if (!config.server.https) return undefined

const httpsOption =
typeof config.server.https === 'object' ? config.server.https : {}
const httpsOption = isObject(config.server.https) ? config.server.https : {}

const { ca, cert, key, pfx } = httpsOption
Object.assign(httpsOption, {
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/server/middlewares/proxy.ts
@@ -1,5 +1,5 @@
import * as http from 'http'
import { createDebugger } from '../../utils'
import { createDebugger, isObject } from '../../utils'
import httpProxy from 'http-proxy'
import { HMR_HEADER } from '../ws'
import { Connect } from 'types/connect'
Expand Down Expand Up @@ -94,7 +94,7 @@ export function proxyMiddleware(
req.url = bypassResult
debug(`bypass: ${req.url} -> ${bypassResult}`)
return next()
} else if (typeof bypassResult === 'object') {
} else if (isObject(bypassResult)) {
Object.assign(options, bypassResult)
debug(`bypass: ${req.url} use modified options: %O`, options)
return next()
Expand Down
3 changes: 2 additions & 1 deletion packages/vite/src/node/server/pluginContainer.ts
Expand Up @@ -61,6 +61,7 @@ import {
createDebugger,
ensureWatchedFile,
generateCodeFrame,
isObject,
isExternalUrl,
normalizePath,
numberToPos,
Expand Down Expand Up @@ -506,7 +507,7 @@ export async function createPluginContainer(
plugin.name,
prettifyUrl(id, root)
)
if (typeof result === 'object') {
if (isObject(result)) {
code = result.code || ''
if (result.map) ctx.sourcemapChain.push(result.map)
} else {
Expand Down
7 changes: 4 additions & 3 deletions packages/vite/src/node/server/transformRequest.ts
Expand Up @@ -11,7 +11,8 @@ import {
prettifyUrl,
removeTimestampQuery,
timeFrom,
ensureWatchedFile
ensureWatchedFile,
isObject
} from '../utils'
import { checkPublicFile } from '../plugins/asset'
import { ssrTransform } from '../ssr/ssrTransform'
Expand Down Expand Up @@ -101,7 +102,7 @@ export async function transformRequest(
}
} else {
isDebug && debugLoad(`${timeFrom(loadStart)} [plugin] ${prettyUrl}`)
if (typeof loadResult === 'object') {
if (isObject(loadResult)) {
code = loadResult.code
map = loadResult.map
} else {
Expand Down Expand Up @@ -130,7 +131,7 @@ export async function transformRequest(
const transformResult = await pluginContainer.transform(code, id, map, ssr)
if (
transformResult == null ||
(typeof transformResult === 'object' && transformResult.code == null)
(isObject(transformResult) && transformResult.code == null)
) {
// no transform applied, keep code as-is
isDebug &&
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/server/ws.ts
Expand Up @@ -7,7 +7,7 @@ import {
import WebSocket from 'ws'
import { ErrorPayload, HMRPayload } from 'types/hmrPayload'
import { ResolvedConfig } from '..'

import { isObject } from '../utils'
export const HMR_HEADER = 'vite-hmr'

export interface WebSocketServer {
Expand All @@ -23,7 +23,7 @@ export function createWebSocketServer(
let wss: WebSocket.Server
let httpsServer: Server | undefined = undefined

const hmr = typeof config.server.hmr === 'object' && config.server.hmr
const hmr = isObject(config.server.hmr) && config.server.hmr
const wsServer = (hmr && hmr.server) || server

if (wsServer) {
Expand Down

0 comments on commit 514e124

Please sign in to comment.