Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update sourcemap in importAnalysisBuild #7825

Merged
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
73 changes: 43 additions & 30 deletions packages/vite/src/node/plugins/importAnalysisBuild.ts
Expand Up @@ -22,7 +22,7 @@ export const preloadMarker = `__VITE_PRELOAD__`
export const preloadBaseMarker = `__VITE_PRELOAD_BASE__`

const preloadHelperId = 'vite/preload-helper'
const preloadMarkerRE = new RegExp(`"${preloadMarker}"`, 'g')
const preloadMarkerWithQuote = `"${preloadMarker}"` as const

/**
* Helper for preloading CSS and direct imports of async chunks in parallel to
Expand Down Expand Up @@ -265,27 +265,10 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
this.error(e, e.idx)
}

const updateChunkCode = (ms: MagicString) => {
if (!ms.hasChanged()) return

chunk.code = ms.toString()
if (config.build.sourcemap && chunk.map) {
const nextMap = ms.generateMap({
source: chunk.fileName,
hires: true
})
const map = combineSourcemaps(
chunk.fileName,
[nextMap as RawSourceMap, chunk.map as RawSourceMap],
false
) as SourceMap
map.toUrl = () => genSourceMapUrl(map)
chunk.map = map
}
}
const s = new MagicString(code)
const rewroteMarkerStartPos = new Set() // position of the leading double quote

if (imports.length) {
const s = new MagicString(code)
for (let index = 0; index < imports.length; index++) {
// To handle escape sequences in specifier strings, the .n field will be provided where possible.
const {
Expand Down Expand Up @@ -345,16 +328,16 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
addDeps(normalizedFile)
}

let markPos = code.indexOf(preloadMarker, end)
let markerStartPos = code.indexOf(preloadMarkerWithQuote, end)
// fix issue #3051
if (markPos === -1 && imports.length === 1) {
markPos = code.indexOf(preloadMarker)
if (markerStartPos === -1 && imports.length === 1) {
markerStartPos = code.indexOf(preloadMarkerWithQuote)
}

if (markPos > 0) {
if (markerStartPos > 0) {
s.overwrite(
markPos - 1,
markPos + preloadMarker.length + 1,
markerStartPos,
markerStartPos + preloadMarkerWithQuote.length,
// the dep list includes the main chunk, so only need to
// preload when there are actual other deps.
deps.size > 1 ||
Expand All @@ -364,16 +347,46 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
: `[]`,
{ contentOnly: true }
)
rewroteMarkerStartPos.add(markerStartPos)
}
}
updateChunkCode(s)
}

// there may still be markers due to inlined dynamic imports, remove
// all the markers regardless
const s = new MagicString(chunk.code)
s.replace(preloadMarkerRE, 'void 0')
updateChunkCode(s)
let markerStartPos = code.indexOf(preloadMarkerWithQuote)
do {
if (!rewroteMarkerStartPos.has(markerStartPos)) {
s.overwrite(
markerStartPos,
markerStartPos + preloadMarkerWithQuote.length,
'void 0',
{ contentOnly: true }
)
}

markerStartPos = code.indexOf(
preloadMarkerWithQuote,
markerStartPos + preloadMarkerWithQuote.length
)
} while (markerStartPos >= 0)
sapphi-red marked this conversation as resolved.
Show resolved Hide resolved

if (s.hasChanged()) {
chunk.code = s.toString()
if (config.build.sourcemap && chunk.map) {
const nextMap = s.generateMap({
source: chunk.fileName,
hires: true
})
const map = combineSourcemaps(
chunk.fileName,
[nextMap as RawSourceMap, chunk.map as RawSourceMap],
false
) as SourceMap
map.toUrl = () => genSourceMapUrl(map)
chunk.map = map
}
}
}
}
}
Expand Down