Skip to content

Commit

Permalink
perf: compute gzip size in parallel (#2813)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan committed Jul 5, 2024
1 parent 0fcaf2e commit da815e6
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 18 deletions.
1 change: 0 additions & 1 deletion .github/renovate.json5
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
// some loaders still depend on loader-utils v2
"loader-utils",
// pure esm packages can not be used now
"gzip-size",
"globby",
"open",
"strip-ansi",
Expand Down
1 change: 0 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@
"dotenv": "16.4.5",
"dotenv-expand": "11.0.6",
"fs-extra": "^11.2.0",
"gzip-size": "^6.0.0",
"http-compression": "1.0.20",
"http-proxy-middleware": "^2.0.6",
"jiti": "^1.21.6",
Expand Down
4 changes: 0 additions & 4 deletions packages/core/prebundle.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export default {
'connect',
'rspack-manifest-plugin',
'webpack-merge',
'gzip-size',
{
name: 'chokidar',
externals: {
Expand Down Expand Up @@ -127,9 +126,6 @@ export default {
// The webpack-bundle-analyzer version was locked to v4.9.0 to be compatible with Rspack
// If we need to upgrade the version, please check if the chunk detail can be displayed correctly
name: 'webpack-bundle-analyzer',
externals: {
'gzip-size': '../gzip-size',
},
},
{
name: 'autoprefixer',
Expand Down
23 changes: 15 additions & 8 deletions packages/core/src/plugins/fileSize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*/
import fs from 'node:fs';
import path from 'node:path';
import { promisify } from 'node:util';
import zlib from 'node:zlib';
import color from 'picocolors';
import { CSS_REGEX, HTML_REGEX, JS_REGEX } from '../constants';
import { logger } from '../logger';
Expand All @@ -14,6 +16,13 @@ import type {
StatsAsset,
} from '../types';

const gzip = promisify(zlib.gzip);

async function gzipSize(input: Buffer) {
const data = await gzip(input);
return data.length;
}

/** Filter source map and license files */
export const filterAsset = (asset: string): boolean =>
!/\.map$/.test(asset) && !/\.LICENSE\.txt$/.test(asset);
Expand Down Expand Up @@ -71,17 +80,15 @@ async function printFileSizes(
return logs;
}

const { default: gzipSize } = await import('gzip-size');

const formatAsset = (
const formatAsset = async (
asset: StatsAsset,
distPath: string,
distFolder: string,
) => {
const fileName = asset.name.split('?')[0];
const contents = fs.readFileSync(path.join(distPath, fileName));
const size = contents.length;
const gzippedSize = gzipSize.sync(contents);
const gzippedSize = await gzipSize(contents);

return {
size,
Expand All @@ -93,7 +100,7 @@ async function printFileSizes(
};
};

const getAssets = () => {
const getAssets = async () => {
const distPath = stats.compilation.outputOptions.path;

if (!distPath) {
Expand All @@ -119,11 +126,11 @@ async function printFileSizes(

const distFolder = path.relative(rootPath, distPath);

return filteredAssets.map((asset) =>
formatAsset(asset, distPath, distFolder),
return Promise.all(
filteredAssets.map((asset) => formatAsset(asset, distPath, distFolder)),
);
};
const assets = getAssets();
const assets = await getAssets();

if (assets.length === 0) {
return logs;
Expand Down
1 change: 0 additions & 1 deletion packages/core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"semver": ["./compiled/semver"],
"connect": ["./compiled/connect"],
"chokidar": ["./compiled/chokidar"],
"gzip-size": ["./compiled/gzip-size"],
"commander": ["./compiled/commander"],
"on-finished": ["./compiled/on-finished"],
"autoprefixer": ["./compiled/autoprefixer"],
Expand Down
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit da815e6

Please sign in to comment.