Skip to content

Commit

Permalink
Revert "fix: replace version in generateBundle (#12700)" (#12779)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
dummdidumm authored Oct 9, 2024
1 parent 793c00e commit 13fb7f5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 72 deletions.
5 changes: 5 additions & 0 deletions .changeset/large-planes-tan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: revert change to replace version in generateBundle
12 changes: 0 additions & 12 deletions packages/kit/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,3 @@ export const GENERATED_COMMENT = '// this file is generated — do not edit it\n
export const ENDPOINT_METHODS = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD'];

export const PAGE_METHODS = ['GET', 'POST', 'HEAD'];

/**
* Placeholders for the hash of the app version.
* Later replaced in the generateBundle hook to avoid affecting the chunk hash.
*/
export const APP_VERSION_HASH_PLACEHOLDER_BASE = '__SVELTEKIT_APP_VERSION_HASH__';

/**
* Placeholder for the app version.
* Later replaced in the generateBundle hook to avoid affecting the chunk hash.
*/
export const APP_VERSION_PLACEHOLDER_BASE = '__SVELTEKIT_APP_VERSION__';
66 changes: 6 additions & 60 deletions packages/kit/src/exports/vite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ import {
sveltekit_server
} from './module_ids.js';
import { resolve_peer_dependency } from '../../utils/import.js';
import {
APP_VERSION_PLACEHOLDER_BASE,
APP_VERSION_HASH_PLACEHOLDER_BASE
} from '../../constants.js';

const cwd = process.cwd();

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

const app_version = kit.version.name;
const version_hash = hash(app_version);

// if the app version or hash is longer than the placeholder, we need to pad it to avoid
// source map damage
const app_version_placeholder = APP_VERSION_PLACEHOLDER_BASE.padEnd(app_version.length, '_');
const app_version_hash_placeholder = APP_VERSION_HASH_PLACEHOLDER_BASE.padEnd(
version_hash.length,
'_'
);
const version_hash = hash(kit.version.name);

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

const global = is_build
? `globalThis.__sveltekit_${browser ? app_version_hash_placeholder : version_hash}`
? `globalThis.__sveltekit_${version_hash}`
: 'globalThis.__sveltekit_dev';

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

case sveltekit_environment: {
const { version } = svelte_config.kit;

return dedent`
export const version = ${is_build && browser ? app_version_placeholder : s(kit.version.name)};
export const version = ${s(version.name)};
export let building = false;
export let prerendering = false;
Expand Down Expand Up @@ -934,50 +923,7 @@ async function kit({ svelte_config }) {
}
};

/** @type {import('vite').Plugin} */
const plugin_replace_version_and_hash = {
name: 'vite-plugin-svelte-replace-version-and-hash',
enforce: 'post',

generateBundle(_, bundle, __) {
if (vite_config.build.ssr) return;

for (const file in bundle) {
if (bundle[file].type !== 'chunk') continue;
const chunk = /** @type {import('rollup').OutputChunk} */ (bundle[file]);
let code = chunk.code;
if (
!(code.includes(app_version_placeholder) || code.includes(app_version_hash_placeholder))
)
continue;

// replace the version and version after the chunk hash has already been calculated
// to avoid affecting the chunk hash
const substitutions = [
[app_version_hash_placeholder, version_hash],
[app_version_placeholder, JSON.stringify(kit.version.name)]
];

for (const [placeholder, replacement] of substitutions) {
code = code.replaceAll(
placeholder,
// pad the replacement to mitigate source map changes
replacement.padEnd(placeholder.length, ' ')
);
}

chunk.code = code;
}
}
};

return [
plugin_setup,
plugin_virtual_modules,
plugin_guard,
plugin_compile,
plugin_replace_version_and_hash
];
return [plugin_setup, plugin_virtual_modules, plugin_guard, plugin_compile];
}

/**
Expand Down

0 comments on commit 13fb7f5

Please sign in to comment.