fix(sourcemap): sourcemap is incorrect when sourcemap has sources: [null] (#14588)

Co-authored-by: 翠 / green <green@sapphi.red>
This commit is contained in:
Dunqing 2024-01-08 22:17:12 +08:00 committed by GitHub
parent fdbe04d3d7
commit f8c6a341fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 66 additions and 3 deletions

View File

@ -600,7 +600,20 @@ export async function createPluginContainer(
break
}
if (!combinedMap) {
combinedMap = m as SourceMap
const sm = m as SourceMap
// sourcemap should not include `sources: [null]` (because `sources` should be string) nor
// `sources: ['']` (because `''` means the path of sourcemap)
// but MagicString generates this when `filename` option is not set.
// Rollup supports these and therefore we support this as well
if (sm.sources.length === 1 && !sm.sources[0]) {
combinedMap = {
...sm,
sources: [this.filename],
sourcesContent: [this.originalCode],
}
} else {
combinedMap = sm
}
} else {
combinedMap = combineSourcemaps(cleanUrl(this.filename), [
m as RawSourceMap,

View File

@ -31,6 +31,27 @@ if (!isBuild) {
`)
})
test('plugin return sourcemap with `sources: [""]`', async () => {
const res = await page.request.get(new URL('./zoo.js', page.url()).href)
const js = await res.text()
expect(js).toContain('// add comment')
const map = extractSourcemap(js)
expect(formatSourcemapForSnapshot(map)).toMatchInlineSnapshot(`
{
"mappings": "AAAA,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;",
"sources": [
"zoo.js",
],
"sourcesContent": [
"export const zoo = 'zoo'
",
],
"version": 3,
}
`)
})
test('js with inline sourcemap injected by a plugin', async () => {
const res = await page.request.get(
new URL('./foo-with-sourcemap.js', page.url()).href,

View File

@ -7,3 +7,4 @@
<script type="module" src="./bar.ts"></script>
<script type="module" src="./after-preload-dynamic.js"></script>
<script type="module" src="./with-multiline-import.ts"></script>
<script type="module" src="./zoo.js"></script>

View File

@ -10,6 +10,7 @@
"preview": "vite preview"
},
"dependencies": {
"@vitejs/test-importee-pkg": "file:importee-pkg"
"@vitejs/test-importee-pkg": "file:importee-pkg",
"magic-string": "^0.30.5"
}
}

View File

@ -0,0 +1 @@
export const foo = 'foo'

View File

@ -1,8 +1,12 @@
import { defineConfig } from 'vite'
import transformFooWithInlineSourceMap from './foo-with-sourcemap-plugin'
import { transformZooWithSourcemapPlugin } from './zoo-with-sourcemap-plugin'
export default defineConfig({
plugins: [transformFooWithInlineSourceMap()],
plugins: [
transformFooWithInlineSourceMap(),
transformZooWithSourcemapPlugin(),
],
build: {
sourcemap: true,
rollupOptions: {

View File

@ -0,0 +1,18 @@
import MagicString from 'magic-string'
import type { Plugin } from 'vite'
export const transformZooWithSourcemapPlugin: () => Plugin = () => ({
name: 'sourcemap',
transform(code, id) {
if (id.includes('zoo.js')) {
const ms = new MagicString(code)
ms.append('// add comment')
return {
code: ms.toString(),
// NOTE: MagicString without `filename` option generates
// a sourcemap with `sources: ['']` or `sources: [null]`
map: ms.generateMap({ hires: true }),
}
}
},
})

View File

@ -0,0 +1 @@
export const zoo = 'zoo'

View File

@ -700,6 +700,9 @@ importers:
'@vitejs/test-importee-pkg':
specifier: file:importee-pkg
version: file:playground/js-sourcemap/importee-pkg
magic-string:
specifier: ^0.30.5
version: 0.30.5
playground/js-sourcemap/importee-pkg: {}