11<script setup lang="ts">
22import ansis from ' ansis'
33import { build } from ' ~/composables/bundler'
4- import { CONFIG_FILES , currentVersion , files , timeCost } from ' ~/state/bundler'
4+ import {
5+ CONFIG_FILES ,
6+ currentVersion ,
7+ files ,
8+ sourcemapEnabled ,
9+ timeCost ,
10+ } from ' ~/state/bundler'
511
612const { data : rolldownVersions } = await useRolldownVersions ()
713
@@ -73,6 +79,14 @@ const { data, status, error, refresh } = useAsyncData(
7379 const startTime = performance .now ()
7480
7581 try {
82+ // Ensure sourcemap is enabled if the state is set
83+ if (sourcemapEnabled .value && ! configObject .output ?.sourcemap ) {
84+ if (! configObject .output ) {
85+ configObject .output = {}
86+ }
87+ configObject .output .sourcemap = true
88+ }
89+
7690 const result = await build (core , files .value , entries , configObject )
7791 return result
7892 } finally {
@@ -82,7 +96,7 @@ const { data, status, error, refresh } = useAsyncData(
8296 { server: false , deep: false },
8397)
8498
85- watch ([files , currentVersion ], () => refresh (), {
99+ watch ([files , currentVersion , sourcemapEnabled ], () => refresh (), {
86100 deep: true ,
87101})
88102
@@ -110,6 +124,31 @@ const errorText = computed(() => {
110124 }
111125 return ` ${str }\n\n ${stack && str !== stack ? ` ${stack }\n ` : ' ' } `
112126})
127+
128+ // Helper function for UTF-8 encoding (needed for sourcemap visualization with unicode)
129+ const utf16ToUTF8 = (str : string ) => unescape (encodeURIComponent (str ))
130+
131+ const sourcemapLinks = computed (() => {
132+ if (! data .value ?.output || ! data .value ?.sourcemaps ) return {}
133+
134+ const links: Record <string , string > = {}
135+
136+ for (const [fileName, code] of Object .entries (data .value .output )) {
137+ const sourcemap = data .value .sourcemaps [fileName ]
138+ if (code && sourcemap ) {
139+ // Encode for source map visualization
140+ const encodedCode = utf16ToUTF8 (code )
141+ const encodedMap = utf16ToUTF8 (sourcemap )
142+ const hash = btoa (
143+ ` ${encodedCode .length }\0 ${encodedCode }${encodedMap .length }\0 ${encodedMap } ` ,
144+ )
145+ links [fileName ] =
146+ ` https://evanw.github.io/source-map-visualization/#${hash } `
147+ }
148+ }
149+
150+ return links
151+ })
113152 </script >
114153
115154<template >
@@ -133,14 +172,32 @@ const errorText = computed(() => {
133172 w-full
134173 flex-1
135174 >
136- <CodeEditor
137- :model-value =" data?.output[value] || ''"
138- language =" javascript"
139- readonly
140- min-h-0
141- w-full
142- flex-1
143- />
175+ <div min-h-0 w-full flex flex-1 flex-col >
176+ <CodeEditor
177+ :model-value =" data?.output[value] || ''"
178+ language =" javascript"
179+ readonly
180+ min-h-0
181+ w-full
182+ flex-1
183+ />
184+ <a
185+ v-if =" sourcemapLinks[value]"
186+ class =" m-2 flex items-center self-start text-sm opacity-80"
187+ :href =" sourcemapLinks[value]"
188+ target =" _blank"
189+ rel =" noopener"
190+ >
191+ <span
192+ class =" text-[#3c3c43] font-medium dark:text-[#fffff5]/[.86] hover:text-[#3451b2] dark:hover:text-[#a8b1ff]"
193+ >
194+ Visualize source map
195+ </span >
196+ <div
197+ class =" i-ri:arrow-right-up-line ml-1 h-3 w-3 text-[#3c3c43]/[.56] dark:text-[#fffff5]/[.6]"
198+ />
199+ </a >
200+ </div >
144201 </Tabs >
145202 <div
146203 v-if =" status === 'success' && data?.warnings?.length"
0 commit comments