React + Webpack + Material UI样式在生产环境中出现问题

7
我试图创建一个可重用的React组件,使用Material UI,并通过npm link将其连接到另一个应用程序。该组件与应用程序一起使用webpack打包。在开发中,应用程序可以正常渲染组件,但当我打包应用程序时,组件开始破坏material-ui样式。
我尝试过的解决方案包括: 我曾经认为在peerDependencies中定义@material/core会解决这个问题,但每次使用Material-UI组件时,应用程序都会抛出Invalid Hook Call Warning

似乎什么都不起作用 ☹️


组件的 package.json 文件:
{
  "name": "component",
  "version": "1.0.0",
  "description": "",
  "main": "build/index.js",
  "scripts": {
    "test": "jest",
    "start": "webpack --watch",
    "build": "webpack --optimize-minimize -p",
    "dist": "npm run build"
  },
  "peerDependencies": {
    "@material-ui/core": "^3.2.0 || ^4.0.0",
    "react": ">=16.8.0",
    "react-dom": ">=16.8.0"
  },
  "devDependencies": {
    "@babel/core": "^7.2.2",
    "@babel/plugin-proposal-class-properties": "^7.5.0",
    "@babel/preset-env": "^7.5.4",
    "@babel/preset-react": "^7.0.0",
    "@material-ui/core": "^4.9.0",
    "@material-ui/icons": "^3.0.2",
    "babel-cli": "^6.26.0",
    "babel-core": "^7.0.0-bridge.0",
    "babel-eslint": "^7.2.3",
    "babel-jest": "^23.6.0",
    "babel-loader": "^8.0.5",
    "babel-plugin-transform-object-rest-spread": "^6.26.0",
    "babel-plugin-transform-react-jsx": "^6.24.1",
    "babel-plugin-transform-runtime": "^6.23.0",
    "babel-runtime": "^6.26.0",
    "enzyme": "^3.7.0",
    "enzyme-adapter-react-16": "^1.7.0",
    "eslint": "^4.19.1",
    "eslint-config-airbnb": "^16.1.0",
    "eslint-loader": "^1.7.1",
    "eslint-plugin-import": "^2.1.0",
    "eslint-plugin-jsx-a11y": "^6.1.2",
    "eslint-plugin-react": "^7.7.0",
    "faker": "^4.1.0",
    "husky": "^1.3.1",
    "jest": "^23.6.0",
    "jest-styled-components": "^6.3.1",
    "react": "^16.12.0",
    "react-dom": "^16.12.0",
    "sinon": "^7.2.2",
    "webpack": "^4.39.2",
    "webpack-cli": "^3.3.7"
  },
  "dependencies": {
    "clsx": "^1.0.4",
    "prop-types": "^15.6.2"
  }
}

组件的webpack.config.js

const path = require('path');

module.exports = {
  entry: './src/index.jsx',
  mode: 'production',
  output: {
    path: path.resolve(__dirname, 'build'),
    filename: 'index.js',
    libraryTarget: 'commonjs2',
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        include: path.resolve(__dirname, 'src'),
        exclude: /(node_modules|bower_components|build)/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/env'],
          },
        },
      },
    ],
  },
  resolve: { extensions: ['*', '.js', '.jsx'] },
  externals: {
    react: 'react',
  },
  optimization: {
    minimize: true,
  },
};

任何帮助都将不胜感激!提前致谢。

亲爱的@BrettOberg, 请提供GitHub或GitLab或BitBucket存储库链接以查看您重现问题的代码。 对于这种情况,我们应该看到您的代码,而不仅仅是配置。 - AmerllicA
谢谢@AmerllicA,我会在以后的帖子中这样做。 - Brett Oberg
你好,serraosays,这个问题有更新了吗?我遇到了类似的问题,我链接了另一个使用 Material UI 的项目。在开发模式下它可以正常工作,但是在生产构建中会出现问题 :/ - Daniel Gabor
1个回答

2
如果你查看npm link文档,这实际上是有道理的:
首先,在包文件夹中运行npm link将在全局文件夹{prefix}/lib/node_modules/中创建一个符号链接,该符号链接链接到执行npm link命令的包。
请注意,该命令只会在本地创建符号链接,因此当你构建/部署时,逻辑上应该无法找到该包。
我的建议是为自定义组件创建一个作用域包。创建一个npm账户,上传你的包,然后像这样将其添加到你的项目中: npm install @brettoberg/reusable-component 现在,webpack和任何其他系统都应该能够找到它,因为它已经发布了。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接