Skip to content

Commit 05a003e

Browse files
lazergsapphi-red
andauthored
fix(config): resolve sourcemap paths against sourcemap location (#23239)
Co-authored-by: sapphi-red <[email protected]>
1 parent 495d9ff commit 05a003e

3 files changed

Lines changed: 25 additions & 4 deletions

File tree

‎packages/vite/src/node/__tests__/config.spec.ts‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ import { stripVTControlCharacters } from 'node:util'
66
import { afterEach, assert, describe, expect, test, vi } from 'vitest'
77
import type { InlineConfig, PluginOption } from '..'
88
import type { UserConfig, UserConfigExport } from '../config'
9-
import { defineConfig, loadConfigFromFile, resolveConfig } from '../config'
9+
import {
10+
bundleConfigFile,
11+
defineConfig,
12+
loadConfigFromFile,
13+
resolveConfig,
14+
} from '../config'
1015
import { resolveServerOptions } from '../server'
1116
import { resolveEnvPrefix } from '../env'
1217
import {
@@ -1959,6 +1964,21 @@ describe('loadConfigFromFile', () => {
19591964
expect(c.dirname).toContain('shebang-crlf')
19601965
})
19611966

1967+
test('sourcemap of a nested config file points to itself', async () => {
1968+
const configPath = path.resolve(
1969+
fixtures,
1970+
'./nested/.nested/vite.config.mts',
1971+
)
1972+
const { code } = await bundleConfigFile(configPath, true)
1973+
const [, base64Map] = code.match(
1974+
/\/\/# sourceMappingURL=data:application\/json;charset=utf-8;base64,(.+)/,
1975+
)!
1976+
const map = JSON.parse(Buffer.from(base64Map, 'base64').toString())
1977+
expect(map.sources.map(normalizePath)).toStrictEqual([
1978+
normalizePath(configPath),
1979+
])
1980+
})
1981+
19621982
describe('loadConfigFromFile with configLoader: native', () => {
19631983
const fixtureRoot = path.resolve(fixtures, './native-import')
19641984

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default {}

‎packages/vite/src/node/config.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2504,7 +2504,7 @@ async function bundleAndLoadConfigFile(
25042504
}
25052505
}
25062506

2507-
async function bundleConfigFile(
2507+
export async function bundleConfigFile(
25082508
fileName: string,
25092509
isESM: boolean,
25102510
): Promise<{
@@ -2653,8 +2653,8 @@ async function bundleConfigFile(
26532653
const result = await bundle.generate({
26542654
format: isESM ? 'esm' : 'cjs',
26552655
sourcemap: 'inline',
2656-
sourcemapPathTransform(relative) {
2657-
return path.resolve(fileName, relative)
2656+
sourcemapPathTransform(relative, sourcemapPath) {
2657+
return path.resolve(path.dirname(sourcemapPath), relative)
26582658
},
26592659
// we want to generate a single chunk like esbuild does with `splitting: false`
26602660
codeSplitting: false,

0 commit comments

Comments
 (0)