-
-
Notifications
You must be signed in to change notification settings - Fork 366
feat: show a warning when significant size increase #1673
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e7e612d
feat: show a warning when significant size increase
43081j 219fe62
test: add a11y test
43081j 39e1194
chore: fix spelling
43081j 8f0c28f
chore: race condition fun
43081j 96f43bb
chore: fix em up
43081j 70486ca
fix: validate current install size staleness during navigation
danielroe 0e3f107
Merge branch 'main' into jg/we-have-to-go-bigger
danielroe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| <script setup lang="ts"> | ||
| import type { InstallSizeDiff } from '~/composables/useInstallSizeDiff' | ||
|
|
||
| const props = defineProps<{ | ||
| diff: InstallSizeDiff | ||
| }>() | ||
|
|
||
| const bytesFormatter = useBytesFormatter() | ||
| const numberFormatter = useNumberFormatter() | ||
|
|
||
| const sizePercent = computed(() => Math.round(props.diff.sizeRatio * 100)) | ||
| </script> | ||
|
|
||
| <template> | ||
| <div | ||
| class="border border-amber-600/40 bg-amber-500/10 rounded-lg px-3 py-2 text-base text-amber-800 dark:text-amber-400" | ||
| > | ||
| <h2 class="font-medium mb-1 flex items-center gap-2"> | ||
| <span class="i-lucide:trending-up w-4 h-4" aria-hidden="true" /> | ||
| {{ | ||
| diff.sizeThresholdExceeded && diff.depThresholdExceeded | ||
| ? $t('package.size_increase.title_both', { version: diff.comparisonVersion }) | ||
| : diff.sizeThresholdExceeded | ||
| ? $t('package.size_increase.title_size', { version: diff.comparisonVersion }) | ||
| : $t('package.size_increase.title_deps', { version: diff.comparisonVersion }) | ||
| }} | ||
| </h2> | ||
| <p class="text-sm m-0 mt-1"> | ||
| <i18n-t v-if="diff.sizeThresholdExceeded" keypath="package.size_increase.size" scope="global"> | ||
| <template #percent | ||
| ><strong>{{ sizePercent }}%</strong></template | ||
| > | ||
| <template #size | ||
| ><strong>{{ bytesFormatter.format(diff.sizeIncrease) }}</strong></template | ||
| > | ||
| </i18n-t> | ||
| <template v-if="diff.sizeThresholdExceeded && diff.depThresholdExceeded"> · </template> | ||
| <i18n-t v-if="diff.depThresholdExceeded" keypath="package.size_increase.deps" scope="global"> | ||
| <template #count | ||
| ><strong>+{{ numberFormatter.format(diff.depDiff) }}</strong></template | ||
| > | ||
| </i18n-t> | ||
| </p> | ||
| </div> | ||
| </template> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| import { compare, prerelease, valid } from 'semver' | ||
| import type { InstallSizeResult, SlimPackument } from '#shared/types' | ||
|
|
||
| export interface InstallSizeDiff { | ||
| comparisonVersion: string | ||
| sizeRatio: number | ||
| sizeIncrease: number | ||
| currentSize: number | ||
| previousSize: number | ||
| depDiff: number | ||
| currentDeps: number | ||
| previousDeps: number | ||
| sizeThresholdExceeded: boolean | ||
| depThresholdExceeded: boolean | ||
| } | ||
|
|
||
| const SIZE_INCREASE_THRESHOLD = 0.25 | ||
| const DEP_INCREASE_THRESHOLD = 5 | ||
|
|
||
| function getComparisonVersion(pkg: SlimPackument, resolvedVersion: string): string | null { | ||
| const isCurrentPrerelease = prerelease(resolvedVersion) !== null | ||
|
|
||
| if (isCurrentPrerelease) { | ||
| const latest = pkg['dist-tags']?.latest | ||
| if (!latest || latest === resolvedVersion) return null | ||
| return latest | ||
| } | ||
|
|
||
| // Find the previous version in time that was stable | ||
| const stableVersions = Object.keys(pkg.time) | ||
| .filter(v => v !== 'modified' && v !== 'created' && valid(v) !== null && prerelease(v) === null) | ||
| .sort((a, b) => compare(a, b)) | ||
|
|
||
| const currentIdx = stableVersions.indexOf(resolvedVersion) | ||
| if (currentIdx <= 0) return null | ||
|
|
||
| return stableVersions[currentIdx - 1]! | ||
| } | ||
|
|
||
| export function useInstallSizeDiff( | ||
| packageName: MaybeRefOrGetter<string>, | ||
| resolvedVersion: MaybeRefOrGetter<string | null | undefined>, | ||
| pkg: MaybeRefOrGetter<SlimPackument | null | undefined>, | ||
| currentInstallSize: MaybeRefOrGetter<InstallSizeResult | null | undefined>, | ||
| ) { | ||
| const comparisonVersion = computed<string | null>(() => { | ||
| const pkgVal = toValue(pkg) | ||
| const version = toValue(resolvedVersion) | ||
| if (!pkgVal || !version) return null | ||
| return getComparisonVersion(pkgVal, version) | ||
| }) | ||
|
|
||
| const { | ||
| data: comparisonInstallSize, | ||
| status: comparisonStatus, | ||
| execute: fetchComparisonSize, | ||
| } = useLazyFetch<InstallSizeResult | null>( | ||
| () => { | ||
| const v = comparisonVersion.value | ||
| if (!v) return '' | ||
| return `/api/registry/install-size/${toValue(packageName)}/v/${v}` | ||
| }, | ||
| { | ||
| server: false, | ||
| immediate: false, | ||
| default: () => null, | ||
| }, | ||
| ) | ||
|
|
||
| if (import.meta.client) { | ||
| watch( | ||
| [comparisonVersion, () => toValue(packageName)], | ||
| ([v]) => { | ||
| if (v) fetchComparisonSize() | ||
| }, | ||
| { immediate: true }, | ||
| ) | ||
| } | ||
|
|
||
| const diff = computed<InstallSizeDiff | null>(() => { | ||
| const current = toValue(currentInstallSize) | ||
| const previous = comparisonInstallSize.value | ||
| const cv = comparisonVersion.value | ||
|
|
||
| if (!current || !previous || !cv) return null | ||
| const name = toValue(packageName) | ||
| const version = toValue(resolvedVersion) | ||
| if (previous.version !== cv || previous.package !== name) return null | ||
| if (current.version !== version || current.package !== name) return null | ||
|
|
||
| const sizeRatio = | ||
| previous.totalSize > 0 ? (current.totalSize - previous.totalSize) / previous.totalSize : 0 | ||
| const depDiff = current.dependencyCount - previous.dependencyCount | ||
|
|
||
| const sizeThresholdExceeded = sizeRatio > SIZE_INCREASE_THRESHOLD | ||
| const depThresholdExceeded = depDiff > DEP_INCREASE_THRESHOLD | ||
43081j marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if (!sizeThresholdExceeded && !depThresholdExceeded) return null | ||
|
|
||
| return { | ||
| comparisonVersion: cv, | ||
| sizeRatio, | ||
| sizeIncrease: current.totalSize - previous.totalSize, | ||
| currentSize: current.totalSize, | ||
| previousSize: previous.totalSize, | ||
| depDiff, | ||
| currentDeps: current.dependencyCount, | ||
| previousDeps: previous.dependencyCount, | ||
| sizeThresholdExceeded, | ||
| depThresholdExceeded, | ||
| } | ||
| }) | ||
|
|
||
| return { diff, comparisonVersion, comparisonStatus } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| export interface DependencySize { | ||
| name: string | ||
| version: string | ||
| size: number | ||
| /** True if this is an optional dependency */ | ||
| optional?: boolean | ||
| } | ||
|
|
||
| export interface InstallSizeResult { | ||
| /** Package name */ | ||
| package: string | ||
| /** Package version */ | ||
| version: string | ||
| /** Unpacked size of the package itself (bytes) */ | ||
| selfSize: number | ||
| /** Total unpacked size including all dependencies (bytes) */ | ||
| totalSize: number | ||
| /** Number of dependencies (including transitive) */ | ||
| dependencyCount: number | ||
| /** Breakdown of dependency sizes */ | ||
| dependencies: DependencySize[] | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.