-
Notifications
You must be signed in to change notification settings - Fork 241
[Feature] report file size for ui extensions on build and dev #7205
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
Open
robin-drexler
wants to merge
1
commit into
main
Choose a base branch
from
rd/bundle-size-reporting
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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,5 @@ | ||
| --- | ||
| '@shopify/app': minor | ||
| --- | ||
|
|
||
| report file size for extensions on build and dev |
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,81 @@ | ||
| import {getBundleSize, formatBundleSize} from './bundle-size.js' | ||
| import {describe, expect, test, vi} from 'vitest' | ||
| import {readFile} from '@shopify/cli-kit/node/fs' | ||
| import {deflate} from 'node:zlib' | ||
| import {promisify} from 'node:util' | ||
|
|
||
| const deflateAsync = promisify(deflate) | ||
|
|
||
| vi.mock('@shopify/cli-kit/node/fs') | ||
|
|
||
| describe('getBundleSize', () => { | ||
| test('returns raw and compressed sizes', async () => { | ||
| // Given | ||
| const content = 'a'.repeat(10000) | ||
| vi.mocked(readFile).mockResolvedValue(content as any) | ||
|
|
||
| // When | ||
| const result = await getBundleSize('/some/path.js') | ||
|
|
||
| // Then | ||
| expect(result.rawBytes).toBe(10000) | ||
| expect(result.compressedBytes).toBe((await deflateAsync(Buffer.from(content))).byteLength) | ||
| expect(result.compressedBytes).toBeLessThan(result.rawBytes) | ||
| }) | ||
|
|
||
| test('compressed size uses deflate to match the backend (Ruby Zlib::Deflate.deflate)', async () => { | ||
| // Given | ||
| const content = JSON.stringify({key: 'value', nested: {array: [1, 2, 3]}}) | ||
| vi.mocked(readFile).mockResolvedValue(content as any) | ||
|
|
||
| // When | ||
| const result = await getBundleSize('/some/path.js') | ||
|
|
||
| // Then | ||
| const expectedCompressed = (await deflateAsync(Buffer.from(content))).byteLength | ||
| expect(result.compressedBytes).toBe(expectedCompressed) | ||
| }) | ||
| }) | ||
|
|
||
| describe('formatBundleSize', () => { | ||
| test('returns formatted size string with raw and compressed sizes', async () => { | ||
| // Given | ||
| const content = 'x'.repeat(50000) | ||
| const compressedSize = (await deflateAsync(Buffer.from(content))).byteLength | ||
| vi.mocked(readFile).mockResolvedValue(content as any) | ||
|
|
||
| // When | ||
| const result = await formatBundleSize('/some/path.js') | ||
|
|
||
| // Then | ||
| const expectedRaw = (50000 / 1024).toFixed(1) | ||
| const expectedCompressed = (compressedSize / 1024).toFixed(1) | ||
| expect(result).toBe(` (${expectedRaw} KB original, ~${expectedCompressed} KB compressed)`) | ||
| }) | ||
|
|
||
| test('formats MB for large files', async () => { | ||
| // Given | ||
| const content = 'a'.repeat(2 * 1024 * 1024) | ||
| const compressedSize = (await deflateAsync(Buffer.from(content))).byteLength | ||
| vi.mocked(readFile).mockResolvedValue(content as any) | ||
|
|
||
| // When | ||
| const result = await formatBundleSize('/some/path.js') | ||
|
|
||
| // Then | ||
| const expectedRaw = (Buffer.byteLength(content) / (1024 * 1024)).toFixed(2) | ||
| const expectedCompressed = (compressedSize / 1024).toFixed(1) | ||
| expect(result).toBe(` (${expectedRaw} MB original, ~${expectedCompressed} KB compressed)`) | ||
| }) | ||
|
|
||
| test('returns empty string on error', async () => { | ||
| // Given | ||
| vi.mocked(readFile).mockRejectedValue(new Error('file not found')) | ||
|
|
||
| // When | ||
| const result = await formatBundleSize('/missing/path.js') | ||
|
|
||
| // Then | ||
| expect(result).toBe('') | ||
| }) | ||
| }) |
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,44 @@ | ||
| import {readFile} from '@shopify/cli-kit/node/fs' | ||
| import {outputDebug} from '@shopify/cli-kit/node/output' | ||
| import {deflate} from 'node:zlib' | ||
| import {promisify} from 'node:util' | ||
|
|
||
| const deflateAsync = promisify(deflate) | ||
|
|
||
| /** | ||
| * Computes the raw and compressed (deflate) size of a file. | ||
| * Uses the same compression algorithm as the Shopify backend (Zlib::Deflate.deflate). | ||
| */ | ||
| export async function getBundleSize(filePath: string) { | ||
| const content = await readFile(filePath) | ||
| const rawBytes = Buffer.byteLength(content) | ||
| const compressed = await deflateAsync(Buffer.from(content)) | ||
| const compressedBytes = compressed.byteLength | ||
|
|
||
| return {path: filePath, rawBytes, compressedBytes} | ||
| } | ||
|
|
||
| /** | ||
| * Formats a byte count as a human-readable string (KB or MB). | ||
| */ | ||
| function formatSize(bytes: number) { | ||
| if (bytes >= 1024 * 1024) { | ||
| return `${(bytes / (1024 * 1024)).toFixed(2)} MB` | ||
| } | ||
| return `${(bytes / 1024).toFixed(1)} KB` | ||
| } | ||
|
|
||
| /** | ||
| * Returns a formatted bundle size suffix like " (21.4 KB original, ~8.3 KB compressed)". | ||
| * Returns an empty string on failure so callers can append it unconditionally. | ||
| */ | ||
| export async function formatBundleSize(filePath: string) { | ||
| try { | ||
| const {rawBytes, compressedBytes} = await getBundleSize(filePath) | ||
| return ` (${formatSize(rawBytes)} original, ~${formatSize(compressedBytes)} compressed)` | ||
| // eslint-disable-next-line no-catch-all/no-catch-all | ||
| } catch (error) { | ||
| outputDebug(`Failed to get bundle size for ${filePath}: ${error}`) | ||
| return '' | ||
| } | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💬 Suggestion: When an extension has assets, each is bundled to a separate output file via
bundleExtension, butformatBundleSizeonly reports the size ofextension.outputPath(the main entry point). If the 64 KB compressed limit applies to the total extension payload (main + assets), developers could be misled about how close they are to the limit. Worth verifying whether the backend counts total extension size or just the main bundle.Suggestion: Consider also reporting asset sizes or a total size so developers get a complete picture. If the 64 KB limit only applies to the main bundle, a brief comment clarifying that would help future maintainers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question. The main bundle JS size is checked individually, but some extensions have more files like should_render or tool files that are also validated by the backend. I think I'd like to start with reporting just the main entry point since this is the file most people seem to have issues with. We can add reporting for other files later on?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good 👍