我一直在寻找如何结合以下内容:ReactJS+ReactStrap和CSS-Modules(react-css-modules和/或boostrap-css-modules),但是,我似乎无法将这三个模块组合起来实现所需的效果(或在网上找到任何帮助)。
这个想法是为了使ReactStrap元素可用,例如:
import React from 'react';
import { Button } from 'reactstrap';
export default (props) => {
return (
<Button color="danger">Danger!</Button>
);
};
但同时也允许我使用 CSS-Modules,以便最终结果类似于:
import React from 'react';
import CSSModules from 'react-css-modules';
import styles from './mybutton.css';
class Test extends React.Component {
render () {
return <Button color='danger' styleName='myButton'>Danger</div>;
}
}
export default CSSModules(Table, styles);
其中 mybutton.css 可能类似于:
.myButton {
composes: btnPrimary from "bootstrap-css-modules/css/buttons.css";
background-color: dodgerblue;
border-color: dodgerblue;
}
我也在使用Webpack,所以关于如何在Webpack中使用它,我甚至不知道从哪里开始。
编辑:
我使用的方法如下:
npm install --save bootstrap@4.0.0-alpha.6
npm install --save reactstrap react-addons-transition-group react-addons-css-transition-group react react-dom
npm install --save bootstrap-css-modules
这是我的Webpack配置
const webpack = require('webpack');
const path = require('path');
const HtmlwebpackPlugin = require('html-webpack-plugin');
const ROOT_PATH = path.resolve(__dirname);
module.exports = {
devtool: process.env.NODE_ENV === 'production' ? '' : 'source-map',
entry: [
path.resolve(ROOT_PATH, 'app/src/index'),
],
module: {
rules: [
{
enforce: 'pre',
test: /\.jsx?$/,
loader: process.env.NODE_ENV === 'production' ? [] : ['eslint-loader'],
include: path.resolve(ROOT_PATH, 'app'),
},
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel-loader',
query: {
presets: ['react'],
},
},
{
test: /\.scss$/,
loaders: ['style-loader', 'css-loader', 'sass-loader'],
}],
},
resolve: {
extensions: ['.js', '.jsx'],
},
output: {
path: process.env.NODE_ENV === 'production' ? path.resolve(ROOT_PATH, 'app/dist') : path.resolve(ROOT_PATH, 'app/build'),
publicPath: '/',
filename: 'bundle.js',
},
devServer: {
contentBase: path.resolve(ROOT_PATH, 'app/dist'),
historyApiFallback: true,
hot: true,
inline: true,
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new HtmlwebpackPlugin({
title: 'testapp',
}),
],
};
谢谢
css-loader
的 webpack 配置吗? - Nagaraj Tantri