Skip to content

Commit

Permalink
test: update test for inline sourcemap (#14597)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing authored Oct 14, 2023
1 parent 7861a33 commit 1243c2f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
7 changes: 3 additions & 4 deletions playground/js-sourcemap/__tests__/js-sourcemap.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { URL } from 'node:url'
import { describe, expect, test } from 'vitest'
import { mapFileCommentRegex } from 'convert-source-map'
import { commentSourceMap } from '../foo-with-sourcemap-plugin'
import {
extractSourcemap,
findAssetFile,
Expand Down Expand Up @@ -30,12 +31,13 @@ if (!isBuild) {
`)
})

test('js with existing inline sourcemap', async () => {
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,
)
const js = await res.text()

expect(js).toContain(commentSourceMap)
const sourcemapComments = js.match(mapFileCommentRegex).length
expect(sourcemapComments).toBe(1)

Expand All @@ -46,9 +48,6 @@ if (!isBuild) {
"sources": [
"",
],
"sourcesContent": [
null,
],
"version": 3,
}
`)
Expand Down
17 changes: 17 additions & 0 deletions playground/js-sourcemap/foo-with-sourcemap-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { Plugin } from 'vite'

export const commentSourceMap = [
'// default boundary sourcemap with magic-string',
'//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxNQUFNLENBQUMsS0FBSyxDQUFDLEdBQUcsQ0FBQyxDQUFDLENBQUMsQ0FBQyxHQUFHIn0=',
].join('\n')

export default function transformFooWithInlineSourceMap(): Plugin {
return {
name: 'transform-foo-with-inline-sourcemap',
transform(code, id) {
if (id.includes('foo-with-sourcemap.js')) {
return `${code}${commentSourceMap}`
}
},
}
}
4 changes: 0 additions & 4 deletions playground/js-sourcemap/foo-with-sourcemap.js
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
export const foo = 'foo'

// default boundary sourcemap with magic-string

//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxNQUFNLENBQUMsS0FBSyxDQUFDLEdBQUcsQ0FBQyxDQUFDLENBQUMsQ0FBQyxHQUFHIn0=
2 changes: 2 additions & 0 deletions playground/js-sourcemap/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { defineConfig } from 'vite'
import transformFooWithInlineSourceMap from './foo-with-sourcemap-plugin'

export default defineConfig({
plugins: [transformFooWithInlineSourceMap()],
build: {
sourcemap: true,
rollupOptions: {
Expand Down

0 comments on commit 1243c2f

Please sign in to comment.