Skip to content

Commit

Permalink
Revert "refactor: can revert #15805 ?"
Browse files Browse the repository at this point in the history
This reverts commit eea342d.
  • Loading branch information
hi-ogawa committed Sep 20, 2024
1 parent eea342d commit 481e7b4
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/vite/src/node/plugins/define.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { transform } from 'esbuild'
import { TraceMap, decodedMap, encodedMap } from '@jridgewell/trace-mapping'
import type { ResolvedConfig } from '../config'
import type { Plugin } from '../plugin'
import { escapeRegex } from '../utils'
Expand Down Expand Up @@ -200,6 +201,26 @@ export async function replaceDefine(
: true,
})

// remove esbuild's <define:...> source entries
// since they would confuse source map remapping/collapsing which expects a single source
if (result.map.includes('<define:')) {
const originalMap = new TraceMap(result.map)
if (originalMap.sources.length >= 2) {
const sourceIndex = originalMap.sources.indexOf(id)
const decoded = decodedMap(originalMap)
decoded.sources = [id]
decoded.mappings = decoded.mappings.map((segments) =>
segments.filter((segment) => {
// modify and filter
const index = segment[1]
segment[1] = 0
return index === sourceIndex
}),
)
result.map = JSON.stringify(encodedMap(new TraceMap(decoded as any)))
}
}

return {
code: result.code,
map: result.map || null,
Expand Down

0 comments on commit 481e7b4

Please sign in to comment.