Skip to content

Commit

Permalink
fix: update sourcemap in importAnalysisBuild (#7825)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Apr 21, 2022
1 parent e29d1d9 commit d7540c8
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 17 deletions.
61 changes: 48 additions & 13 deletions packages/vite/src/node/plugins/importAnalysisBuild.ts
Expand Up @@ -4,10 +4,12 @@ import type { Plugin } from '../plugin'
import MagicString from 'magic-string'
import type { ImportSpecifier } from 'es-module-lexer'
import { init, parse as parseImports } from 'es-module-lexer'
import type { OutputChunk } from 'rollup'
import type { OutputChunk, SourceMap } from 'rollup'
import { isCSSRequest, removedPureCssFilesCache } from './css'
import { transformImportGlob } from '../importGlob'
import { bareImportRE } from '../utils'
import { bareImportRE, combineSourcemaps } from '../utils'
import type { RawSourceMap } from '@ampproject/remapping'
import { genSourceMapUrl } from '../server/sourcemap'

/**
* A flag for injected helpers. This flag will be set to `false` if the output
Expand All @@ -20,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 @@ -263,8 +265,10 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
this.error(e, e.idx)
}

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 @@ -324,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 @@ -343,15 +347,46 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
: `[]`,
{ contentOnly: true }
)
rewroteMarkerStartPos.add(markerStartPos)
}
}
chunk.code = s.toString()
// TODO source map
}

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

markerStartPos = code.indexOf(
preloadMarkerWithQuote,
markerStartPos + preloadMarkerWithQuote.length
)
}

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
2 changes: 1 addition & 1 deletion packages/vite/src/node/server/sourcemap.ts
Expand Up @@ -61,7 +61,7 @@ export async function injectSourcesContent(
}
}

function genSourceMapUrl(map: SourceMap | string | undefined) {
export function genSourceMapUrl(map: SourceMap | string | undefined) {
if (typeof map !== 'string') {
map = JSON.stringify(map)
}
Expand Down
7 changes: 4 additions & 3 deletions packages/vite/src/node/utils.ts
Expand Up @@ -605,7 +605,8 @@ const nullSourceMap: RawSourceMap = {
}
export function combineSourcemaps(
filename: string,
sourcemapList: Array<DecodedSourceMap | RawSourceMap>
sourcemapList: Array<DecodedSourceMap | RawSourceMap>,
excludeContent = true
): RawSourceMap {
if (
sourcemapList.length === 0 ||
Expand Down Expand Up @@ -635,7 +636,7 @@ export function combineSourcemaps(
const useArrayInterface =
sourcemapList.slice(0, -1).find((m) => m.sources.length !== 1) === undefined
if (useArrayInterface) {
map = remapping(sourcemapList, () => null, true)
map = remapping(sourcemapList, () => null, excludeContent)
} else {
map = remapping(
sourcemapList[0],
Expand All @@ -646,7 +647,7 @@ export function combineSourcemaps(
return null
}
},
true
excludeContent
)
}
if (!map.file) {
Expand Down

0 comments on commit d7540c8

Please sign in to comment.