mirror of
https://github.com/vitejs/vite.git
synced 2024-11-21 14:48:41 +00:00
fix(sourcemap): sourcemap is incorrect when sourcemap has sources: [null]
(#14588)
Co-authored-by: 翠 / green <green@sapphi.red>
This commit is contained in:
parent
fdbe04d3d7
commit
f8c6a341fb
@ -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,
|
||||
|
@ -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,
|
||||
|
@ -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>
|
||||
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
1
playground/js-sourcemap/plugin-foo.js
Normal file
1
playground/js-sourcemap/plugin-foo.js
Normal file
@ -0,0 +1 @@
|
||||
export const foo = 'foo'
|
@ -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: {
|
||||
|
18
playground/js-sourcemap/zoo-with-sourcemap-plugin.ts
Normal file
18
playground/js-sourcemap/zoo-with-sourcemap-plugin.ts
Normal 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 }),
|
||||
}
|
||||
}
|
||||
},
|
||||
})
|
1
playground/js-sourcemap/zoo.js
Normal file
1
playground/js-sourcemap/zoo.js
Normal file
@ -0,0 +1 @@
|
||||
export const zoo = 'zoo'
|
@ -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: {}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user