From 338ec7dad74279bdbc4e06eed280c567ffb6f029 Mon Sep 17 00:00:00 2001 From: Karott Date: Tue, 10 May 2022 15:43:29 +0800 Subject: [PATCH] perf: refactor devWarning for production code size (#35411) * pref: better code style for production * refactor `devWarning` * don't use `useEffect` only wrap `devWarning` * chore: add 'noop' to coverage * chore: add test cases for devWarning * chore: add test case * chore: update test cases for devWarning * chore: restore test script command * fix: remove 'throw new Error' * should not use `throw` for browser * chore: update test case for AutoComplete * perf: add prefix for `devWarning` * update RegExp for UMD * add prefix for ES and CJS * chore: better code style * perf: * upgrade antd-tools * remove `injectWarningCondition` * rename `devWarning` to `warning` * chore: better code style * chore: better code style * chore: restore hasValidName --- components/_util/__tests__/warning.test.js | 65 +++++++++++++++++++ components/_util/devWarning.ts | 12 ---- components/_util/warning.ts | 21 ++++++ .../auto-complete/__tests__/index.test.js | 19 +++--- components/auto-complete/index.tsx | 34 +++++----- components/avatar/avatar.tsx | 4 +- components/breadcrumb/Breadcrumb.tsx | 4 +- components/button/button-group.tsx | 4 +- components/button/button.tsx | 6 +- components/cascader/index.tsx | 20 +++--- components/checkbox/Checkbox.tsx | 4 +- .../checkbox/__tests__/checkbox.test.js | 2 +- components/collapse/CollapsePanel.tsx | 4 +- components/collapse/__tests__/index.test.js | 2 +- .../config-provider/__tests__/theme.test.ts | 2 +- components/config-provider/cssVariables.tsx | 4 +- .../__tests__/QuarterPicker.test.js | 2 +- .../generatePicker/generateSinglePicker.tsx | 4 +- .../descriptions/__tests__/index.test.js | 2 +- components/descriptions/index.tsx | 4 +- components/dropdown/dropdown.tsx | 6 +- components/form/FormItem.tsx | 18 ++--- components/form/FormList.tsx | 4 +- components/form/index.tsx | 4 +- components/icon/index.tsx | 4 +- components/input/Input.tsx | 4 +- components/list/__tests__/pagination.test.js | 2 +- components/locale-provider/index.tsx | 4 +- components/menu/__tests__/index.test.js | 3 +- components/menu/index.tsx | 8 +-- components/modal/ConfirmDialog.tsx | 4 +- components/modal/confirm.tsx | 8 +-- components/progress/progress.tsx | 4 +- components/progress/utils.ts | 4 +- components/radio/radio.tsx | 6 +- components/result/index.tsx | 4 +- components/switch/__tests__/index.test.js | 2 +- components/switch/index.tsx | 4 +- components/table/Table.tsx | 8 +-- .../table/__tests__/Table.order.test.js | 2 +- .../table/__tests__/Table.pagination.test.js | 2 +- .../__tests__/Table.rowSelection.test.js | 2 +- components/table/hooks/useFilter/index.tsx | 4 +- components/table/hooks/useSelection.tsx | 46 ++++++------- components/tabs/index.tsx | 4 +- .../time-picker/__tests__/index.test.js | 2 +- components/time-picker/index.tsx | 4 +- components/transfer/index.tsx | 4 +- components/tree-select/index.tsx | 4 +- components/typography/Link.tsx | 4 +- components/typography/Text.tsx | 4 +- components/typography/Title.tsx | 4 +- components/typography/Typography.tsx | 4 +- components/upload/Upload.tsx | 24 ++++--- components/upload/__tests__/upload.test.js | 2 +- package.json | 2 +- site/theme/template/Icon/index.jsx | 2 +- site/theme/template/Icon/utils.js | 2 +- webpack.config.js | 22 ------- 59 files changed, 253 insertions(+), 211 deletions(-) create mode 100644 components/_util/__tests__/warning.test.js delete mode 100644 components/_util/devWarning.ts create mode 100644 components/_util/warning.ts diff --git a/components/_util/__tests__/warning.test.js b/components/_util/__tests__/warning.test.js new file mode 100644 index 000000000000..2eeeb0f1bf57 --- /dev/null +++ b/components/_util/__tests__/warning.test.js @@ -0,0 +1,65 @@ +describe('Test warning', () => { + let spy: jest.SpyInstance; + + beforeAll(() => { + spy = jest.spyOn(console, 'error'); + }); + + afterAll(() => { + spy.mockRestore(); + }); + + beforeEach(() => { + jest.resetModules(); + }); + + afterEach(() => { + spy.mockReset(); + }); + + it('Test noop', async () => { + const { noop } = await import('../warning'); + const value = noop(); + + expect(value).toBe(undefined); + expect(spy).not.toHaveBeenCalled(); + expect(() => { + noop(); + }).not.toThrow(); + }); + + describe('process.env.NODE_ENV !== "production"', () => { + it('If `false`, exec `console.error`', async () => { + const warning = (await import('../warning')).default; + warning(false, 'error'); + + expect(spy).toHaveBeenCalled(); + }); + + it('If `true`, do not exec `console.error`', async () => { + const warning = (await import('../warning')).default; + warning(true, 'error message'); + + expect(spy).not.toHaveBeenCalled(); + }); + }); + + describe('process.env.NODE_ENV === "production"', () => { + it('Whether `true` or `false`, do not exec `console.error`', async () => { + const prevEnv = process.env.NODE_ENV; + process.env.NODE_ENV = 'production'; + + const { default: warning, noop } = await import('../warning'); + + expect(warning).toEqual(noop); + + warning(false, 'error message'); + expect(spy).not.toHaveBeenCalled(); + + warning(true, 'error message'); + expect(spy).not.toHaveBeenCalled(); + + process.env.NODE_ENV = prevEnv; + }); + }); +}); diff --git a/components/_util/devWarning.ts b/components/_util/devWarning.ts deleted file mode 100644 index 34090ca30680..000000000000 --- a/components/_util/devWarning.ts +++ /dev/null @@ -1,12 +0,0 @@ -import devWarning, { resetWarned } from 'rc-util/lib/warning'; - -export { resetWarned }; - -export default (valid: boolean, component: string, message: string): void => { - devWarning(valid, `[antd: ${component}] ${message}`); - - // StrictMode will inject console which will not throw warning in React 17. - if (process.env.NODE_ENV === 'test') { - resetWarned(); - } -}; diff --git a/components/_util/warning.ts b/components/_util/warning.ts new file mode 100644 index 000000000000..ff7357f4f943 --- /dev/null +++ b/components/_util/warning.ts @@ -0,0 +1,21 @@ +import rcWarning, { resetWarned } from 'rc-util/lib/warning'; + +export { resetWarned }; +export function noop() {} + +type Warning = (valid: boolean, component: string, message: string) => void; + +// eslint-disable-next-line import/no-mutable-exports +let warning: Warning = noop; +if (process.env.NODE_ENV !== 'production') { + warning = (valid, component, message) => { + rcWarning(valid, `[antd: ${component}] ${message}`); + + // StrictMode will inject console which will not throw warning in React 17. + if (process.env.NODE_ENV === 'test') { + resetWarned(); + } + }; +} + +export default warning; diff --git a/components/auto-complete/__tests__/index.test.js b/components/auto-complete/__tests__/index.test.js index 4c925f0e9479..702e885278a6 100644 --- a/components/auto-complete/__tests__/index.test.js +++ b/components/auto-complete/__tests__/index.test.js @@ -42,16 +42,15 @@ describe('AutoComplete', () => { }); it('AutoComplete throws error when contains invalid dataSource', () => { - jest.spyOn(console, 'error').mockImplementation(() => undefined); - expect(() => { - mount( - {}]}> -