Skip to content

Commit

Permalink
chore(react-ui): 打包优化
Browse files Browse the repository at this point in the history
1. iife 打包与 es、cjs 打包模式分离
2. 添加打包依赖分析
3. 修复会打包 jsx-runtime 依赖的问题
4. es、cjs 打包排除 @tool-pack/types、@tool-pack/basic、@tool-pack/dom、rxjs,使用依赖代替打包
5. iife 打包会将 react、react-dom 之外的必要依赖都打包进去,减少不必要的 script 导入
6. 使用 vite 插件把 process.env.NODE_ENV 直接替换为 production
7. 组件库的依赖包含 @tool-pack/types、@tool-pack/basic、@tool-pack/dom、rxjs
  • Loading branch information
mengxinssfd committed Jun 13, 2024
1 parent 0265c7e commit 0b3d69a
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 90 deletions.
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
"devDependencies": {
"@commitlint/cli": "^17.7.1",
"@commitlint/config-conventional": "^17.7.0",
"dumi-theme-antd-style": "^0.31.0",
"@rollup/plugin-json": "^6.0.0",
"@testing-library/jest-dom": "^6.1.2",
"@testing-library/react": "^14.1.2",
Expand All @@ -74,12 +73,13 @@
"@typescript-eslint/eslint-plugin": "5.62.0",
"@typescript-eslint/parser": "5.62.0",
"@umijs/lint": "^4.2.6",
"@vitejs/plugin-react": "^4.2.1",
"@vitejs/plugin-react": "^4.3.1",
"autoprefixer": "^10.4.15",
"chalk": "^5.3.0",
"conventional-changelog-cli": "^3.0.0",
"cssnano": "^6.0.1",
"dumi": "~2.2.17",
"dumi-theme-antd-style": "^0.31.0",
"enquirer": "^2.4.1",
"eslint": "8.47.0",
"eslint-config-prettier": "^9.0.0",
Expand All @@ -103,6 +103,7 @@
"rollup": "^3.28.1",
"rollup-plugin-dts": "^6.0.0",
"rollup-plugin-typescript2": "^0.35.0",
"rollup-plugin-visualizer": "^5.12.0",
"rxjs": "^7.8.1",
"sass": "^1.66.1",
"serve": "^14.2.1",
Expand All @@ -114,7 +115,8 @@
"stylelint-scss": "^5.1.0",
"tsc-alias": "^1.8.7",
"typescript": "^5.4.5",
"vite": "^5.0.10",
"vite": "^5.2.13",
"vite-plugin-replace": "^0.1.1",
"yalc": "1.0.0-pre.53"
},
"browserslist": {
Expand Down
15 changes: 9 additions & 6 deletions packages/react-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
"scripts": {
"rm:dist": "rimraf ./dist",
"build": "npm run build:vite && npm run build:dts && npm run build:scss && npm run build:postcss && npm run build:locale",
"build:vite": "vite build",
"build:vite": "npm run build:vite:es-cjs && npm run build:vite:iife",
"build:vite:es-cjs": "vite build -c vite.config.ts",
"build:vite:iife": "vite build -c vite.config.iife.ts",
"analyze:es": "vite build -c vite.config.ts --mode analyze",
"analyze:iife": "vite build -c vite.config.iife.ts --mode analyze",
"build:scss-single": "tsx scripts/scss-single-file.ts",
"build:scss": "sass src/index.scss dist/index.css --no-source-map && npm run build:scss-single",
"build:postcss": "postcss --config ./postcss.config.js dist/index.css -o dist/index.css",
Expand Down Expand Up @@ -51,11 +55,10 @@
"LICENSE"
],
"dependencies": {
"@tool-pack/types": "^0.2.0"
},
"devDependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
"@tool-pack/types": "^0.3.0",
"@tool-pack/basic": "^0.9.1",
"@tool-pack/dom": "^0.4.0",
"rxjs": "^7.8.1"
},
"peerDependencies": {
"react": "^18.2.0",
Expand Down
26 changes: 26 additions & 0 deletions packages/react-ui/vite.config.iife.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { getCommonViteConfig, getBanner } from './vite.utils';
import { type UserConfig, defineConfig } from 'vite';

/**
* Vite configuration
* https://vitejs.dev/config/
*/
export default defineConfig((): UserConfig => {
const config = getCommonViteConfig({
output: [
{
globals: {
'react-dom': 'ReactDOM',
react: 'React',
},
entryFileNames: '[name].iife.js',
banner: getBanner('iife'),
name: 'ToolPackReactUI',
format: 'iife',
},
],
external: ['react', 'react-dom'],
});
config.build.emptyOutDir = false;
return config;
});
101 changes: 34 additions & 67 deletions packages/react-ui/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,75 +1,42 @@
import { defineConfig, UserConfig } from 'vite';
import react from '@vitejs/plugin-react';
import { getAlias } from '../../utils';
import pkg from './package.json';
import { resolve } from 'path';
import { getCommonViteConfig, getBanner } from './vite.utils';
import type { UserConfig } from 'vite';
import { defineConfig } from 'vite';

function getBanner(format: string) {
const date = new Date();
return (
'/*!\n' +
` * ToolPackReactUI(${pkg.name}) v${pkg.version}\n` +
` * Author: ${pkg.author}\n` +
` * Documentation: ${pkg.homepage}\n` +
` * License: ${pkg.license}\n` +
` * File: index.${format}.js\n` +
` * Date: ${date.getFullYear()}-${
date.getMonth() + 1
}-${date.getDate()}\n` +
` */\n`
);
}

const entryPath = resolve(__dirname, './src/index.ts');
type Output = Exclude<
Exclude<UserConfig['build'], undefined>['rollupOptions'],
undefined
>['output'];
/**
* Vite configuration
* https://vitejs.dev/config/
*/
export default defineConfig((): UserConfig => {
return {
build: {
rollupOptions: {
output: [
{
globals: {
'react-dom': 'ReactDom',
react: 'React',
},
entryFileNames: '[name].iife.js',
banner: getBanner('iife'),
name: 'ToolPackReactUI',
format: 'iife',
},
{
entryFileNames: '[name].es.js',
banner: getBanner('es'),
format: 'es',
},

{
entryFileNames: '[name].cjs.js',
banner: getBanner('cjs'),
format: 'cjs',
},
],
external: ['react', 'react-dom'],
},
outDir: resolve(__dirname, './dist'),
lib: { entry: entryPath },
// minify: true,
cssCodeSplit: true,
target: 'modules',
emptyOutDir: true,
export default defineConfig(({ mode }): UserConfig => {
const cjsOutput: Output = [
{
entryFileNames: '[name].cjs.js',
banner: getBanner('cjs'),
format: 'cjs',
},
plugins: [
// https://github.com/vitejs/vite/tree/main/packages/plugin-react
react({
jsxRuntime: 'classic',
}),
],
resolve: {
alias: getAlias(),
];
const output: Output = [
{
entryFileNames: '[name].es.js',
banner: getBanner('es'),
format: 'es',
},
cacheDir: `./.cache`,
};
...(mode !== 'analyze' ? cjsOutput : []),
];
const config = getCommonViteConfig({
external: [
'react',
'react-dom',
// 'prop-types',
'rxjs',
'@tool-pack/basic',
'@tool-pack/dom',
],
output,
});
config.build.emptyOutDir = true;
return config;
});
72 changes: 72 additions & 0 deletions packages/react-ui/vite.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { replaceCodePlugin } from 'vite-plugin-replace';
import { visualizer } from 'rollup-plugin-visualizer';
import type { RequiredPart } from '@tool-pack/types';
import type { UserConfig, Plugin } from 'vite';
// import react from '@vitejs/plugin-react';
import { getAlias } from '../../utils';
import pkg from './package.json';
import { resolve } from 'path';

export function getBanner(format: string) {
const date = new Date();
return (
'/*!\n' +
` * ToolPackReactUI(${pkg.name}) v${pkg.version}\n` +
` * Author: ${pkg.author}\n` +
` * Documentation: ${pkg.homepage}\n` +
` * License: ${pkg.license}\n` +
` * File: index.${format}.js\n` +
` * Date: ${date.getFullYear()}-${
date.getMonth() + 1
}-${date.getDate()}\n` +
` */\n`
);
}

const entryPath = resolve(__dirname, './src/index.ts');
/**
* Vite configuration
* https://vitejs.dev/config/
*/
export function getCommonViteConfig(
rollupOptions: Exclude<UserConfig['build'], undefined>['rollupOptions'],
): RequiredPart<UserConfig, 'build'> {
return {
plugins: [
// https://github.com/vitejs/vite/tree/main/packages/plugin-react
// react({
// jsxRuntime: 'classic',
// }),
replaceCodePlugin({
replacements: [
{ to: JSON.stringify('production'), from: 'process.env.NODE_ENV' },
],
}),
{
...(visualizer({ filename: 'temp/analyze.html' }) as Plugin),
apply(_, { mode }) {
return mode === 'analyze';
},
},
],
build: {
outDir: resolve(__dirname, './dist'),
lib: { entry: entryPath },
target: 'modules',
// minify: true,
// cssCodeSplit: true,
// emptyOutDir: true,
rollupOptions,
},
// optimizeDeps: {
// exclude: ['react', 'react-dom', 'react-is', 'rxjs', 'react-jsx-runtime'],
// },
esbuild: {
tsconfigRaw: { compilerOptions: { jsx: 'react' } },
},
resolve: {
alias: getAlias(),
},
cacheDir: `./.cache`,
};
}
Loading

0 comments on commit 0b3d69a

Please sign in to comment.