I’m just learning webpack. As an exercise, I’ve used it to bundle Bootstrap. If I use the bundle with one of their basic examples, the Starter Template, I get a FOUC if I put <script src="./main.js"></script>
at the end of the <body>
. There’s no FOUC if I put it at the beginning of the <body>
.
Should the <script>
go at the beginning of the <body>
? Or should it go at the end of the <body>
, in which case some other method should be used to prevent the FOUC? For example, use mini-css-extract-plugin to create a separate CSS file rather than include it in the webpack JS bundle as described in other posts like this one.
Code
dist/index.html
Same as Starter Template | Bootstrap except
- Remove the stylesheet
<link>
and JS<script>
lines - Insert
<script src="./main.js"></script>
- At beggining of
<body>
: no FOUC - At end of
<body>
: FOUC
- At beggining of
webpack.config.js
const path = require('path');
module.exports = {
mode: 'development',
entry: './src/index.js',
output: {
filename: 'main.js',
path: path.resolve(__dirname, 'dist'),
},
module: {
rules: [
{
test: /\.(scss)$/,
use: [{
loader: 'style-loader', // inject CSS to page
}, {
loader: 'css-loader', // translates CSS into CommonJS modules
}, {
loader: 'postcss-loader', // Run postcss actions
options: {
plugins: function () { // postcss plugins, can be exported to postcss.config.js
return [
require('autoprefixer')
];
}
}
}, {
loader: 'sass-loader' // compiles Sass to CSS
}]
}
]
}
};
src/index.js
import 'bootstrap';
import './scss/index.scss';
src/scss/index.scss
@import "~bootstrap/scss/bootstrap";
@import "custom";
Where scss/_custom.scss
is trivial.
Environment: webpack: 4.44, bootstrap: 4.4, Chrome: v80, Firefox: v78, Edge: v44 (these browsers all had a FOUC/no-FOUC as described above).