From 45c61db88538464392312cdc90c8d32dfa88768b Mon Sep 17 00:00:00 2001 From: ClarkXia Date: Thu, 18 Jan 2024 10:37:35 +0800 Subject: [PATCH] Feat: support optimize package import (#6736) * feat: support optimize package import * chore: comment * fix: optimize imports --- .changeset/five-kids-sit.md | 7 +++ .changeset/thin-ligers-taste.md | 5 ++ examples/with-antd5/ice.config.mts | 3 ++ packages/bundles/package.json | 2 +- packages/ice/src/config.ts | 77 ++++++++++++++++++++++++++++ packages/ice/src/types/userConfig.ts | 6 +++ packages/rspack-config/src/index.ts | 18 +++++++ packages/shared-config/src/types.ts | 2 + pnpm-lock.yaml | 50 +++++++++--------- 9 files changed, 144 insertions(+), 26 deletions(-) create mode 100644 .changeset/five-kids-sit.md create mode 100644 .changeset/thin-ligers-taste.md diff --git a/.changeset/five-kids-sit.md b/.changeset/five-kids-sit.md new file mode 100644 index 0000000000..c9bcfe9e37 --- /dev/null +++ b/.changeset/five-kids-sit.md @@ -0,0 +1,7 @@ +--- +'@ice/rspack-config': patch +'@ice/shared-config': patch +'@ice/app': patch +--- + +feat: support optimize package import diff --git a/.changeset/thin-ligers-taste.md b/.changeset/thin-ligers-taste.md new file mode 100644 index 0000000000..3de7365aab --- /dev/null +++ b/.changeset/thin-ligers-taste.md @@ -0,0 +1,5 @@ +--- +'@ice/bundles': patch +--- + +fix: update binding version for optimize imports diff --git a/examples/with-antd5/ice.config.mts b/examples/with-antd5/ice.config.mts index 63f3c0bcab..4bf0e20a7b 100644 --- a/examples/with-antd5/ice.config.mts +++ b/examples/with-antd5/ice.config.mts @@ -2,4 +2,7 @@ import { defineConfig } from '@ice/app'; export default defineConfig(() => ({ ssg: false, + optimization: { + optimizePackageImport: true, + } })); diff --git a/packages/bundles/package.json b/packages/bundles/package.json index 72d04deff7..a366027dab 100644 --- a/packages/bundles/package.json +++ b/packages/bundles/package.json @@ -45,7 +45,7 @@ "zod": "^3.22.3", "zod-validation-error": "1.2.0", "terminal-link": "^2.1.1", - "@ice/pack-binding": "0.0.6", + "@ice/pack-binding": "0.0.8", "mime-types": "2.1.35" }, "devDependencies": { diff --git a/packages/ice/src/config.ts b/packages/ice/src/config.ts index eebd72bfcd..efc4c979c8 100644 --- a/packages/ice/src/config.ts +++ b/packages/ice/src/config.ts @@ -352,6 +352,83 @@ const userConfig = [ { name: 'optimization', validation: 'object', + setConfig: (config: Config, optimization: UserConfig['optimization'], context: UserConfigContext) => { + const { commandArgs } = context; + if (optimization?.optimizePackageImport) { + if (commandArgs.speedup) { + config.optimizePackageImports = [ + ...new Set([ + ...(Array.isArray(optimization?.optimizePackageImport) ? optimization?.optimizePackageImport : []), + // Buit-in packages is modified based on + // https://github.com/vercel/next.js/blob/7b73f1137b21c7b1fb1612c3389caaaadd18da65/packages/next/src/server/config.ts#L827 + '@alifd/next', + '@ali/uni-api', + 'lucide-react', + 'date-fns', + 'lodash-es', + 'ramda', + 'antd', + 'react-bootstrap', + 'ahooks', + '@ant-design/icons', + '@headlessui/react', + '@headlessui-float/react', + '@heroicons/react/20/solid', + '@heroicons/react/24/solid', + '@heroicons/react/24/outline', + '@visx/visx', + '@tremor/react', + 'rxjs', + '@mui/material', + '@mui/icons-material', + 'recharts', + 'react-use', + '@material-ui/core', + '@material-ui/icons', + '@tabler/icons-react', + 'mui-core', + 'react-icons/ai', + 'react-icons/bi', + 'react-icons/bs', + 'react-icons/cg', + 'react-icons/ci', + 'react-icons/di', + 'react-icons/fa', + 'react-icons/fa6', + 'react-icons/fc', + 'react-icons/fi', + 'react-icons/gi', + 'react-icons/go', + 'react-icons/gr', + 'react-icons/hi', + 'react-icons/hi2', + 'react-icons/im', + 'react-icons/io', + 'react-icons/io5', + 'react-icons/lia', + 'react-icons/lib', + 'react-icons/lu', + 'react-icons/md', + 'react-icons/pi', + 'react-icons/ri', + 'react-icons/rx', + 'react-icons/si', + 'react-icons/sl', + 'react-icons/tb', + 'react-icons/tfi', + 'react-icons/ti', + 'react-icons/vsc', + 'react-icons/wi', + ]), + ]; + } else { + logger.warn(` + optimizePackageImport only works in speedup mode, + try to run \`npm ${commandArgs.command === 'start' ? 'start' : 'run build'} -- --speedup\``, + ); + } + } + }, }, { name: 'mock', diff --git a/packages/ice/src/types/userConfig.ts b/packages/ice/src/types/userConfig.ts index a1f96f8a49..727b8465b0 100644 --- a/packages/ice/src/types/userConfig.ts +++ b/packages/ice/src/types/userConfig.ts @@ -22,6 +22,12 @@ interface Optimization { * Remove react-router dependencies by force, even if route count is greater than 1. */ disableRouter?: boolean; + /** + * Automatically the apply modularize imports optimization, + * it will remove unused code of package when it is a barrel file. + * Note: It is only supported in speedup mode. + */ + optimizePackageImport?: string[] | boolean; } interface MinifyOptions { diff --git a/packages/rspack-config/src/index.ts b/packages/rspack-config/src/index.ts index 57b54046e3..e75d48785c 100644 --- a/packages/rspack-config/src/index.ts +++ b/packages/rspack-config/src/index.ts @@ -48,6 +48,7 @@ const getConfig: GetConfig = async (options) => { } = options; const { + cacheDir, mode, minify, publicPath = '/', @@ -68,6 +69,7 @@ const getConfig: GetConfig = async (options) => { middlewares, configureWebpack = [], minimizerOptions = {}, + optimizePackageImports = [], } = taskConfig || {}; const isDev = mode === 'development'; const absoluteOutputDir = path.isAbsolute(outputDir) ? outputDir : path.join(rootDir, outputDir); @@ -154,6 +156,7 @@ const getConfig: GetConfig = async (options) => { transformFeatures: { removeExport: swcOptions.removeExportExprs, keepExport: swcOptions.keepExports, + optimizeImport: optimizePackageImports, }, compileRules: { // "bundles/compiled" is the path when using @ice/bundles. @@ -162,6 +165,21 @@ const getConfig: GetConfig = async (options) => { }, }, }, + { + test: /__barrel_optimize__/, + use: ({ realResource }: { realResource: string }) => { + const names = ( + realResource.match(/\?names=([^&]+)!=!/)?.[1] || '' + ).split(','); + return [{ + loader: 'builtin:barrel-loader', + options: { + names, + cacheDir, + }, + }]; + }, + }, ...getAssetsRule(), ...getCssRules({ rootDir, diff --git a/packages/shared-config/src/types.ts b/packages/shared-config/src/types.ts index 0d0b90e8e2..d1a7722e8b 100644 --- a/packages/shared-config/src/types.ts +++ b/packages/shared-config/src/types.ts @@ -205,4 +205,6 @@ export interface Config { useDevServer?: boolean; useDataLoader?: boolean; + + optimizePackageImports?: string[]; } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 18732317d3..fc470ee99c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1276,8 +1276,8 @@ importers: specifier: 0.0.6 version: 0.0.6 '@ice/pack-binding': - specifier: 0.0.6 - version: 0.0.6 + specifier: 0.0.8 + version: 0.0.8 '@ice/swc-plugin-keep-export': specifier: 0.2.0 version: 0.2.0 @@ -6658,8 +6658,8 @@ packages: '@ice/css-modules-hash-win32-x64-msvc': 0.0.6 dev: false - /@ice/pack-binding-darwin-arm64@0.0.6: - resolution: {integrity: sha512-vJNdd1rvAA+3l2wlZvZKoOaz+ti7RG4MaHM/T+fEgt9lDpxhY3X+/4MNKdJnzMGsBsX5vMjvifRXS4UFFSg5oQ==} + /@ice/pack-binding-darwin-arm64@0.0.8: + resolution: {integrity: sha512-dlJEOZCLaxv1S+RUTB4sFRtLP7EISNYXAwN97WiZ+KUG9TRkzvNwV/WjzoOCOmaoNNbqeHwxbliM60gV3O9BVg==} engines: {node: '>= 10'} cpu: [arm64] os: [darwin] @@ -6667,16 +6667,16 @@ packages: dev: false optional: true - /@ice/pack-binding-darwin-universal@0.0.6: - resolution: {integrity: sha512-Y/eDuy667DvUAWl9hpg4D8YCFogW6eCCdOIEMVhxHQOBCMa9QbXCg6TKsI+xKfudUCNCsqg+dQTdRUMCazNjxQ==} + /@ice/pack-binding-darwin-universal@0.0.8: + resolution: {integrity: sha512-26Ga4Ct7yfOpPNp3cPZgM+XNupKB6IvZ5vsDrnWl9vwmNQszCMyVZcpgaNQ9rPRB/lbP4NgtKc9pQU8T38Ptsg==} engines: {node: '>= 10'} os: [darwin] requiresBuild: true dev: false optional: true - /@ice/pack-binding-darwin-x64@0.0.6: - resolution: {integrity: sha512-QEWYkPtzIYLvw9h5K9NjwXjB+XFVRUnpaEjQPoO3xGC/0l0xABOtdl4kbo/ya1ucIwq8hM530fi/STBUy8UaEg==} + /@ice/pack-binding-darwin-x64@0.0.8: + resolution: {integrity: sha512-AJ7mPitkJ7j37WUYUx+VB3xWQOsL5zSET4CeDBEthxrUScmIjduZ32edB0DHtnsqEFz7NBVN2EHjLq54lHv09A==} engines: {node: '>= 10'} cpu: [x64] os: [darwin] @@ -6684,8 +6684,8 @@ packages: dev: false optional: true - /@ice/pack-binding-linux-x64-gnu@0.0.6: - resolution: {integrity: sha512-Ol6E62HxLhCPAeTJ7e6vaC1UsKJNt5mCngQVlUAut5QIFccQw6m+ZFGHu/OZfvmtTYwCo4d3m8OlBoTvIyIY5w==} + /@ice/pack-binding-linux-x64-gnu@0.0.8: + resolution: {integrity: sha512-Zkwn0AdtBt4DyJ3GhNRM/PMzOHWJAuZB0LjgymtbKNqAvkB3cScJTdV7dui2VZJ3W0vosjTQ/wNpWVKdxClXXg==} engines: {node: '>= 10'} cpu: [x64] os: [linux] @@ -6693,8 +6693,8 @@ packages: dev: false optional: true - /@ice/pack-binding-linux-x64-musl@0.0.6: - resolution: {integrity: sha512-koCI4HhBJk/MhMdeAJiMMza1Y0+HRexl2YsAsgZZLTAuOPhPCqFrofiIAJ1hBJhQpwWdK4Dfi13szeoWp2HIlA==} + /@ice/pack-binding-linux-x64-musl@0.0.8: + resolution: {integrity: sha512-wQROpPgNKikRMfDp5j7/PrEQ12iarEtKWmnFPl93xeGNBibVLfR+mmWhuBlZu46cHYcFyZmS4UqEJZOmB4vXKQ==} engines: {node: '>= 10'} cpu: [x64] os: [linux] @@ -6702,8 +6702,8 @@ packages: dev: false optional: true - /@ice/pack-binding-win32-arm64-msvc@0.0.6: - resolution: {integrity: sha512-4xyVffE2wzI1TAaJhXj3SYGEgPSlg5qwDJxu+8S77sMrFmqGFOkTbo3Jg/wI1FUFsvWCEJZz4ly4AY4CF7dxhA==} + /@ice/pack-binding-win32-arm64-msvc@0.0.8: + resolution: {integrity: sha512-0eSCL+WLk47Mc9ePPH7Wu36KAOAULFkPJJUssARi59XzfpyOodTy5vjXxoh0X5YXwzuySR4ljEitvSxvY5pgLw==} engines: {node: '>= 10'} cpu: [arm64] os: [win32] @@ -6711,8 +6711,8 @@ packages: dev: false optional: true - /@ice/pack-binding-win32-x64-msvc@0.0.6: - resolution: {integrity: sha512-7rJF5RMzg2vdyBoSAS9nBSGKHUqWB9S5TN4oAqssrTWbBeCT5Sf8GHUfwzRKBc0a/3muSUPzRxOIKh6yCT859g==} + /@ice/pack-binding-win32-x64-msvc@0.0.8: + resolution: {integrity: sha512-Iw/JcTJSeuZnYecEEELuL5b67jngfQP3YEdYXwzUp3o4mn3AIKHeBeXN6ErsBb3r698M1AY9DXn2Bl9GyDOqHw==} engines: {node: '>= 10'} cpu: [x64] os: [win32] @@ -6720,17 +6720,17 @@ packages: dev: false optional: true - /@ice/pack-binding@0.0.6: - resolution: {integrity: sha512-nH2oBXgSOQfLILVBWDgxvmf2+UbibGGzNaynAxlSM+v1keQqGcy/me//wvwKDFlk/dWYvHQSv2vGZ3AUYshh5g==} + /@ice/pack-binding@0.0.8: + resolution: {integrity: sha512-kDtJvWdK6w+ooukRIRz25v+LNvEHDj7U9O+33nKf5uaHQFWMsbh8A+kM56yE8EF/BtrgEpxSujUynRmVfuwm0g==} engines: {node: '>= 10'} optionalDependencies: - '@ice/pack-binding-darwin-arm64': 0.0.6 - '@ice/pack-binding-darwin-universal': 0.0.6 - '@ice/pack-binding-darwin-x64': 0.0.6 - '@ice/pack-binding-linux-x64-gnu': 0.0.6 - '@ice/pack-binding-linux-x64-musl': 0.0.6 - '@ice/pack-binding-win32-arm64-msvc': 0.0.6 - '@ice/pack-binding-win32-x64-msvc': 0.0.6 + '@ice/pack-binding-darwin-arm64': 0.0.8 + '@ice/pack-binding-darwin-universal': 0.0.8 + '@ice/pack-binding-darwin-x64': 0.0.8 + '@ice/pack-binding-linux-x64-gnu': 0.0.8 + '@ice/pack-binding-linux-x64-musl': 0.0.8 + '@ice/pack-binding-win32-arm64-msvc': 0.0.8 + '@ice/pack-binding-win32-x64-msvc': 0.0.8 dev: false /@ice/pkg@1.5.5: