Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions e2e/cases/assets/assets-url/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { build } from '@e2e/helper';
import { expect, test } from '@playwright/test';
import { pluginReact } from '@rsbuild/plugin-react';

test('should allow to get assets URL with `?url`', async ({ page }) => {
const rsbuild = await build({
cwd: __dirname,
page,
plugins: [pluginReact()],
});

await expect(
Expand Down
6 changes: 6 additions & 0 deletions e2e/cases/assets/assets-url/rsbuild.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { defineConfig } from '@rsbuild/core';
import { pluginReact } from '@rsbuild/plugin-react';

export default defineConfig({
plugins: [pluginReact()],
});
2 changes: 1 addition & 1 deletion e2e/cases/assets/assets-url/src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import img from '@assets/icon.png?url';
import img from '../../../../assets/icon.png?url';

function App() {
return (
Expand Down
28 changes: 28 additions & 0 deletions e2e/cases/print-file-size/basic/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,32 @@ test.describe('should print file size correctly', async () => {
expect(logs.some((log) => log.includes('.js'))).toBeTruthy();
expect(logs.some((log) => log.includes('.css'))).toBeTruthy();
});

test('should not calculate gzip size if the asset is not compressible', async () => {
await build({
cwd,
rsbuildConfig: {
performance: {
printFileSize: true,
},
},
});

for (const log of logs) {
if (log.includes('File')) {
const lines = log.split('\n');
for (const line of lines) {
// dist/static/js/index.js should have gzip size
if (line.includes('.js')) {
expect(line.split('kB').length).toBe(3);
}

// dist/static/image/icon.png should not have gzip size
if (line.includes('.png')) {
expect(line.split('kB').length).toBe(2);
}
}
}
}
});
});
8 changes: 7 additions & 1 deletion e2e/cases/print-file-size/basic/src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import './App.css';
import img from '../../../../assets/icon.png?url';

const App = () => <div id="test">Hello Rsbuild!</div>;
const App = () => (
<div id="test">
<img src={img} alt="test" />
Hello Rsbuild!
</div>
);

export default App;
11 changes: 10 additions & 1 deletion packages/core/src/plugins/fileSize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ const coloringAssetName = (assetName: string) => {
return color.magenta(assetName);
};

const COMPRESSIBLE_REGEX =
/\.(?:js|css|html|json|svg|txt|xml|xhtml|wasm|manifest)$/i;

const isCompressible = (assetName: string) =>
COMPRESSIBLE_REGEX.test(assetName);

async function printFileSizes(
options: PrintFileSizeOptions,
stats: Rspack.Stats,
Expand All @@ -100,7 +106,10 @@ async function printFileSizes(
const fileName = asset.name.split('?')[0];
const contents = await fs.promises.readFile(path.join(distPath, fileName));
const size = contents.length;
const gzippedSize = options.compressed ? await gzipSize(contents) : null;
const gzippedSize =
options.compressed && isCompressible(fileName)
? await gzipSize(contents)
: null;
const gzipSizeLabel = gzippedSize
? getAssetColor(gzippedSize)(calcFileSize(gzippedSize))
: null;
Expand Down