From 5e6c1156d92d836854eebe8190b234ff6b62fbd8 Mon Sep 17 00:00:00 2001 From: patak Date: Tue, 28 Mar 2023 20:54:43 +0200 Subject: [PATCH 1/2] fix: sourcemapIgnoreList for optimizedDeps --- .../src/node/server/middlewares/transform.ts | 20 ++++-- packages/vite/src/node/server/sourcemap.ts | 40 ++++++++++++ .../vite/src/node/server/transformRequest.ts | 62 ++++++++----------- 3 files changed, 83 insertions(+), 39 deletions(-) diff --git a/packages/vite/src/node/server/middlewares/transform.ts b/packages/vite/src/node/server/middlewares/transform.ts index 28923c0307f5df..a7f3f474915bba 100644 --- a/packages/vite/src/node/server/middlewares/transform.ts +++ b/packages/vite/src/node/server/middlewares/transform.ts @@ -1,6 +1,7 @@ import path from 'node:path' import type { Connect } from 'dep-types/connect' import colors from 'picocolors' +import type { SourceMap } from 'rollup' import type { ViteDevServer } from '..' import { cleanUrl, @@ -18,6 +19,7 @@ import { } from '../../utils' import { send } from '../send' import { ERR_LOAD_URL, transformRequest } from '../transformRequest' +import { applySourcemapIgnoreList } from '../sourcemap' import { isHTMLProxy } from '../../plugins/html' import { DEP_VERSION_RE, @@ -74,14 +76,24 @@ export function transformMiddleware( if (depsOptimizer?.isOptimizedDepUrl(url)) { // If the browser is requesting a source map for an optimized dep, it // means that the dependency has already been pre-bundled and loaded - const mapFile = url.startsWith(FS_PREFIX) + const sourcemapPath = url.startsWith(FS_PREFIX) ? fsPathFromId(url) : normalizePath( ensureVolumeInPath(path.resolve(root, url.slice(1))), ) try { - const map = await loadOptimizedDep(mapFile, depsOptimizer) - return send(req, res, map, 'json', { + const map = JSON.parse( + await loadOptimizedDep(sourcemapPath, depsOptimizer), + ) as SourceMap + + applySourcemapIgnoreList( + map, + sourcemapPath, + server.config.server.sourcemapIgnoreList, + logger, + ) + + return send(req, res, JSON.stringify(map), 'json', { headers: server.config.server.headers, }) } catch (e) { @@ -90,7 +102,7 @@ export function transformMiddleware( // Send back an empty source map so the browser doesn't issue warnings const dummySourceMap = { version: 3, - file: mapFile.replace(/\.map$/, ''), + file: sourcemapPath.replace(/\.map$/, ''), sources: [], sourcesContent: [], names: [], diff --git a/packages/vite/src/node/server/sourcemap.ts b/packages/vite/src/node/server/sourcemap.ts index f5c57cadb5b72b..088558e7eaa022 100644 --- a/packages/vite/src/node/server/sourcemap.ts +++ b/packages/vite/src/node/server/sourcemap.ts @@ -83,3 +83,43 @@ export function getCodeWithSourcemap( return code } + +export function applySourcemapIgnoreList( + map: SourceMap, + sourcemapPath: string, + sourcemapIgnoreList: (sourcePath: string, sourcemapPath: string) => boolean, + logger?: Logger, +): void { + // @ts-expect-error x_google_ignoreList isn't yet in the SourceMap type + let { x_google_ignoreList } = map + if (x_google_ignoreList === undefined) { + x_google_ignoreList = [] + } + for ( + let sourcesIndex = 0; + sourcesIndex < map.sources.length; + ++sourcesIndex + ) { + const sourcePath = map.sources[sourcesIndex] + if (!sourcePath) continue + + const ignoreList = sourcemapIgnoreList( + path.isAbsolute(sourcePath) + ? sourcePath + : path.resolve(path.dirname(sourcemapPath), sourcePath), + sourcemapPath, + ) + if (logger && typeof ignoreList !== 'boolean') { + logger.warn('sourcemapIgnoreList function must return a boolean.') + } + + if (ignoreList && !x_google_ignoreList.includes(sourcesIndex)) { + x_google_ignoreList.push(sourcesIndex) + } + } + + if (x_google_ignoreList.length > 0) { + // @ts-expect-error x_google_ignoreList isn't yet in the SourceMap type + if (!map.x_google_ignoreList) map.x_google_ignoreList = x_google_ignoreList + } +} diff --git a/packages/vite/src/node/server/transformRequest.ts b/packages/vite/src/node/server/transformRequest.ts index 36141f58017aa2..a91076f8ede659 100644 --- a/packages/vite/src/node/server/transformRequest.ts +++ b/packages/vite/src/node/server/transformRequest.ts @@ -18,7 +18,7 @@ import { } from '../utils' import { checkPublicFile } from '../plugins/asset' import { getDepsOptimizer } from '../optimizer' -import { injectSourcesContent } from './sourcemap' +import { applySourcemapIgnoreList, injectSourcesContent } from './sourcemap' import { isFileServingAllowed } from './middlewares/static' export const ERR_LOAD_URL = 'ERR_LOAD_URL' @@ -271,41 +271,33 @@ async function loadAndTransform( if (map.mappings && !map.sourcesContent) { await injectSourcesContent(map, mod.file, logger) } - for ( - let sourcesIndex = 0; - sourcesIndex < map.sources.length; - ++sourcesIndex - ) { - const sourcePath = map.sources[sourcesIndex] - if (!sourcePath) continue - - const sourcemapPath = `${mod.file}.map` - const ignoreList = config.server.sourcemapIgnoreList( - path.isAbsolute(sourcePath) - ? sourcePath - : path.resolve(path.dirname(sourcemapPath), sourcePath), - sourcemapPath, - ) - if (typeof ignoreList !== 'boolean') { - logger.warn('sourcemapIgnoreList function must return a boolean.') - } - if (ignoreList) { - if (map.x_google_ignoreList === undefined) { - map.x_google_ignoreList = [] - } - if (!map.x_google_ignoreList.includes(sourcesIndex)) { - map.x_google_ignoreList.push(sourcesIndex) - } - } - // Rewrite sources to relative paths to give debuggers the chance - // to resolve and display them in a meaningful way (rather than - // with absolute paths). - if (path.isAbsolute(sourcePath) && path.isAbsolute(mod.file)) { - map.sources[sourcesIndex] = path.relative( - path.dirname(mod.file), - sourcePath, - ) + const sourcemapPath = `${mod.file}.map` + applySourcemapIgnoreList( + map as SourceMap, + sourcemapPath, + config.server.sourcemapIgnoreList, + logger, + ) + + if (path.isAbsolute(mod.file)) { + for ( + let sourcesIndex = 0; + sourcesIndex < map.sources.length; + ++sourcesIndex + ) { + const sourcePath = map.sources[sourcesIndex] + if (sourcePath) { + // Rewrite sources to relative paths to give debuggers the chance + // to resolve and display them in a meaningful way (rather than + // with absolute paths). + if (path.isAbsolute(sourcePath)) { + map.sources[sourcesIndex] = path.relative( + path.dirname(mod.file), + sourcePath, + ) + } + } } } } From 20409c1518d84283734e7681f8cc7c21a68a1fbf Mon Sep 17 00:00:00 2001 From: patak Date: Wed, 29 Mar 2023 11:32:54 +0200 Subject: [PATCH 2/2] chore: use ExistingRawSourceMap --- packages/vite/src/node/server/middlewares/transform.ts | 4 ++-- packages/vite/src/node/server/sourcemap.ts | 6 ++---- packages/vite/src/node/server/transformRequest.ts | 2 +- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/packages/vite/src/node/server/middlewares/transform.ts b/packages/vite/src/node/server/middlewares/transform.ts index c2c7f0e5180944..8fb184181c417d 100644 --- a/packages/vite/src/node/server/middlewares/transform.ts +++ b/packages/vite/src/node/server/middlewares/transform.ts @@ -2,7 +2,7 @@ import path from 'node:path' import fsp from 'node:fs/promises' import type { Connect } from 'dep-types/connect' import colors from 'picocolors' -import type { SourceMap } from 'rollup' +import type { ExistingRawSourceMap } from 'rollup' import type { ViteDevServer } from '..' import { cleanUrl, @@ -85,7 +85,7 @@ export function transformMiddleware( try { const map = JSON.parse( await fsp.readFile(sourcemapPath, 'utf-8'), - ) as SourceMap + ) as ExistingRawSourceMap applySourcemapIgnoreList( map, diff --git a/packages/vite/src/node/server/sourcemap.ts b/packages/vite/src/node/server/sourcemap.ts index 088558e7eaa022..c988a02fca8b18 100644 --- a/packages/vite/src/node/server/sourcemap.ts +++ b/packages/vite/src/node/server/sourcemap.ts @@ -1,6 +1,6 @@ import path from 'node:path' import { promises as fs } from 'node:fs' -import type { SourceMap } from 'rollup' +import type { ExistingRawSourceMap, SourceMap } from 'rollup' import type { Logger } from '../logger' import { createDebugger } from '../utils' @@ -85,12 +85,11 @@ export function getCodeWithSourcemap( } export function applySourcemapIgnoreList( - map: SourceMap, + map: ExistingRawSourceMap, sourcemapPath: string, sourcemapIgnoreList: (sourcePath: string, sourcemapPath: string) => boolean, logger?: Logger, ): void { - // @ts-expect-error x_google_ignoreList isn't yet in the SourceMap type let { x_google_ignoreList } = map if (x_google_ignoreList === undefined) { x_google_ignoreList = [] @@ -119,7 +118,6 @@ export function applySourcemapIgnoreList( } if (x_google_ignoreList.length > 0) { - // @ts-expect-error x_google_ignoreList isn't yet in the SourceMap type if (!map.x_google_ignoreList) map.x_google_ignoreList = x_google_ignoreList } } diff --git a/packages/vite/src/node/server/transformRequest.ts b/packages/vite/src/node/server/transformRequest.ts index a91076f8ede659..82bc7fabc20716 100644 --- a/packages/vite/src/node/server/transformRequest.ts +++ b/packages/vite/src/node/server/transformRequest.ts @@ -274,7 +274,7 @@ async function loadAndTransform( const sourcemapPath = `${mod.file}.map` applySourcemapIgnoreList( - map as SourceMap, + map, sourcemapPath, config.server.sourcemapIgnoreList, logger,