React + Webpack + Material UI样式的生产中断

时间:2020-01-23 18:04:24

标签: node.js reactjs npm webpack material-ui

我正在尝试使用Material UI创建一个可重用的React组件,并将其npm link应用于另一个应用程序。使用webpack将组件和应用程序捆绑在一起。该应用程序在开发中很好地呈现了该组件,但是当我捆绑该应用程序时,该组件开始破坏material-ui样式。

我尝试过的一些解决方案包括:

我认为在@material/core中定义peerDependencies可以解决问题,但是每次我使用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,
  },
};

任何帮助将不胜感激!预先感谢。

1 个答案:

答案 0 :(得分:2)

如果您查看npm link上的文档,这实际上是很有意义的:

首先,程序包文件夹中的npm链接将在全局文件夹{prefix} / lib / node_modules /中创建一个符号链接,该符号链接链接到执行npm link命令的程序包。

请注意,该命令仅在本地创建 符号链接,因此在构建/部署时,逻辑上将遵循找不到该包的状态。

我的建议是为您的自定义组件创建一个scoped package。创建一个npm帐户,上传您的软件包,然后将其添加到您的项目中,如下所示:

npm install @brettoberg/reusable-component

现在,Webpack和任何其他系统都应该能够找到它,因为它已经发布。