Skip to content

Commit 13fb7f5

Browse files
authored
Revert "fix: replace version in generateBundle (#12700)" (#12779)
* Revert "fix: replace version in generateBundle (#12700)" This reverts commit 3591411. #12700 did introduce a bug where the file name stays the same, but the contents of a file can change because of a changed version. Revert for now and take another look at tackling this later. Fixes #12771 Reopens #12260 * changeset
1 parent 793c00e commit 13fb7f5

File tree

3 files changed

+11
-72
lines changed

3 files changed

+11
-72
lines changed

.changeset/large-planes-tan.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
fix: revert change to replace version in generateBundle

packages/kit/src/constants.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,3 @@ export const GENERATED_COMMENT = '// this file is generated — do not edit it\n
99
export const ENDPOINT_METHODS = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD'];
1010

1111
export const PAGE_METHODS = ['GET', 'POST', 'HEAD'];
12-
13-
/**
14-
* Placeholders for the hash of the app version.
15-
* Later replaced in the generateBundle hook to avoid affecting the chunk hash.
16-
*/
17-
export const APP_VERSION_HASH_PLACEHOLDER_BASE = '__SVELTEKIT_APP_VERSION_HASH__';
18-
19-
/**
20-
* Placeholder for the app version.
21-
* Later replaced in the generateBundle hook to avoid affecting the chunk hash.
22-
*/
23-
export const APP_VERSION_PLACEHOLDER_BASE = '__SVELTEKIT_APP_VERSION__';

packages/kit/src/exports/vite/index.js

Lines changed: 6 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ import {
3535
sveltekit_server
3636
} from './module_ids.js';
3737
import { resolve_peer_dependency } from '../../utils/import.js';
38-
import {
39-
APP_VERSION_PLACEHOLDER_BASE,
40-
APP_VERSION_HASH_PLACEHOLDER_BASE
41-
} from '../../constants.js';
4238

4339
const cwd = process.cwd();
4440

@@ -188,16 +184,7 @@ async function kit({ svelte_config }) {
188184
const { kit } = svelte_config;
189185
const out = `${kit.outDir}/output`;
190186

191-
const app_version = kit.version.name;
192-
const version_hash = hash(app_version);
193-
194-
// if the app version or hash is longer than the placeholder, we need to pad it to avoid
195-
// source map damage
196-
const app_version_placeholder = APP_VERSION_PLACEHOLDER_BASE.padEnd(app_version.length, '_');
197-
const app_version_hash_placeholder = APP_VERSION_HASH_PLACEHOLDER_BASE.padEnd(
198-
version_hash.length,
199-
'_'
200-
);
187+
const version_hash = hash(kit.version.name);
201188

202189
/** @type {import('vite').ResolvedConfig} */
203190
let vite_config;
@@ -397,7 +384,7 @@ async function kit({ svelte_config }) {
397384
const browser = !options?.ssr;
398385

399386
const global = is_build
400-
? `globalThis.__sveltekit_${browser ? app_version_hash_placeholder : version_hash}`
387+
? `globalThis.__sveltekit_${version_hash}`
401388
: 'globalThis.__sveltekit_dev';
402389

403390
if (options?.ssr === false && process.env.TEST !== 'true') {
@@ -483,8 +470,10 @@ async function kit({ svelte_config }) {
483470
}
484471

485472
case sveltekit_environment: {
473+
const { version } = svelte_config.kit;
474+
486475
return dedent`
487-
export const version = ${is_build && browser ? app_version_placeholder : s(kit.version.name)};
476+
export const version = ${s(version.name)};
488477
export let building = false;
489478
export let prerendering = false;
490479
@@ -934,50 +923,7 @@ async function kit({ svelte_config }) {
934923
}
935924
};
936925

937-
/** @type {import('vite').Plugin} */
938-
const plugin_replace_version_and_hash = {
939-
name: 'vite-plugin-svelte-replace-version-and-hash',
940-
enforce: 'post',
941-
942-
generateBundle(_, bundle, __) {
943-
if (vite_config.build.ssr) return;
944-
945-
for (const file in bundle) {
946-
if (bundle[file].type !== 'chunk') continue;
947-
const chunk = /** @type {import('rollup').OutputChunk} */ (bundle[file]);
948-
let code = chunk.code;
949-
if (
950-
!(code.includes(app_version_placeholder) || code.includes(app_version_hash_placeholder))
951-
)
952-
continue;
953-
954-
// replace the version and version after the chunk hash has already been calculated
955-
// to avoid affecting the chunk hash
956-
const substitutions = [
957-
[app_version_hash_placeholder, version_hash],
958-
[app_version_placeholder, JSON.stringify(kit.version.name)]
959-
];
960-
961-
for (const [placeholder, replacement] of substitutions) {
962-
code = code.replaceAll(
963-
placeholder,
964-
// pad the replacement to mitigate source map changes
965-
replacement.padEnd(placeholder.length, ' ')
966-
);
967-
}
968-
969-
chunk.code = code;
970-
}
971-
}
972-
};
973-
974-
return [
975-
plugin_setup,
976-
plugin_virtual_modules,
977-
plugin_guard,
978-
plugin_compile,
979-
plugin_replace_version_and_hash
980-
];
926+
return [plugin_setup, plugin_virtual_modules, plugin_guard, plugin_compile];
981927
}
982928

983929
/**

0 commit comments

Comments
 (0)