Skip to content

Commit

Permalink
perf: reduce the bundle size of client
Browse files Browse the repository at this point in the history
  • Loading branch information
easy1090 committed Dec 17, 2024
1 parent a9c0b0f commit f4c1bf5
Show file tree
Hide file tree
Showing 14 changed files with 57 additions and 26 deletions.
8 changes: 1 addition & 7 deletions packages/client/rsbuild.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ export default defineConfig(({ env }) => {
ids: true,
version: true,
entrypoints: true,
optimizationBailout: true,
});
await fs.promises.writeFile(
WebpackStatsFilePath,
Expand All @@ -161,13 +162,6 @@ export default defineConfig(({ env }) => {
chainConfig.plugin('rsdoctor').use(RsdoctorRspackPlugin, [
{
disableClientServer: !ENABLE_CLIENT_SERVER,
features: {
loader: true,
plugins: true,
resolver: true,
bundle: true,
treeShaking: true,
},
},
]);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/pages/TreeShaking/table.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SDK } from '@rsdoctor/types';
import {
import type {
Module,
ModuleGraph,
Statement,
Expand Down
3 changes: 2 additions & 1 deletion packages/components/src/pages/TreeShaking/utils.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useMemo } from 'react';
import path from 'path-browserify';
import { escape, get } from 'lodash-es';
import { Module, ModuleGraph, Statement, Variable } from '@rsdoctor/graph';
import type { ModuleGraph, Statement, Variable } from '@rsdoctor/graph';
import { Module } from '@rsdoctor/graph';
import { Tag, Space } from 'antd';
import { Range } from './range';
import type { editor, Range as RangeClass } from 'monaco-editor';
Expand Down
4 changes: 2 additions & 2 deletions packages/graph/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
"dependencies": {
"@rsdoctor/types": "workspace:*",
"@rsdoctor/utils": "workspace:*",
"lodash": "^4.17.21",
"lodash.unionby": "^4.8.0",
"socket.io": "4.8.1",
"source-map": "^0.7.4"
},
"devDependencies": {
"@types/body-parser": "1.19.5",
"@types/estree": "1.0.5",
"@types/lodash": "^4.17.13",
"@types/lodash.unionby": "^4.8.9",
"@types/node": "^16",
"fs-extra": "^11.1.1",
"tslib": "2.8.1",
Expand Down
6 changes: 3 additions & 3 deletions packages/graph/src/graph/module-graph/module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SDK } from '@rsdoctor/types';
import path from 'path';
import { isNumber } from 'lodash';
import { Lodash } from '@rsdoctor/utils/common';
import type { SourceMapConsumer } from 'source-map';
import type { Program } from 'estree';
import { Dependency } from './dependency';
Expand Down Expand Up @@ -263,7 +263,7 @@ export class Module implements SDK.ModuleInstance {
bias: 1,
});

if (isNumber(startInSource.line)) {
if (Lodash.isNumber(startInSource.line)) {
source.start = {
line: startInSource.line,
column: startInSource.column ?? undefined,
Expand All @@ -278,7 +278,7 @@ export class Module implements SDK.ModuleInstance {
// bias: 2,
});

if (isNumber(endInSource.line)) {
if (Lodash.isNumber(endInSource.line)) {
source.end = {
line: endInSource.line,
column: endInSource.column ?? undefined,
Expand Down
8 changes: 4 additions & 4 deletions packages/graph/src/graph/module-graph/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { SDK } from '@rsdoctor/types';
import { last, isUndefined, isNil } from 'lodash';
import { Lodash } from '@rsdoctor/utils/common';

export function isSamePosition(
po1: SDK.SourcePosition,
Expand All @@ -13,11 +13,11 @@ export function isSameRange(po1: SDK.SourceRange, po2: SDK.SourceRange) {
return false;
}

if (!isNil(po1.end) && !isNil(po2.end)) {
if (!Lodash.isNil(po1.end) && !Lodash.isNil(po2.end)) {
return isSamePosition(po1.end, po2.end);
}

return isUndefined(po1.end) && isUndefined(po2.end);
return Lodash.isUndefined(po1.end) && Lodash.isUndefined(po2.end);
}

/**
Expand All @@ -44,7 +44,7 @@ export function getModuleName(name?: string) {
}

if (NAME_WITH_LOADERS.test(name)) {
const normalizedName = last(name.split(NAME_WITH_LOADERS));
const normalizedName = Lodash.last(name.split(NAME_WITH_LOADERS));

if (normalizedName?.trim()) {
return normalizedName;
Expand Down
4 changes: 2 additions & 2 deletions packages/graph/src/graph/package-graph/graph.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { unionBy } from 'lodash';
import { dirname, join, resolve } from 'path';
import unionBy from 'lodash.unionby';
import { resolve } from 'path';
import { SDK } from '@rsdoctor/types';
import type { ModuleGraph, Module } from '../module-graph';
import { Package } from './package';
Expand Down
2 changes: 0 additions & 2 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@
"get-port": "5.1.1",
"json-stream-stringify": "3.0.1",
"lines-and-columns": "2.0.4",
"lodash": "^4.17.21",
"rslog": "^1.2.3",
"strip-ansi": "^6.0.1"
},
Expand All @@ -98,7 +97,6 @@
"@types/deep-eql": "4.0.2",
"@types/envinfo": "7.8.4",
"@types/fs-extra": "^11.0.4",
"@types/lodash": "^4.17.13",
"@types/node": "^16",
"tslib": "2.8.1",
"typescript": "^5.2.2"
Expand Down
1 change: 1 addition & 0 deletions packages/utils/src/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export * as Data from './data';
export * as Alerts from './alerts';
export * as Rspack from './rspack';
export * as Package from './package';
export * as Lodash from './lodash';
37 changes: 37 additions & 0 deletions packages/utils/src/common/lodash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Replace lodash's isUndefined function
export function isUndefined(value: unknown): value is undefined {
return typeof value === 'undefined';
}

// Replace lodash's isNumber function
export function isNumber(value: unknown): value is number {
return typeof value === 'number' && !Number.isNaN(value);
}

export function isObject(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null;
}

// Replace lodash's isEmpty function
export function isEmpty(value: unknown): boolean {
return (
value == null || // Check for null or undefined
(Array.isArray(value) && value.length === 0) || // Check for empty array
(typeof value === 'object' && Object.keys(value).length === 0) // Check for empty object
);
}

// Replace lodash's last function
export function last<T>(array: T[]): T | undefined {
return array[array.length - 1];
}

// Replace lodash's compact function
export function compact<T>(array: (T | null | undefined)[]): T[] {
return array.filter((item): item is T => item != null || !item); // Filter out null and undefined
}

// Replace lodash's isNil function
export function isNil(value: unknown): value is null | undefined {
return value === null || value === undefined;
}
2 changes: 1 addition & 1 deletion packages/utils/src/common/package.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SDK } from '@rsdoctor/types';
import { isEmpty, last, compact } from 'lodash';
import { compact, isEmpty, last } from './lodash';

/**
* The following code is modified based on
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/error/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { codeFrameColumns } from '@babel/code-frame';
import { Err, Rule } from '@rsdoctor/types';
import { Chalk, Instance } from 'chalk';
import deepEql from 'deep-eql';
import { isNil } from 'lodash';
import stripAnsi from 'strip-ansi';
import { transform } from './transform';
import { insertSpace, toLevel } from './utils';
import { isNil } from 'src/common/lodash';

let id = 1;

Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/rule-utils/document/document.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { LinesAndColumns } from 'lines-and-columns';
import { isUndefined, isNumber } from 'lodash';
import { Range, OffsetRange, Position, DocumentEditData } from './types';
import { isNumber, isUndefined } from 'src/common/lodash';

/** Document Catalogue */
export class Document {
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/rule-utils/parser/asserts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isObject } from 'lodash';
import { isObject } from 'src/common/lodash';
import { Node } from './types';

function isSyntaxNode(node: unknown): node is Node.SyntaxNode {
Expand Down

0 comments on commit f4c1bf5

Please sign in to comment.