Its my first time configuring a webpack in a React project. I have configured the Sass and the glob loader to dynamically import all the SCSS files from my components folder. The problem is all the styles are getting appended in the global Style tag in head. How can i create a minified CSS file of my project. I tried using MiniCSSExtract plugin, but i dont know whether its used to create a minified file of the project styles. I have posted my webpack.config.js file below.
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
var HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
template: __dirname + '/public/index.html',
filename: 'index.html',
inject: 'body'
});
module.exports = {
entry: ['./src/main.js', './src/main.scss'],
output: {
path: path.join(__dirname, '/dist'),
filename: 'bundle.js',
publicPath: '/'
},
devServer: {
inline: false,
contentBase: "./dist",
port: 8001
},
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel-loader',
},
{
test: /\.js$/,
use: 'webpack-import-glob-loader'
},
{
test: /\.scss$/,
use: ['style-loader', 'css-loader', 'sass-loader', 'webpack-import-glob-loader']
},
{
test: /\.json$/,
loader: 'json'
},
{
test: /\.css$/i,
use: [MiniCssExtractPlugin.loader, 'style-loader', 'css-loader'],
}
]
},
devServer: {
historyApiFallback: true
},
plugins:[
new MiniCssExtractPlugin(),
HtmlWebpackPluginConfig
]
}
Here is my index.html where i have added the bundle.js which gets created by webpack.
<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "UTF-8">
<title>React App</title>
</head>
<body>
<div id="app"></div>
<script src='bundle.js'></script>
</body>
</html>