I try to build in production with Webpack5 + MiniCssExtractPlugin + html-webpack-plugin and other plugins but i'm not able to have css tag included in index.html.
Every time css files are append by javascript with "flash of unstyled content" (FOUC) problem.
I use npm run build with this easy command: webpack --config webpack.prod.js
webpack.prod.js
const path = require('path');
const common = require('./webpack.config');
const { merge } = require('webpack-merge');
const {CleanWebpackPlugin} = require('clean-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
module.exports = merge(common,{
mode: 'production',
output: {
path: path.resolve(__dirname, 'build'),
publicPath: '',
filename: '[name].[hash].js',
},
plugins: [
new CleanWebpackPlugin({}),
new MiniCssExtractPlugin({
filename:'[name].[hash].css'
})
],
module: {
rules: [
{
test: /\.css$/i,
use: [
MiniCssExtractPlugin.loader,
'css-loader'
]
},
{
test: /\.scss$/i,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
'sass-loader'
]
}
]
}
});
webpack.config.js
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: {
index: './src/index.js',
vendor: './src/vendor.js'
},
plugins: [
new HtmlWebpackPlugin({
inject: 'body',
template: './src/index.html',
scriptLoading: "defer"
})
],
module: {
rules: [
{
test: /\.html$/,
use: [{
loader: 'html-loader'
}]
},
{
test: /\.(png|jpe?g|gif)$/i,
use: [{
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: 'assets/images/'
}
}],
},
{
test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
use: [{
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: 'assets/fonts/'
}
}]
}
]
}
};
and this is the index.html result:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="msapplication-tap-highlight" content="no"/>
<title>title</title></head>
<body>
all code here...
<script defer="defer" src="index.19fae54ea5525bb23f67.js"></script>
<script defer="defer" src="vendor.19fae54ea5525bb23f67.js"></script>
</body>
</html>
and inside the build folder i have a list of *.js and *.css files with hash and assets folder with his content.
Thank you for help