Skip to content

Commit

Permalink
fix(sourcemap): dont inject fallback sourcemap if have existing (#14370)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Sep 15, 2023
1 parent 6e0b0ee commit 55a3b4f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/vite/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ function createCjsConfig(isProduction: boolean) {
...Object.keys(pkg.dependencies),
...(isProduction ? [] : Object.keys(pkg.devDependencies)),
],
plugins: [...createNodePlugins(false, false, false), bundleSizeLimit(155)],
plugins: [...createNodePlugins(false, false, false), bundleSizeLimit(161)],
})
}

Expand Down
22 changes: 17 additions & 5 deletions packages/vite/src/node/server/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ import type {
ServerResponse,
} from 'node:http'
import path from 'node:path'
import convertSourceMap from 'convert-source-map'
import getEtag from 'etag'
import type { SourceMap } from 'rollup'
import MagicString from 'magic-string'
import { removeTimestampQuery } from '../utils'
import { createDebugger, removeTimestampQuery } from '../utils'
import { getCodeWithSourcemap } from './sourcemap'

const debug = createDebugger('vite:send', {
onlyWhenFocused: true,
})

const alias: Record<string, string | undefined> = {
js: 'application/javascript',
css: 'text/css',
Expand Down Expand Up @@ -63,13 +68,20 @@ export function send(
if (type === 'js' || type === 'css') {
content = getCodeWithSourcemap(type, content.toString(), map)
}
} else {
if (type === 'js' && (!map || map.mappings !== '')) {
}
// inject fallback sourcemap for js for improved debugging
// https://github.com/vitejs/vite/pull/13514#issuecomment-1592431496
else if (type === 'js' && (!map || map.mappings !== '')) {
const code = content.toString()
// if the code has existing inline sourcemap, assume it's correct and skip
if (convertSourceMap.mapFileCommentRegex.test(code)) {
debug?.(`Skipped injecting fallback sourcemap for ${req.url}`)
} else {
const urlWithoutTimestamp = removeTimestampQuery(req.url!)
const ms = new MagicString(content.toString())
const ms = new MagicString(code)
content = getCodeWithSourcemap(
type,
content.toString(),
code,
ms.generateMap({
source: path.basename(urlWithoutTimestamp),
hires: 'boundary',
Expand Down
25 changes: 25 additions & 0 deletions playground/js-sourcemap/__tests__/js-sourcemap.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { URL } from 'node:url'
import { describe, expect, test } from 'vitest'
import { mapFileCommentRegex } from 'convert-source-map'
import {
extractSourcemap,
findAssetFile,
Expand Down Expand Up @@ -29,6 +30,30 @@ if (!isBuild) {
`)
})

test('js with existing inline sourcemap', async () => {
const res = await page.request.get(
new URL('./foo-with-sourcemap.js', page.url()).href,
)
const js = await res.text()

const sourcemapComments = js.match(mapFileCommentRegex).length
expect(sourcemapComments).toBe(1)

const map = extractSourcemap(js)
expect(formatSourcemapForSnapshot(map)).toMatchInlineSnapshot(`
{
"mappings": "AAAA,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG",
"sources": [
"",
],
"sourcesContent": [
null,
],
"version": 3,
}
`)
})

test('ts', async () => {
const res = await page.request.get(new URL('./bar.ts', page.url()).href)
const js = await res.text()
Expand Down
5 changes: 5 additions & 0 deletions playground/js-sourcemap/foo-with-sourcemap.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 55a3b4f

Please sign in to comment.