-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Webpack dev server not working #5326
Comments
We already doing this, please submit a reproducible repository using github and I will show the error to you |
Because you have: ...glob.sync("./src/pages/*.html").map((file) => {
return new HtmlWebpackPlugin({
template: file,
filename: path.basename(file),
});
}), this means that the new html plugin will only appear at startup, that's why when adding a new file and using the dev server you don't see it Anyway it solved it, you need:
const path = require("path");
const webpack = require("webpack");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
const glob = require("glob");
const { processHtmlLoader } = require("./html-partial-processor");
const {compile} = require("sass");
module.exports = (env, argv) => {
const isProduction = argv.mode === "production";
return {
entry: "./src/js/app.js",
output: {
path: path.resolve(__dirname, "dist"),
filename: "js/app.bundle.js",
clean: true,
},
devServer: {
static: {
directory: path.join(__dirname, "dist"),
},
compress: true,
port: 3000,
open: true,
hot: true,
},
module: {
rules: [
{
test: /\.(s[ac]ss|css)$/i,
use: [
isProduction ? MiniCssExtractPlugin.loader : "style-loader",
"css-loader",
"postcss-loader",
"sass-loader",
],
},
{
test: /\.html$/i,
use: [
{
loader: "html-loader",
options: {
preprocessor: processHtmlLoader,
},
},
],
},
{
test: /\.(png|jpe?g|gif|svg|ico|eot|ttf|woff)$/i,
type: "asset/resource",
generator: {
filename: "images/[name][ext]",
},
},
{
test: /\.m?js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
presets: ["@babel/preset-env"],
},
},
},
],
},
resolve: {
extensions: [".js"],
},
plugins: [
{
apply: (compiler) => {
let alreadyAdded = new Set();
const injectPlugin = () => {
for (const page of glob.sync("./src/pages/*.html")) {
if (alreadyAdded.has(page)) {
continue;
}
new HtmlWebpackPlugin({
template: page,
filename: path.basename(page),
}).apply(compiler);
}
};
injectPlugin();
compiler.hooks.thisCompilation.tap("html-pages", (compilation) => {
compilation.contextDependencies.add(path.resolve(__dirname, "/src/pages"));
});
compiler.hooks.invalid.tap("html-pages", () => {
injectPlugin();
})
}
},
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
}),
...(isProduction
? [
new MiniCssExtractPlugin({
filename: "css/app.min.css",
}),
new ImageMinimizerPlugin({
minimizer: {
implementation: ImageMinimizerPlugin.imageminMinify,
options: {
plugins: [
["mozjpeg", { quality: 70 }],
["pngquant", { quality: [0.6, 0.8], speed: 3 }],
["svgo", { removeViewBox: false }],
["gifsicle", { interlaced: true, optimizationLevel: 3 }],
["imagemin-webp", { quality: 75 }],
],
},
},
}),
]
: []),
],
mode: isProduction ? "production" : "development",
};
}; renaming and removing in dev-server/watch mode for HTML pages impossible because hooks used inside plugins cannot be deleted I also recommend using - https://github.com/webpack-contrib/copy-webpack-plugin/ and https://github.com/webpack-contrib/copy-webpack-plugin/?tab=readme-ov-file#transform as alternative solution (no problems with renaming and deleting) feel free to feedback |
Hey there! If you need support, help, or advice then this is not the place to ask.
Please visit GitHub Discussions or StackOverflow instead.
Webpack dev server not working.
The text was updated successfully, but these errors were encountered: