For performance improvements i need to separate the CSS from the JS in my Webpack config into it’s own files. E.g style.css
and main.js
or whatever i chose to call them. Currently I’m importing the css in my main JS-file like import 'style.css'
.
How can i change this?
Webpack config.
const HtmlWebpackPlugin = require('html-webpack-plugin');
const path = require('path');
const isProduction = process.env.NODE_ENV === 'production';
const stylesHandler = 'style-loader';
const config = {
entry: './src/js/index.js',
output: {
path: path.resolve(__dirname, './dist/js'),
filename: 'main.js',
clean: true, // remove unused bundled files
},
module: {
rules: [
{
test: /.(js|jsx)$/i,
loader: 'babel-loader',
},
{
test: /.css$/i,
use: [stylesHandler, 'css-loader', 'postcss-loader'],
},
{
test: /.(eot|svg|ttf|woff|woff2|png|jpg|gif|otf)$/i,
type: 'asset',
},
],
},
};
module.exports = () => {
if (isProduction) {
config.mode = 'production';
} else {
config.mode = 'development';
}
return config;
};
Package.json (scripts)
"scripts": {
"build": "webpack --mode=production --node-env=production",
"build:dev": "webpack --mode=development",
"build:prod": "webpack --mode=production --node-env=production",
"watch": "webpack --watch"
}
3
Answers
You can use the modern html-bundler-webpack-plugin.
This plugin renders HTML templates containing references to source files of script, styles, images, etc.
Very simple usage example:
You can specify script, style and image source files in HTML using a relative path or Webpack aliases.
No additional plugins and loaders required.
The plugin detects all source files referenced in a template and extracts processed assets to the output directory. Compiled JS and CSS can be inlined into generated HTML using the inline plugin option. In the generated HTML and CSS, the plugin substitutes the source filenames with the output filenames.
The generated HTML contains the output filenames of the processed files:
Open in StackBlitz
You can add the
MiniCssExtractPlugin
:If you’re fine with working with scss, you can do something like this:
You want to import the scss for your website, convert it to css and then do some post-processing. In the end it will end up as minified css file in your dist folder. That’s the file you need to serve and import in your html-page.
If you build, you should have a ./dist/styles/_index.min.css file. That’s the file you need to serve. If you’re using express, that’s looks something like this:
app.use(express.static(path.join(__dirname, './styles')))
import it into your html, eg:
<link rel="stylesheet" href="/_index.min.css"/>
NOTE: you need to keep the bundles css file size in to consideration. You probably want to have an _index.scss file per page (code splitting). If you still end up with too big files sizes you need to go even further. For every page, you can have multiple bundled style.min.css files you can lazy load while scrolling.
NOTE: You should be able to use the css-loader in your webpack config, probably between the file-loader and the sass-loader.
You can have a look here where I did something similar: https://github.com/BeeckmanThe1/uploader/blob/main/webpack.config.js
Hope that helps