test: update test for inline sourcemap (#14597)

This commit is contained in:
Dunqing 2023-10-14 05:53:21 -05:00 committed by GitHub
parent 7861a337f6
commit 1243c2f1cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 8 deletions

View File

@ -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,
@ -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)
@ -46,9 +48,6 @@ if (!isBuild) {
"sources": [
"",
],
"sourcesContent": [
null,
],
"version": 3,
}
`)

View File

@ -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}`
}
},
}
}

View File

@ -1,5 +1 @@
export const foo = 'foo'
// default boundary sourcemap with magic-string
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxNQUFNLENBQUMsS0FBSyxDQUFDLEdBQUcsQ0FBQyxDQUFDLENBQUMsQ0FBQyxHQUFHIn0=

View File

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