Skip to content

Commit 04fc30a

Browse files
fix(sourcemap): skip URL source roots when injecting sources content (#23519)
Co-authored-by: sapphi-red <[email protected]>
1 parent 5f89433 commit 04fc30a

2 files changed

Lines changed: 73 additions & 3 deletions

File tree

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

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
import { describe, expect, test } from 'vitest'
1+
import { describe, expect, test, vi } from 'vitest'
22
import { isWindows } from '../../../shared/utils'
3-
import { getNodeModulesPackageRoot } from '../sourcemap'
3+
import { createLogger } from '../../logger'
4+
import {
5+
getNodeModulesPackageRoot,
6+
injectSourcesContent,
7+
type SourceMapLike,
8+
} from '../sourcemap'
49

510
describe('getNodeModulesPackageRoot', () => {
611
const cases = [
@@ -69,3 +74,62 @@ describe('getNodeModulesPackageRoot', () => {
6974
})
7075
}
7176
})
77+
78+
describe('injectSourcesContent', () => {
79+
function createMockLogger() {
80+
const logger = createLogger()
81+
logger.warnOnce = vi.fn()
82+
return logger
83+
}
84+
85+
test('leaves maps with a remote sourceRoot alone', async () => {
86+
const map: SourceMapLike = {
87+
sources: ['index.ts'],
88+
sourceRoot: 'https://raw.githubusercontent.com/fb55/domutils/abc123/src/',
89+
}
90+
const logger = createMockLogger()
91+
92+
await injectSourcesContent(
93+
map,
94+
'/project/node_modules/domutils/lib/esm/index.js',
95+
logger,
96+
)
97+
98+
expect(logger.warnOnce).not.toHaveBeenCalled()
99+
expect(map.sourcesContent).toBeUndefined()
100+
})
101+
102+
test('does not inject content for remote sources', async () => {
103+
const map: SourceMapLike = {
104+
sources: [
105+
'https://raw.githubusercontent.com/fb55/domutils/abc123/src/index.ts',
106+
],
107+
}
108+
const logger = createMockLogger()
109+
110+
await injectSourcesContent(
111+
map,
112+
'/project/node_modules/domutils/lib/esm/index.js',
113+
logger,
114+
)
115+
116+
expect(logger.warnOnce).not.toHaveBeenCalled()
117+
expect(map.sourcesContent).toStrictEqual([])
118+
})
119+
120+
test('warns for sources that resolve outside the package', async () => {
121+
const map: SourceMapLike = {
122+
sources: ['/outside/project/index.ts'],
123+
}
124+
const logger = createMockLogger()
125+
126+
await injectSourcesContent(
127+
map,
128+
'/project/node_modules/foo/dist/index.js',
129+
logger,
130+
)
131+
132+
expect(logger.warnOnce).toHaveBeenCalledOnce()
133+
expect(map.sourcesContent).toStrictEqual([null])
134+
})
135+
})

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type { Logger } from '../logger'
99
import {
1010
blankReplacer,
1111
createDebugger,
12+
isExternalUrl,
1213
isParentDirectory,
1314
normalizePath,
1415
} from '../utils'
@@ -49,7 +50,7 @@ export function getNodeModulesPackageRoot(
4950
// prefixes used for special handling in esbuildDepPlugin.
5051
const virtualSourceRE = /^(?:dep:|browser-external:|virtual:)|\0/
5152

52-
interface SourceMapLike {
53+
export interface SourceMapLike {
5354
sources: string[]
5455
sourcesContent?: (string | null)[]
5556
sourceRoot?: string
@@ -71,6 +72,10 @@ export async function injectSourcesContent(
7172
file: string,
7273
logger: Logger,
7374
): Promise<void> {
75+
if (map.sourceRoot && isExternalUrl(map.sourceRoot)) {
76+
return
77+
}
78+
7479
let sourceRootPromise: Promise<string | undefined>
7580

7681
const packageRoot = getNodeModulesPackageRoot(file)
@@ -82,6 +87,7 @@ export async function injectSourcesContent(
8287
if (
8388
sourcesContent[index] == null &&
8489
sourcePath &&
90+
!isExternalUrl(sourcePath) &&
8591
!virtualSourceRE.test(sourcePath)
8692
) {
8793
sourcesContentPromises.push(

0 commit comments

Comments
 (0)