【问题标题】:React - Use Bootstrap globally with css modulesReact - 全局使用 Bootstrap 和 CSS 模块
【发布时间】:2018-09-12 08:36:29
【问题描述】:

反应和所有这些东西都很新,所以我需要一些帮助。我最近将https://github.com/gajus/babel-plugin-react-css-modules 插件添加到我的项目中。经过一些麻烦后,我让它工作了,所以我现在可以将我的本地 css 文件与我的组件一起使用。到目前为止一切顺利。

我不想为整个应用程序添加引导程序。在我添加 css-modules 插件之前它已经工作了......

这是我的代码的相关部分(我猜......如果我错过了什么,请告诉我):

index.js(我的应用程序的入口点):

import 'bootstrap/dist/css/bootstrap.min.css';
...

.babelrc:

{
    "presets": [
        "react"
    ],
    "plugins": [
      ["react-css-modules", {
        "exclude": "node_modules"
      }]
    ]
}

webpack.config.js

/* webpack.config.js */

const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')

module.exports = {
  devtool: 'eval',

  entry: [
    path.resolve('src/index.js'),
  ],

  output: {
    path: path.resolve('build'),
    filename: 'static/js/bundle.js',
    publicPath: '/',
  },

  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        include: path.resolve('src'),
        loader: 'babel-loader',
      },
      {
        test: /\.css$/,
        use: [
          'style-loader',
          {
            loader: 'css-loader',
            options: {
              importLoaders: 1,
              modules: true,
              localIdentName: '[path]___[name]__[local]___[hash:base64:5]', // Add naming scheme
            },
          },
        ],
      },

    ],
  },

  plugins: [
    new HtmlWebpackPlugin({
      inject: true,
      template: path.resolve('src/index.html'),
    }),
  ],
}

欢迎任何建议。感谢您的宝贵时间。

哦,顺便说一句:我也想在我的应用程序中引入 scss,我还不知道该怎么做(还没有做过任何研究,但如果你们中有人知道怎么做并且愿意解释那,我真的很感激......不知道它是否有很大的变化)。

编辑: 我完全忘了添加我的组件:

import React, { Component } from "react";
import { Switch, Route, withRouter } from "react-router-dom";
import './style.css';

export default class HomeContainer extends Component {
  constructor() {

    super();
  }
  /* Test */
  render() {
    return (
      <div styleName="title">
        This woorks (styleprops from "title" are appliced"
        <button type="button" className="btn btn-primary">Primary</button> // Doesn't style the button :(
      </div>
    );
  }
}

【问题讨论】:

    标签: reactjs bootstrap-4 css-modules babel-plugin-react-css-modules


    【解决方案1】:

    我自己找到了解决方案。会发在这里,也许有一天会有人需要它:

    在 webpack 配置中定义两条规则:

    {
      test: /\.css$/,
      exclude: /node_modules/,
      use: [
        'style-loader', {
          loader: 'css-loader',
          options: {
            importLoaders: 2,
            modules: true,
            localIdentName: '[path]___[name]__[local]___[hash:base64:5]', // Add naming scheme
          },
        },
      ],
    },
    
    // Second CSS Loader, including node_modules, allowing to load bootstrap globally over the whole project.
    {
      test: /\.css$/,
      include: /node_modules/,
      use: ['style-loader', 'css-loader']
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-13
      • 2018-10-06
      • 2021-03-16
      • 2016-10-10
      • 2017-03-09
      • 2017-02-09
      • 2018-07-07
      相关资源
      最近更新 更多