Skip to content

Commit

Permalink
fix(sourcemap): sourcemap is incorrect when sourcemap has `sources: […
Browse files Browse the repository at this point in the history
…null]` (#14588)

Co-authored-by: 翠 / green <green@sapphi.red>
  • Loading branch information
Dunqing and sapphi-red committed Jan 8, 2024
1 parent fdbe04d commit f8c6a34
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 3 deletions.
15 changes: 14 additions & 1 deletion packages/vite/src/node/server/pluginContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
21 changes: 21 additions & 0 deletions playground/js-sourcemap/__tests__/js-sourcemap.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions playground/js-sourcemap/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ <h1>JS Sourcemap</h1>
<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>
3 changes: 2 additions & 1 deletion playground/js-sourcemap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 change: 1 addition & 0 deletions playground/js-sourcemap/plugin-foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 'foo'
6 changes: 5 additions & 1 deletion playground/js-sourcemap/vite.config.js
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down
18 changes: 18 additions & 0 deletions playground/js-sourcemap/zoo-with-sourcemap-plugin.ts
Original file line number Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions playground/js-sourcemap/zoo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const zoo = 'zoo'
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit f8c6a34

Please sign in to comment.