我正在创建组件库。这些组件中的某些只是封装在新组件中的Material-ui库的组件。问题在于,以这种方式在另一个项目中导入组件时,类名称会发生变化,例如:“ MuiAppBar-root”更改为“ jss7”。
我尝试使用withStyles以及themeProvider,但是得到了相同的输出。我相信问题出在组件库的构建或使用create-react-app的项目的comp导入中,但是缺乏关于babel和webpack的知识。
文件夹结构和文件:
/root
- /lib
- /material
- /node_modules
- /src
- /components
- /AppBar
- AppBar.jsx
- index.js
...
-index.js
- ui
webpack.config.js
const fs = require('fs');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const path = require('path');
const webpack = require('webpack');
const packageInfo = require('./package.json');
const outputPath = path.join(__dirname, 'ui');
const srcPath = path.join(__dirname, 'src');
const componentExternals = [];
const entryPoints = {
index: './src/components/index.js'
};
const externals = [].concat(Object.keys(packageInfo.dependencies));
module.exports = (env, argv) => {
const isDevelopment = argv.mode === 'development';
return {
entry: entryPoints,
output: {
path: outputPath,
filename: 'index.js',
publicPath: '/ui/',
library: packageInfo.name,
libraryTarget: 'umd'
},
resolve: {
modules: ['node_modules', path.resolve(__dirname, 'ui/index')],
extensions: [
'.scss',
'.js',
'.jsx',
'.json',
'.png',
'.gif',
'.jpg',
'.svg'
]
},
externals,
plugins: [
new webpack.optimize.OccurrenceOrderPlugin(true),
new MiniCssExtractPlugin({
filename: isDevelopment ? '[name].css' : '[name].css'
})
],
module: {
rules: [
{
test: /\.(js|jsx)$/,
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env', '@babel/react']
},
include: srcPath
},
{
test: /\.module\.s(a|c)ss$/,
loader: [
isDevelopment
? 'style-loader'
: MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
modules: true,
localIdentName:
'[folder]_[local]__[hash:base64:5]',
camelCase: true,
sourceMap: isDevelopment
}
},
{
loader: 'sass-loader',
options: {
sourceMap: isDevelopment
}
}
]
},
{
test: /\.s(a|c)ss$/,
exclude: /\.module.(s(a|c)ss)$/,
loader: [
isDevelopment
? 'style-loader'
: MiniCssExtractPlugin.loader,
'css-loader',
{
loader: 'sass-loader',
options: {
sourceMap: isDevelopment
}
}
]
},
{
test: /\.svg$/,
loader: 'url-loader?limit=10000&mimetype=image/svg+xml'
}
]
}
};
};
AppBar.jsx:
import React, { Component } from 'react';
import { default as WAppBar } from '@material-ui/core/AppBar';
class AppBar extends Component {
render() {
return (
<WAppBar {...this.props} />
);
}
}
export default AppBar;
输出:
<header class="jss11 jss17 jss2 jss1 jss7 jss9 Simulacoes_navbar__2d50u">
预期输出:
<header class="MuiPaper-root MuiPaper-elevation4 MuiAppBar-root MuiAppBar-positionRelative Simulacoes_navbar__2d50u">
在将material-ui库直接导入到项目中时,我希望在输出中具有相同的类名。