diff --git a/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts b/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts index 14daa7654c08a5..b4e63e66290a74 100644 --- a/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts +++ b/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts @@ -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, } `) diff --git a/playground/js-sourcemap/foo-with-sourcemap-plugin.ts b/playground/js-sourcemap/foo-with-sourcemap-plugin.ts new file mode 100644 index 00000000000000..a181c39eaf6036 --- /dev/null +++ b/playground/js-sourcemap/foo-with-sourcemap-plugin.ts @@ -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}` + } + }, + } +} diff --git a/playground/js-sourcemap/foo-with-sourcemap.js b/playground/js-sourcemap/foo-with-sourcemap.js index 571667802fbf05..cb356468240d50 100644 --- a/playground/js-sourcemap/foo-with-sourcemap.js +++ b/playground/js-sourcemap/foo-with-sourcemap.js @@ -1,5 +1 @@ export const foo = 'foo' - -// default boundary sourcemap with magic-string - -//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxNQUFNLENBQUMsS0FBSyxDQUFDLEdBQUcsQ0FBQyxDQUFDLENBQUMsQ0FBQyxHQUFHIn0= diff --git a/playground/js-sourcemap/vite.config.js b/playground/js-sourcemap/vite.config.js index b56e74e40f3633..efebbc5ca00dee 100644 --- a/playground/js-sourcemap/vite.config.js +++ b/playground/js-sourcemap/vite.config.js @@ -1,6 +1,8 @@ import { defineConfig } from 'vite' +import transformFooWithInlineSourceMap from './foo-with-sourcemap-plugin' export default defineConfig({ + plugins: [transformFooWithInlineSourceMap()], build: { sourcemap: true, rollupOptions: {