diff options
author | 2024-11-01 16:01:19 +0800 | |
---|---|---|
committer | 2024-11-01 16:01:19 +0800 | |
commit | 20e5a843c86e9328814615edf3e8a6fb5e4696cc (patch) | |
tree | 6f698ef9cdb12e835bdf3f4dd7e58c81eaeda531 | |
parent | 836cd91c37cea8ae58dd04a326435fcb2c88f358 (diff) | |
download | astro-20e5a843c86e9328814615edf3e8a6fb5e4696cc.tar.gz astro-20e5a843c86e9328814615edf3e8a6fb5e4696cc.tar.zst astro-20e5a843c86e9328814615edf3e8a6fb5e4696cc.zip |
Fix prefetch sourcemap generation (#12346)
-rw-r--r-- | .changeset/heavy-walls-rhyme.md | 5 | ||||
-rw-r--r-- | packages/astro/src/prefetch/vite-plugin-prefetch.ts | 22 |
2 files changed, 21 insertions, 6 deletions
diff --git a/.changeset/heavy-walls-rhyme.md b/.changeset/heavy-walls-rhyme.md new file mode 100644 index 000000000..138e548a2 --- /dev/null +++ b/.changeset/heavy-walls-rhyme.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes sourcemap generation when prefetch is enabled diff --git a/packages/astro/src/prefetch/vite-plugin-prefetch.ts b/packages/astro/src/prefetch/vite-plugin-prefetch.ts index d64c6d500..67e6df79d 100644 --- a/packages/astro/src/prefetch/vite-plugin-prefetch.ts +++ b/packages/astro/src/prefetch/vite-plugin-prefetch.ts @@ -45,15 +45,25 @@ export default function astroPrefetch({ settings }: { settings: AstroSettings }) }, transform(code, id) { // NOTE: Handle replacing the specifiers even if prefetch is disabled so View Transitions - // can import the internal module as not hit runtime issues. + // can import the internal module and not hit runtime issues. if (id.includes(prefetchInternalModuleFsSubpath)) { - return code - .replace('__PREFETCH_PREFETCH_ALL__', JSON.stringify(prefetch?.prefetchAll)) - .replace('__PREFETCH_DEFAULT_STRATEGY__', JSON.stringify(prefetch?.defaultStrategy)) + // We perform a simple replacement with padding so that the code offset is not changed and + // we don't have to generate a sourcemap. This has the assumption that the replaced string + // will always be shorter than the search string to work. + code = code .replace( - '__EXPERIMENTAL_CLIENT_PRERENDER__', - JSON.stringify(settings.config.experimental.clientPrerender), + '__PREFETCH_PREFETCH_ALL__', // length: 25 + `${JSON.stringify(prefetch?.prefetchAll)}`.padEnd(25), + ) + .replace( + '__PREFETCH_DEFAULT_STRATEGY__', // length: 29 + `${JSON.stringify(prefetch?.defaultStrategy)}`.padEnd(29), + ) + .replace( + '__EXPERIMENTAL_CLIENT_PRERENDER__', // length: 33 + `${JSON.stringify(settings.config.experimental.clientPrerender)}`.padEnd(33), ); + return { code, map: null }; } }, }; |