I am trying to figure out how to use webpack 5 for a multi-page site. It is currently placing most of the files as expected into the ‘dist’ folder however, when I am working in development, my SCSS/CSS files are being ignored. I have also dropped my ‘dist’ folder into a server and, the JS files are there but the JS isn’t working. Any help would be appreciated.
In the index.js and purchaselinks_object.js files I am also importing the .scss file from the correct location,
import '../scss/main.scss';
webpack common
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const pages = ['index', 'products'];
module.exports = {
entry: {
index: {
import: ['./src/js/index.js'],
dependOn: 'shared',
},
products: {
import: ['./src/js/purchaselinks_object.js', './src/js/custom_select.js'],
dependOn: 'shared',
},
shared: 'js-cookie',
},
module: {
rules: [
{
test: /.m?js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env'],
},
},
},
{
test: /.(png|svg|jpg|jpeg|gif|webp)$/i,
type: 'asset/resource',
},
{
test: /.html$/i,
use: 'html-loader',
},
],
},
optimization: {
splitChunks: {
chunks: 'all',
},
},
plugins: [].concat(
pages.map(
page =>
new HtmlWebpackPlugin({
inject: true,
template: `./src/${page}.html`,
filename: `${page}.html`,
chunks: [page],
}),
),
),
output: {
filename: '[name].bundle.js',
path: path.resolve(__dirname, 'dist'),
clean: true,
assetModuleFilename: pathData => {
const filepath = path
.dirname(pathData.filename)
.split('/')
.slice(1)
.join('/');
return `${filepath}/[name][ext][query]`;
},
},
};
webpack dev
const { merge } = require('webpack-merge');
const common = require('./webpack.common');
module.exports = merge(common, {
mode: 'development',
devtool: 'inline-source-map',
devServer: {
static: './dist',
},
module: {
rules: [
{
test: /.css$/i,
use: [
'style-loader',
{ loader: 'css-loader', options: { sourceMap: true } },
{ loader: 'postcss-loader', options: { sourceMap: true } },
],
},
{
test: /.s[ac]ss$/i,
use: [
'style-loader',
{ loader: 'css-loader', options: { sourceMap: true } },
{ loader: 'postcss-loader', options: { sourceMap: true } },
{ loader: 'sass-loader', options: { sourceMap: true } },
],
},
],
},
});
webpack production
const { merge } = require('webpack-merge');
const common = require('./webpack.common');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
module.exports = merge(common, {
mode: 'production',
module: {
rules: [
{
test: /.css$/i,
use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader'],
},
{
test: /.s[ac]ss$/i,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
'postcss-loader',
'sass-loader',
],
},
],
},
plugins: [
new MiniCssExtractPlugin({
filename: '[name].css',
}),
],
});
I have redone this configuration multiple times. I consistently have been getting the following files in my dist folder
index.bundle.js
index.css
products.bundle.js
products.css
shared.bundle.js
I also see them in the .html documents as expected within the dist folder. What I am not getting is a runtime.bundle.js file which before I tried turning this into a multipage site, I was getting. Not sure if that is the difference.
Erik Nuber is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.