Skip to content

Commit

Permalink
perf(transformRequest): fast-path watch and sourcemap handling (#16170)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Mar 17, 2024
1 parent 50caf67 commit de60f1e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 24 deletions.
32 changes: 31 additions & 1 deletion packages/vite/src/node/server/sourcemap.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import path from 'node:path'
import fsp from 'node:fs/promises'
import convertSourceMap from 'convert-source-map'
import type { ExistingRawSourceMap, SourceMap } from 'rollup'
import type { Logger } from '../logger'
import { createDebugger } from '../utils'
import { blankReplacer, createDebugger } from '../utils'

const debug = createDebugger('vite:sourcemap', {
onlyWhenFocused: true,
Expand Down Expand Up @@ -143,3 +144,32 @@ export function applySourcemapIgnoreList(
if (!map.x_google_ignoreList) map.x_google_ignoreList = x_google_ignoreList
}
}

export async function extractSourcemapFromFile(
code: string,
filePath: string,
): Promise<{ code: string; map: SourceMap } | undefined> {
const map = (
convertSourceMap.fromSource(code) ||
(await convertSourceMap.fromMapFileSource(
code,
createConvertSourceMapReadMap(filePath),
))
)?.toObject()

if (map) {
return {
code: code.replace(convertSourceMap.mapFileCommentRegex, blankReplacer),
map,
}
}
}

function createConvertSourceMapReadMap(originalFileName: string) {
return (filename: string) => {
return fsp.readFile(
path.resolve(path.dirname(originalFileName), filename),
'utf-8',
)
}
}
37 changes: 14 additions & 23 deletions packages/vite/src/node/server/transformRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ import fsp from 'node:fs/promises'
import path from 'node:path'
import { performance } from 'node:perf_hooks'
import getEtag from 'etag'
import convertSourceMap from 'convert-source-map'
import MagicString from 'magic-string'
import { init, parse as parseImports } from 'es-module-lexer'
import type { PartialResolvedId, SourceDescription, SourceMap } from 'rollup'
import colors from 'picocolors'
import type { ModuleNode, ViteDevServer } from '..'
import {
blankReplacer,
createDebugger,
ensureWatchedFile,
injectQuery,
Expand All @@ -24,7 +22,11 @@ import { checkPublicFile } from '../publicDir'
import { isDepsOptimizerEnabled } from '../config'
import { getDepsOptimizer, initDevSsrDepsOptimizer } from '../optimizer'
import { cleanUrl, unwrapId } from '../../shared/utils'
import { applySourcemapIgnoreList, injectSourcesContent } from './sourcemap'
import {
applySourcemapIgnoreList,
extractSourcemapFromFile,
injectSourcesContent,
} from './sourcemap'
import { isFileServingAllowed } from './middlewares/static'
import { throwClosedServerError } from './pluginContainer'

Expand Down Expand Up @@ -263,21 +265,19 @@ async function loadAndTransform(
throw e
}
}
ensureWatchedFile(server.watcher, file, config.root)
if (code != null) {
ensureWatchedFile(server.watcher, file, config.root)
}
}
if (code) {
try {
map = (
convertSourceMap.fromSource(code) ||
(await convertSourceMap.fromMapFileSource(
code,
createConvertSourceMapReadMap(file),
))
)?.toObject()

code = code.replace(convertSourceMap.mapFileCommentRegex, blankReplacer)
const extracted = await extractSourcemapFromFile(code, file)
if (extracted) {
code = extracted.code
map = extracted.map
}
} catch (e) {
logger.warn(`Failed to load source map for ${url}.`, {
logger.warn(`Failed to load source map for ${file}.\n${e}`, {
timestamp: true,
})
}
Expand Down Expand Up @@ -406,15 +406,6 @@ async function loadAndTransform(
return result
}

function createConvertSourceMapReadMap(originalFileName: string) {
return (filename: string) => {
return fsp.readFile(
path.resolve(path.dirname(originalFileName), filename),
'utf-8',
)
}
}

/**
* When a module is soft-invalidated, we can preserve its previous `transformResult` and
* return similar code to before:
Expand Down

0 comments on commit de60f1e

Please sign in to comment.