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: missing js sourcemaps with rewritten imports broke debugging (#7767) #9476

Merged
merged 8 commits into from
Nov 22, 2022
6 changes: 6 additions & 0 deletions packages/vite/src/node/server/transformRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import getEtag from 'etag'
import * as convertSourceMap from 'convert-source-map'
import type { SourceDescription, SourceMap } from 'rollup'
import colors from 'picocolors'
import MagicString from 'magic-string'
import type { ViteDevServer } from '..'
import {
cleanUrl,
Expand Down Expand Up @@ -249,6 +250,11 @@ async function loadAndTransform(
isDebug && debugTransform(`${timeFrom(transformStart)} ${prettyUrl}`)
code = transformResult.code!
map = transformResult.map

// To enable debugging create a sourcemap for known modified JS files without one:
if (!map && mod.file && mod.type === 'js' && code !== originalCode) {
map = new MagicString(code).generateMap({ source: mod.file })
}
}

sapphi-red marked this conversation as resolved.
Show resolved Hide resolved
if (map && mod.file) {
Expand Down
20 changes: 19 additions & 1 deletion playground/js-sourcemap/__tests__/js-sourcemap.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,31 @@ import {
} from '~utils'

if (!isBuild) {
test('js', async () => {
test('js without import', async () => {
const res = await page.request.get(new URL('./foo.js', page.url()).href)
const js = await res.text()
const lines = js.split('\n')
expect(lines[lines.length - 1].includes('//')).toBe(false) // expect no sourcemap
})

test('js', async () => {
const res = await page.request.get(new URL('./qux.js', page.url()).href)
const js = await res.text()
const map = extractSourcemap(js)
expect(formatSourcemapForSnapshot(map)).toMatchInlineSnapshot(`
{
"mappings": "AAAA;AACA;AACA;",
"sources": [
"/root/qux.js",
],
"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
3 changes: 3 additions & 0 deletions playground/js-sourcemap/qux.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import foo from './foo'

export const qux = 'qux'