Skip to content

Commit c1d3fc9

Browse files
authoredMar 29, 2023
fix: sourcemapIgnoreList for optimizedDeps (#12633)
1 parent 4f0af3f commit c1d3fc9

File tree

3 files changed

+82
-40
lines changed

3 files changed

+82
-40
lines changed
 

‎packages/vite/src/node/server/middlewares/transform.ts

+16-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import path from 'node:path'
22
import fsp from 'node:fs/promises'
33
import type { Connect } from 'dep-types/connect'
44
import colors from 'picocolors'
5+
import type { ExistingRawSourceMap } from 'rollup'
56
import type { ViteDevServer } from '..'
67
import {
78
cleanUrl,
@@ -19,6 +20,7 @@ import {
1920
} from '../../utils'
2021
import { send } from '../send'
2122
import { ERR_LOAD_URL, transformRequest } from '../transformRequest'
23+
import { applySourcemapIgnoreList } from '../sourcemap'
2224
import { isHTMLProxy } from '../../plugins/html'
2325
import {
2426
DEP_VERSION_RE,
@@ -75,14 +77,24 @@ export function transformMiddleware(
7577
if (depsOptimizer?.isOptimizedDepUrl(url)) {
7678
// If the browser is requesting a source map for an optimized dep, it
7779
// means that the dependency has already been pre-bundled and loaded
78-
const mapFile = url.startsWith(FS_PREFIX)
80+
const sourcemapPath = url.startsWith(FS_PREFIX)
7981
? fsPathFromId(url)
8082
: normalizePath(
8183
ensureVolumeInPath(path.resolve(root, url.slice(1))),
8284
)
8385
try {
84-
const map = await fsp.readFile(mapFile, 'utf-8')
85-
return send(req, res, map, 'json', {
86+
const map = JSON.parse(
87+
await fsp.readFile(sourcemapPath, 'utf-8'),
88+
) as ExistingRawSourceMap
89+
90+
applySourcemapIgnoreList(
91+
map,
92+
sourcemapPath,
93+
server.config.server.sourcemapIgnoreList,
94+
logger,
95+
)
96+
97+
return send(req, res, JSON.stringify(map), 'json', {
8698
headers: server.config.server.headers,
8799
})
88100
} catch (e) {
@@ -91,7 +103,7 @@ export function transformMiddleware(
91103
// Send back an empty source map so the browser doesn't issue warnings
92104
const dummySourceMap = {
93105
version: 3,
94-
file: mapFile.replace(/\.map$/, ''),
106+
file: sourcemapPath.replace(/\.map$/, ''),
95107
sources: [],
96108
sourcesContent: [],
97109
names: [],

‎packages/vite/src/node/server/sourcemap.ts

+39-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from 'node:path'
22
import { promises as fs } from 'node:fs'
3-
import type { SourceMap } from 'rollup'
3+
import type { ExistingRawSourceMap, SourceMap } from 'rollup'
44
import type { Logger } from '../logger'
55
import { createDebugger } from '../utils'
66

@@ -83,3 +83,41 @@ export function getCodeWithSourcemap(
8383

8484
return code
8585
}
86+
87+
export function applySourcemapIgnoreList(
88+
map: ExistingRawSourceMap,
89+
sourcemapPath: string,
90+
sourcemapIgnoreList: (sourcePath: string, sourcemapPath: string) => boolean,
91+
logger?: Logger,
92+
): void {
93+
let { x_google_ignoreList } = map
94+
if (x_google_ignoreList === undefined) {
95+
x_google_ignoreList = []
96+
}
97+
for (
98+
let sourcesIndex = 0;
99+
sourcesIndex < map.sources.length;
100+
++sourcesIndex
101+
) {
102+
const sourcePath = map.sources[sourcesIndex]
103+
if (!sourcePath) continue
104+
105+
const ignoreList = sourcemapIgnoreList(
106+
path.isAbsolute(sourcePath)
107+
? sourcePath
108+
: path.resolve(path.dirname(sourcemapPath), sourcePath),
109+
sourcemapPath,
110+
)
111+
if (logger && typeof ignoreList !== 'boolean') {
112+
logger.warn('sourcemapIgnoreList function must return a boolean.')
113+
}
114+
115+
if (ignoreList && !x_google_ignoreList.includes(sourcesIndex)) {
116+
x_google_ignoreList.push(sourcesIndex)
117+
}
118+
}
119+
120+
if (x_google_ignoreList.length > 0) {
121+
if (!map.x_google_ignoreList) map.x_google_ignoreList = x_google_ignoreList
122+
}
123+
}

‎packages/vite/src/node/server/transformRequest.ts

+27-35
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
} from '../utils'
1919
import { checkPublicFile } from '../plugins/asset'
2020
import { getDepsOptimizer } from '../optimizer'
21-
import { injectSourcesContent } from './sourcemap'
21+
import { applySourcemapIgnoreList, injectSourcesContent } from './sourcemap'
2222
import { isFileServingAllowed } from './middlewares/static'
2323

2424
export const ERR_LOAD_URL = 'ERR_LOAD_URL'
@@ -271,41 +271,33 @@ async function loadAndTransform(
271271
if (map.mappings && !map.sourcesContent) {
272272
await injectSourcesContent(map, mod.file, logger)
273273
}
274-
for (
275-
let sourcesIndex = 0;
276-
sourcesIndex < map.sources.length;
277-
++sourcesIndex
278-
) {
279-
const sourcePath = map.sources[sourcesIndex]
280-
if (!sourcePath) continue
281-
282-
const sourcemapPath = `${mod.file}.map`
283-
const ignoreList = config.server.sourcemapIgnoreList(
284-
path.isAbsolute(sourcePath)
285-
? sourcePath
286-
: path.resolve(path.dirname(sourcemapPath), sourcePath),
287-
sourcemapPath,
288-
)
289-
if (typeof ignoreList !== 'boolean') {
290-
logger.warn('sourcemapIgnoreList function must return a boolean.')
291-
}
292-
if (ignoreList) {
293-
if (map.x_google_ignoreList === undefined) {
294-
map.x_google_ignoreList = []
295-
}
296-
if (!map.x_google_ignoreList.includes(sourcesIndex)) {
297-
map.x_google_ignoreList.push(sourcesIndex)
298-
}
299-
}
300274

301-
// Rewrite sources to relative paths to give debuggers the chance
302-
// to resolve and display them in a meaningful way (rather than
303-
// with absolute paths).
304-
if (path.isAbsolute(sourcePath) && path.isAbsolute(mod.file)) {
305-
map.sources[sourcesIndex] = path.relative(
306-
path.dirname(mod.file),
307-
sourcePath,
308-
)
275+
const sourcemapPath = `${mod.file}.map`
276+
applySourcemapIgnoreList(
277+
map,
278+
sourcemapPath,
279+
config.server.sourcemapIgnoreList,
280+
logger,
281+
)
282+
283+
if (path.isAbsolute(mod.file)) {
284+
for (
285+
let sourcesIndex = 0;
286+
sourcesIndex < map.sources.length;
287+
++sourcesIndex
288+
) {
289+
const sourcePath = map.sources[sourcesIndex]
290+
if (sourcePath) {
291+
// Rewrite sources to relative paths to give debuggers the chance
292+
// to resolve and display them in a meaningful way (rather than
293+
// with absolute paths).
294+
if (path.isAbsolute(sourcePath)) {
295+
map.sources[sourcesIndex] = path.relative(
296+
path.dirname(mod.file),
297+
sourcePath,
298+
)
299+
}
300+
}
309301
}
310302
}
311303
}

0 commit comments

Comments
 (0)