You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何构建自定义Material UI主题库并托管至GitHub实现跨项目复用

解决方案

1. 创建自定义MUI主题库

初始化项目

在本地新建文件夹并初始化npm包:

mkdir custom-mui-theme && cd custom-mui-theme
npm init -y

安装依赖

安装MUI核心依赖和构建工具:

npm install @mui/material @emotion/react @emotion/styled --save
npm install rollup @rollup/plugin-babel @rollup/plugin-node-resolve rollup-plugin-terser --save-dev

编写自定义主题与Provider组件

在src目录下创建两个核心文件:

src/theme.js

import { createTheme } from '@mui/material/styles';

// 自定义主题:修改按钮颜色、圆角等配置
const customTheme = createTheme({
  palette: {
    primary: {
      main: '#your-custom-primary-color', // 替换为你的自定义主色
    },
    secondary: {
      main: '#your-custom-secondary-color',
    },
  },
  shape: {
    borderRadius: 8, // 自定义全局圆角大小
  },
  // 可按需添加更多配置(如Typography、组件默认样式等)
});

export default customTheme;

src/ThemeProvider.js

import React from 'react';
import { ThemeProvider as MuiThemeProvider } from '@mui/material/styles';
import customTheme from './theme';

// 封装MUI的ThemeProvider,默认注入自定义主题
export default function ThemeProvider({ children }) {
  return (
    <MuiThemeProvider theme={customTheme}>
      {children}
    </MuiThemeProvider>
  );
}

配置Rollup打包

创建rollup.config.js,确保打包输出兼容不同模块规范:

import babel from '@rollup/plugin-babel';
import resolve from '@rollup/plugin-node-resolve';
import terser from 'rollup-plugin-terser';

export default {
  input: 'src/ThemeProvider.js',
  output: [
    {
      file: 'dist/index.js',
      format: 'esm',
      sourcemap: true,
    },
    {
      file: 'dist/index.umd.js',
      format: 'umd',
      name: 'CustomMuiTheme',
      sourcemap: true,
    },
  ],
  plugins: [
    resolve(),
    babel({ babelHelpers: 'bundled', exclude: 'node_modules/**' }),
    terser(),
  ],
  external: ['@mui/material/styles', '@emotion/react', '@emotion/styled', 'react'], // 排除外部依赖,避免重复打包
};

2. 配置package.json

修改package.json关键字段,适配GitHub Packages发布:

{
  "name": "@your-github-username/custom-mui-theme", // 必须是@用户名/包名格式
  "version": "1.0.0",
  "main": "dist/index.js",
  "module": "dist/index.js",
  "files": ["dist"],
  "scripts": {
    "build": "rollup -c",
    "prepublishOnly": "npm run build"
  },
  "peerDependencies": {
    "@mui/material": "^5.0.0",
    "@emotion/react": "^11.0.0",
    "@emotion/styled": "^11.0.0",
    "react": "^17.0.0 || ^18.0.0"
  },
  "publishConfig": {
    "registry": "https://npm.pkg.github.com/"
  }
}

3. 发布到GitHub Packages

  1. 在GitHub创建与包名一致的公开仓库。
  2. 本地登录GitHub npm registry:
npm login --registry=https://npm.pkg.github.com

输入GitHub用户名和带write:packages权限的个人访问令牌。
3. 执行发布命令:

npm publish

4. 新项目中使用

  1. 安装自定义主题包:
npm install @your-github-username/custom-mui-theme
  1. 在项目根组件中替换MUI默认Provider:
import React from 'react';
import ThemeProvider from '@your-github-username/custom-mui-theme';
import AppContent from './AppContent';

function App() {
  return (
    <ThemeProvider>
      <AppContent />
    </ThemeProvider>
  );
}

export default App;

此后项目中所有MUI组件会自动使用你的自定义主题,无需复制主题文件。

内容的提问来源于stack exchange,提问作者Pasindu Dineth Peiris

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 06:35:21