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(sourcemap): sourcemap is incorrect when sourcemap has sources: [null] #14588

Merged
merged 15 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -586,7 +586,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]) {
Dunqing marked this conversation as resolved.
Show resolved Hide resolved
combinedMap = {
...sm,
sources: [this.filename],
sourcesContent: [this.originalCode],
}
} else {
combinedMap = sm
}
} else {
combinedMap = combineSourcemaps(cleanUrl(this.filename), [
m as RawSourceMap,
Comment on lines 604 to 605
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does combineSourcemaps work without doing the same normalization (i.e. setting [this.filename]) above? This happens when a module is transformed by a plugin then transformed by another plugin that uses MagicString.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sources are absolutely present when entering here Because the first source map has added sources or the sources themselves exist

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

combineSourcemaps has two inputs. The second one (combinedMap) wouldn't have sources: ['']. But the first one (m) might have sources: [''], no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the second one definitely have

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've actually tested it now by changing the code like below and renaming zoo.js to zoo.ts.

          console.log('Before', m, combinedMap)
          combinedMap = combineSourcemaps(cleanUrl(this.filename), [
            m as RawSourceMap,
            combinedMap as RawSourceMap,
          ]) as SourceMap
          console.log('After', combinedMap)

I got the following result:

Before SourceMap {
  version: 3,
  file: undefined,
  sources: [ '' ],
  sourcesContent: undefined,
  names: [],
  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,CAAC;'
} {
  version: 3,
  sources: [ 'D:/documents/GitHub/vite/playground/js-sourcemap/zoo.ts' ],
  sourcesContent: [ "export const zoo: string = 'zoo'\n" ],
  mappings: 'AAAO,aAAM,MAAc;',
  names: []
}
After SourceMap {
  version: 3,
  mappings: 'AAAO,aAAM,MAAc',
  names: [],
  sourceRoot: undefined,
  sources: [ 'D:/documents/GitHub/vite/playground/js-sourcemap/zoo.ts' ],
  sourcesContent: [ "export const zoo: string = 'zoo'\n" ],
  file: 'D:/documents/GitHub/vite/playground/js-sourcemap/zoo.ts'
}

It seems it works at least for this case.

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.