Skip to content

Commit

Permalink
chore: centralize debug codes
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Jul 30, 2021
1 parent 024a2de commit 6d322f5
Show file tree
Hide file tree
Showing 16 changed files with 90 additions and 66 deletions.
11 changes: 3 additions & 8 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,8 @@ import {
ServerOptions
} from './server'
import { CSSOptions } from './plugins/css'
import {
createDebugger,
isExternalUrl,
isObject,
lookupFile,
normalizePath
} from './utils'
import { isExternalUrl, isObject, lookupFile, normalizePath } from './utils'
import { createDebugger, CODES } from './debugger'
import { resolvePlugins } from './plugins'
import chalk from 'chalk'
import { ESBuildOptions } from './plugins/esbuild'
Expand All @@ -40,7 +35,7 @@ import {
import aliasPlugin from '@rollup/plugin-alias'
import { build } from 'esbuild'

const debug = createDebugger('vite:config')
const debug = createDebugger(CODES.CONFIG)

// NOTE: every export in this file is re-exported from ./index.ts so it will
// be part of the public API.
Expand Down
52 changes: 52 additions & 0 deletions packages/vite/src/node/debugger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import debug from 'debug'

// set in bin/vite.js
const filter = process.env.VITE_DEBUG_FILTER

const DEBUG = process.env.DEBUG

interface DebuggerOptions {
onlyWhenFocused?: boolean | string
}

export function createDebugger(
ns: string,
options: DebuggerOptions = {}
): debug.Debugger['log'] {
const log = debug(ns)
const { onlyWhenFocused } = options
const focus = typeof onlyWhenFocused === 'string' ? onlyWhenFocused : ns
return (msg: string, ...args: any[]) => {
if (filter && !msg.includes(filter)) {
return
}
if (onlyWhenFocused && !DEBUG?.includes(focus)) {
return
}
log(msg, ...args)
}
}

/**
* Set DEBUG environment variable to turn on debugging
* E.g. DEBUG="vite:resolve"
* To see all debug logs set DEBUG="vite:*" though it will be noisy
*/
export const CODES = {
CACHE: 'vite:cache',
CONFIG: 'vite:config',
DEPS: 'vite:deps',
ESBUILD: 'vite:esbuild',
HMR: 'vite:hmr',
LOAD: 'vite:load',
PLUGIN_RESOLVE: 'vite:plugin-resolve',
PLUGIN_TRANSFORM: 'vite:plugin-transform',
PROXY: 'vite:proxy',
RESOLVE: 'vite:resolve',
RESOLVE_DETAILS: 'vite:resolve-details',
REWRITE: 'vite:rewrite',
SOURCEMAP: 'vite:sourcemap',
SPA_FALLBACK: 'vite:spa-fallback',
TIME: 'vite:time',
TRANSFORM: 'vite:transform'
}
4 changes: 2 additions & 2 deletions packages/vite/src/node/optimizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import { createHash } from 'crypto'
import { build, BuildOptions as EsbuildBuildOptions } from 'esbuild'
import { ResolvedConfig } from '../config'
import {
createDebugger,
emptyDir,
lookupFile,
normalizePath,
writeFile,
flattenId
} from '../utils'
import { createDebugger, CODES } from '../debugger'
import { esbuildDepPlugin } from './esbuildDepPlugin'
import { ImportSpecifier, init, parse } from 'es-module-lexer'
import { scanImports } from './scan'

const debug = createDebugger('vite:deps')
const debug = createDebugger(CODES.DEPS)

export type ExportsData = [ImportSpecifier[], string[]] & {
// es-module-lexer has a facade detection but isn't always accurate for our
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/optimizer/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import {
OPTIMIZABLE_ENTRY_RE
} from '../constants'
import {
createDebugger,
normalizePath,
isObject,
cleanUrl,
externalRE,
dataUrlRE
} from '../utils'
import { createDebugger, CODES } from '../debugger'
import {
createPluginContainer,
PluginContainer
Expand All @@ -25,7 +25,7 @@ import { init, parse } from 'es-module-lexer'
import MagicString from 'magic-string'
import { transformImportGlob } from '../importGlob'

const debug = createDebugger('vite:deps')
const debug = createDebugger(CODES.DEPS)

const htmlTypesRE = /\.(html|vue|svelte)$/

Expand Down
5 changes: 3 additions & 2 deletions packages/vite/src/node/plugins/esbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
TransformOptions,
TransformResult
} from 'esbuild'
import { cleanUrl, createDebugger, generateCodeFrame } from '../utils'
import { cleanUrl, generateCodeFrame } from '../utils'
import { createDebugger, CODES } from '../debugger'
import { RawSourceMap } from '@ampproject/remapping/dist/types/types'
import { SourceMap } from 'rollup'
import { ResolvedConfig } from '..'
Expand All @@ -17,7 +18,7 @@ import { combineSourcemaps } from '../utils'
import { find as findTSConfig, readFile as readTSConfig } from 'tsconfig'
import { createRequire } from 'module'

const debug = createDebugger('vite:esbuild')
const debug = createDebugger(CODES.ESBUILD)

export interface ESBuildOptions extends TransformOptions {
include?: string | RegExp | string[] | RegExp[]
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/plugins/importAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { isCSSRequest, isDirectCSSRequest } from './css'
import {
isBuiltin,
cleanUrl,
createDebugger,
generateCodeFrame,
injectQuery,
isDataUrl,
Expand All @@ -19,6 +18,7 @@ import {
timeFrom,
normalizePath
} from '../utils'
import { createDebugger, CODES } from '../debugger'
import {
debugHmr,
handlePrunedModules,
Expand All @@ -41,7 +41,7 @@ import { makeLegalIdentifier } from '@rollup/pluginutils'
import { shouldExternalizeForSSR } from '../ssr/ssrExternal'

const isDebug = !!process.env.DEBUG
const debugRewrite = createDebugger('vite:rewrite')
const debugRewrite = createDebugger(CODES.REWRITE)

const clientDir = normalizePath(CLIENT_DIR)

Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
import {
isBuiltin,
bareImportRE,
createDebugger,
deepImportRE,
injectQuery,
isExternalUrl,
Expand All @@ -25,6 +24,7 @@ import {
cleanUrl,
slash
} from '../utils'
import { createDebugger, CODES } from '../debugger'
import { ViteDevServer, SSRTarget } from '..'
import { createFilter } from '@rollup/pluginutils'
import { PartialResolvedId } from 'rollup'
Expand All @@ -35,7 +35,7 @@ import { resolve as _resolveExports } from 'resolve.exports'
export const browserExternalId = '__vite-browser-external'

const isDebug = process.env.DEBUG
const debug = createDebugger('vite:resolve-details', {
const debug = createDebugger(CODES.RESOLVE_DETAILS, {
onlyWhenFocused: true
})

Expand Down
5 changes: 3 additions & 2 deletions packages/vite/src/node/server/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import fs from 'fs'
import path from 'path'
import chalk from 'chalk'
import { createServer, ViteDevServer } from '..'
import { createDebugger, normalizePath } from '../utils'
import { normalizePath } from '../utils'
import { createDebugger, CODES } from '../debugger'
import { ModuleNode } from './moduleGraph'
import { Update } from 'types/hmrPayload'
import { CLIENT_DIR } from '../constants'
Expand All @@ -12,7 +13,7 @@ import match from 'minimatch'
import { Server } from 'http'
import { cssLangRE } from '../plugins/css'

export const debugHmr = createDebugger('vite:hmr')
export const debugHmr = createDebugger(CODES.HMR)

const normalizedClientDir = normalizePath(CLIENT_DIR)

Expand Down
5 changes: 3 additions & 2 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ import {
import { timeMiddleware } from './middlewares/time'
import { ModuleGraph, ModuleNode } from './moduleGraph'
import { Connect } from 'types/connect'
import { createDebugger, ensureLeadingSlash, normalizePath } from '../utils'
import { ensureLeadingSlash, normalizePath } from '../utils'
import { createDebugger, CODES } from '../debugger'
import { errorMiddleware, prepareError } from './middlewares/error'
import { handleHMRUpdate, HmrOptions, handleFileAddUnlink } from './hmr'
import { openBrowser } from './openBrowser'
Expand Down Expand Up @@ -497,7 +498,7 @@ export async function createServer(
if (!middlewareMode || middlewareMode === 'html') {
middlewares.use(
history({
logger: createDebugger('vite:spa-fallback'),
logger: createDebugger(CODES.SPA_FALLBACK),
// support /dir/ without explicit index.html
rewrites: [
{
Expand Down
5 changes: 3 additions & 2 deletions packages/vite/src/node/server/middlewares/proxy.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import * as http from 'http'
import { createDebugger, isObject } from '../../utils'
import { isObject } from '../../utils'
import { createDebugger, CODES } from '../../debugger'
import httpProxy from 'http-proxy'
import { HMR_HEADER } from '../ws'
import { Connect } from 'types/connect'
import { HttpProxy } from 'types/http-proxy'
import chalk from 'chalk'
import { ResolvedConfig } from '../..'

const debug = createDebugger('vite:proxy')
const debug = createDebugger(CODES.PROXY)

export interface ProxyOptions extends HttpProxy.ServerOptions {
/**
Expand Down
5 changes: 3 additions & 2 deletions packages/vite/src/node/server/middlewares/time.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Connect } from 'types/connect'
import { createDebugger, prettifyUrl, timeFrom } from '../../utils'
import { prettifyUrl, timeFrom } from '../../utils'
import { createDebugger, CODES } from '../../debugger'

const logTime = createDebugger('vite:time')
const logTime = createDebugger(CODES.TIME)

export function timeMiddleware(root: string): Connect.NextHandleFunction {
// Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/server/middlewares/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { ViteDevServer } from '..'
import { Connect } from 'types/connect'
import {
cleanUrl,
createDebugger,
injectQuery,
isImportRequest,
isJSRequest,
Expand All @@ -13,6 +12,7 @@ import {
removeTimestampQuery,
unwrapId
} from '../../utils'
import { createDebugger, CODES } from '../../debugger'
import { send } from '../send'
import { transformRequest } from '../transformRequest'
import { isHTMLProxy } from '../../plugins/html'
Expand All @@ -30,7 +30,7 @@ import { isCSSRequest, isDirectCSSRequest } from '../../plugins/css'
*/
const NEW_DEPENDENCY_BUILD_TIMEOUT = 1000

const debugCache = createDebugger('vite:cache')
const debugCache = createDebugger(CODES.CACHE)
const isDebug = !!process.env.DEBUG

const knownIgnoreList = new Set(['/', '/favicon.ico'])
Expand Down
8 changes: 4 additions & 4 deletions packages/vite/src/node/server/pluginContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ import { combineSourcemaps } from '../utils'
import MagicString from 'magic-string'
import { FSWatcher } from 'chokidar'
import {
createDebugger,
ensureWatchedFile,
generateCodeFrame,
isObject,
Expand All @@ -68,6 +67,7 @@ import {
prettifyUrl,
timeFrom
} from '../utils'
import { createDebugger, CODES } from '../debugger'
import { FS_PREFIX } from '../constants'
import chalk from 'chalk'
import { ResolvedConfig } from '../config'
Expand Down Expand Up @@ -127,11 +127,11 @@ export async function createPluginContainer(
const isDebug = process.env.DEBUG

const seenResolves: Record<string, true | undefined> = {}
const debugResolve = createDebugger('vite:resolve')
const debugPluginResolve = createDebugger('vite:plugin-resolve', {
const debugResolve = createDebugger(CODES.RESOLVE)
const debugPluginResolve = createDebugger(CODES.PLUGIN_RESOLVE, {
onlyWhenFocused: 'vite:plugin'
})
const debugPluginTransform = createDebugger('vite:plugin-transform', {
const debugPluginTransform = createDebugger(CODES.PLUGIN_TRANSFORM, {
onlyWhenFocused: 'vite:plugin'
})

Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/server/sourcemap.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import path from 'path'
import { promises as fs } from 'fs'
import { Logger } from '../logger'
import { createDebugger } from '../utils'
import { createDebugger, CODES } from '../debugger'

const isDebug = !!process.env.DEBUG
const debug = createDebugger('vite:sourcemap', {
const debug = createDebugger(CODES.SOURCEMAP, {
onlyWhenFocused: true
})

Expand Down
8 changes: 4 additions & 4 deletions packages/vite/src/node/server/transformRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ import { SourceDescription, SourceMap } from 'rollup'
import { ViteDevServer } from '..'
import chalk from 'chalk'
import {
createDebugger,
cleanUrl,
prettifyUrl,
removeTimestampQuery,
timeFrom,
ensureWatchedFile,
isObject
} from '../utils'
import { createDebugger, CODES } from '../debugger'
import { checkPublicFile } from '../plugins/asset'
import { ssrTransform } from '../ssr/ssrTransform'
import { injectSourcesContent } from './sourcemap'
import { isFileServingAllowed } from './middlewares/static'

const debugLoad = createDebugger('vite:load')
const debugTransform = createDebugger('vite:transform')
const debugCache = createDebugger('vite:cache')
const debugLoad = createDebugger(CODES.LOAD)
const debugTransform = createDebugger(CODES.TRANSFORM)
const debugCache = createDebugger(CODES.CACHE)
const isDebug = !!process.env.DEBUG

export interface TransformResult {
Expand Down
28 changes: 0 additions & 28 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import debug from 'debug'
import chalk from 'chalk'
import fs from 'fs'
import os from 'os'
Expand Down Expand Up @@ -49,33 +48,6 @@ export function resolveFrom(id: string, basedir: string, ssr = false): string {
})
}

// set in bin/vite.js
const filter = process.env.VITE_DEBUG_FILTER

const DEBUG = process.env.DEBUG

interface DebuggerOptions {
onlyWhenFocused?: boolean | string
}

export function createDebugger(
ns: string,
options: DebuggerOptions = {}
): debug.Debugger['log'] {
const log = debug(ns)
const { onlyWhenFocused } = options
const focus = typeof onlyWhenFocused === 'string' ? onlyWhenFocused : ns
return (msg: string, ...args: any[]) => {
if (filter && !msg.includes(filter)) {
return
}
if (onlyWhenFocused && !DEBUG?.includes(focus)) {
return
}
log(msg, ...args)
}
}

export const isWindows = os.platform() === 'win32'
const VOLUME_RE = /^[A-Z]:/i

Expand Down

0 comments on commit 6d322f5

Please sign in to comment.