-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
fix: replace version in generateBundle #12700
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
1355af3
5d33d85
474f425
aa5ad52
79836ef
5345836
fbc5cd4
1bc9d80
7815107
fb26894
afae019
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@sveltejs/kit': patch | ||
--- | ||
|
||
fix: replace version in generateBundle | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,6 @@ 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']; | ||
|
||
export const SUBSTITUTION_APP_VERSION_HASH = '__SVELTEKIT_APP_VERSION_HASH__'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These two constants could use a jsdoc comment explaining a bit why we need them (basically a trimmed down version of the PR description) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. addressed in |
||
export const SUBSTITUTION_APP_VERSION = '__SVELTEKIT_APP_VERSION__'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,7 @@ import { | |
sveltekit_server | ||
} from './module_ids.js'; | ||
import { resolve_peer_dependency } from '../../utils/import.js'; | ||
import { SUBSTITUTION_APP_VERSION, SUBSTITUTION_APP_VERSION_HASH } from '../../constants.js'; | ||
|
||
const cwd = process.cwd(); | ||
|
||
|
@@ -384,7 +385,7 @@ async function kit({ svelte_config }) { | |
const browser = !options?.ssr; | ||
|
||
const global = is_build | ||
? `globalThis.__sveltekit_${version_hash}` | ||
? `globalThis.__sveltekit_${browser ? SUBSTITUTION_APP_VERSION_HASH : version_hash}` | ||
: 'globalThis.__sveltekit_dev'; | ||
|
||
if (options?.ssr === false && process.env.TEST !== 'true') { | ||
|
@@ -470,10 +471,8 @@ async function kit({ svelte_config }) { | |
} | ||
|
||
case sveltekit_environment: { | ||
const { version } = svelte_config.kit; | ||
|
||
return dedent` | ||
export const version = ${s(version.name)}; | ||
export const version = ${s(is_build ? SUBSTITUTION_APP_VERSION : kit.version.name)}; | ||
export let building = false; | ||
export let prerendering = false; | ||
|
||
|
@@ -923,7 +922,44 @@ async function kit({ svelte_config }) { | |
} | ||
}; | ||
|
||
return [plugin_setup, plugin_virtual_modules, plugin_guard, plugin_compile]; | ||
/** @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; | ||
if ( | ||
!( | ||
bundle[file].code.includes(SUBSTITUTION_APP_VERSION) || | ||
bundle[file].code.includes(SUBSTITUTION_APP_VERSION_HASH) | ||
) | ||
) | ||
continue; | ||
|
||
// replace the version after the chunk hash has already been calculated | ||
const code = bundle[file].code | ||
.replaceAll( | ||
SUBSTITUTION_APP_VERSION_HASH, | ||
version_hash.padEnd(SUBSTITUTION_APP_VERSION_HASH.length, ' ') | ||
) | ||
.replaceAll(SUBSTITUTION_APP_VERSION, JSON.stringify(kit.version.name).slice(1, -1)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any danger of this messing with source map positions? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is, tried to mitigate that for the hash with the padding but doing so for the version would change the version string. Could also match the surrounding quotes and add the padding after those. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will padding suffice? i.e. will the replacement always be smaller than the placeholder? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed now I think, |
||
|
||
bundle[file].code = code; | ||
} | ||
} | ||
}; | ||
|
||
return [ | ||
plugin_setup, | ||
plugin_virtual_modules, | ||
plugin_guard, | ||
plugin_compile, | ||
plugin_replace_version_and_hash | ||
]; | ||
} | ||
|
||
/** | ||
|
Uh oh!
There was an error while loading. Please reload this page.