i'm starting a new project using reactjs ES6 and webpack
using react-static-boilerplate starter question is how can i import bootstrap4 into the build proccess ?
i dont want to use the bootstrap.css generated file, instead i want webpack to build it for me so i can get to change its @variables and apply my theme etc.
i started project by cloning boilerplate
> git clone -o react-static-boilerplate -b master --single-branch \ https://github.com/kriasoft/react-static-boilerplate.git MyApp >cd MyApp && npm install
installed bootstrap using npm
npm install [email protected]
now if i required the main bootstrap file into my index.js it will load fine. but how can i include the sass files of bootsrap to start customizing it ?
To enable scss in Create React App you will need to install sass . To customize Bootstrap, create a file called src/custom. scss (or similar) and import the Bootstrap source stylesheet. Add any overrides before the imported file(s).
In your custom. scss , you'll import Bootstrap's source Sass files. You have two options: include all of Bootstrap, or pick the parts you need. We encourage the latter, though be aware there are some requirements and dependencies across our components.
It basically boils down to the following steps: Download bootstrap using npm install react-bootstrap. Install SASS pre-processor (https://sass-lang.com/install) Create a scss file for overriding bootstrap's _variables.
Install sass-loader
npm install sass-loader --save-dev
Then you need to configure your webpack to test all scss files so it can handle it. Here it is how it is done
{test: /\.scss$/, loaders: [ 'style', 'css', 'sass' ]}
If you got error like cannot resolve node-sass then install
npm i node-sass --save-dev
import "bootstrap/scss/bootstrap.scss"
Example in your own scss file
$btn-font-weight:bold;
and then import the component you want to override or the whole bootstrap.scss
@import '~bootstrap/scss/bootstrap.scss';
$btn-font-weight:bold; @import '~bootstrap/scss/bootstrap.scss';
import "bootstrap/scss/bootstrap.scss" import "./style.scss"
run npm install
and npm start
got to localhost:8080
Seems like the boilerplate doesn't use sass-loader, and doesn't look for .scss
files.
So first off install npm i sass-loader --save
Then under the loaders part in the webpack config you should add something like this:
webpack.config.js
var path = require('path'); var nodeModules = path.resolve(path.join(__dirname, 'node_modules')); // this is the entire config object const config = { // ... loaders: [ // ... { test: /\.(css|scss)$/, include: [ path.join(nodeModules, 'bootstrap'), ], loaders: ["style", "css", "sass"] } ] // ... };
Now, if you want to play around with bootstrap's .scss
variables, you can do so like this:
styles/app.scss
$brand-warning: pink !default; @import '~bootstrap/scss/bootstrap.scss';
and in your main.js
put in the style import
import "styles/app.scss";
Also, I should mention, this seems very close to this answer
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With