reactjsbootstrap-4css-modulesbabel-plugin-react-css-modules

React - Use Bootstrap globally with css modules


Pretty new to react and all that stuff, so I need some help here. I recently added the https://github.com/gajus/babel-plugin-react-css-modules plugin to my project. After some troubles I got it to work, so I can now use my local css files with my components. So far so good.

Not I would like to add bootstrap for the whole application. It worked before I added the css-modules plugin...

Here's are the relevant parts of my code (I guess... if I miss something let me know):

index.js (entrypoint of my application):

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'),
    }),
  ],
}

Any advise is welcome. Thank you for your time.

Oh btw: I would also like to introduce scss to my application, I don't know how to do that yet (haven't done any researches, but if someone of you knows how to do it and is open to explain that, I would really appreciate it... don't know if its a big change).

Edit: I totally forgot to add my component:

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>
    );
  }
}

Solution

  • I found the solution by myself. Gonna post it here, may someone will need it one day:

    Define two rules in the webpack config:

    {
      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']
    }