Skip to content
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

simplify webpack config #1

Closed
wants to merge 1 commit into from
Closed

simplify webpack config #1

wants to merge 1 commit into from

Conversation

webdiscus
Copy link

Hello @petrushin-ai

I'm the author of the webpack-remove-empty-scripts which is used in your project.
This plugin is the crutch 🩼 for the mini-css-extract-plugin issue.

I want to demonstrate you my the other plugin, html-bundler-webpack-plugin, which simplifies Webpack config.

Using the html-bundler-webpack-plugin:

  • An entry point is an HTML template.
  • Source scripts and styles can be specified directly in HTML using <script> and <link> tags.
  • Resolving source assets specified in standard attributes href src srcset etc.
  • Resolving URLs in CSS without sourceMap, the sourceMap: true loader option is not required.
  • Inline JS, CSS, SVG, PNG without additional plugins and loaders.
  • Support for template engines such as Eta, EJS, Handlebars, Nunjucks out of the box.
  • Auto processing many HTML templates using the entry path.
  • disable extracting comments from JS files to the *.LICENSE.txt file.

Just one html-bundler-webpack-plugin replaces the functionality of many plugins and loaders:

  • html-webpack-plugin
  • mini-css-extract-plugin
  • webpack-remove-empty-scripts
  • style-loader
  • html-loader
  • resolve-url-loader
  • terser-webpack-plugin (for extractComments: false )

The plugin resolves references in the HTML template and replaces them with output filenames in the generated HTML.

I have changed the script from npm run dev to **default ** npm start. You can do it revert.

@@ -4,7 +4,7 @@
"description": "",
"main": "",
"scripts": {
"dev": "webpack serve --mode development",
"start": "webpack serve --mode development",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Defaults, for development should be used the start script name.

@@ -0,0 +1,9 @@
<%~ include('partials/head.html') %>
Copy link
Author

@webdiscus webdiscus Oct 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use any templating engine to include partials, e.g. very fast Eta (ESJ like).

@@ -4,4 +4,7 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Webpack 5 Project Starter</title>
<link href="@images/favicon.ico" rel="icon" />
<link href="@styles/style.scss" rel="stylesheet" />
<script src="@scripts/global.js" defer="defer"></script>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use the source files of styles, scripts, images etc. directly in the HTML using the relative path to HTML file or an alias specified in Webpack config

use: [
"css-loader",
isDev ? null : "postcss-loader",
"sass-loader", // minimizes generated CSS in production mode
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need to specify sourceMap: true options because the HTML Bundler Plugin resolves URLs in CSS/SCSS without the resolve-url-loader

}
}
},
minimizer: [
new CssMinimizerPlugin(),
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sass-loader minify generated CSS in production mode,
the CssMinimizerPlugin is not needed

}
}
},
minimizer: [
new CssMinimizerPlugin(),
new TerserPlugin({ extractComments: false })
Copy link
Author

@webdiscus webdiscus Oct 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The HTML Bundler Plugin avoid the extracting of comments into a file,
the TerserPlugin is not needed for that

name: 'vendors',
chunks (chunk) {
// exclude `style`
return chunk.name !== 'style';
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can specify source SCSS file directly in HTML, not in a JS file,
therefore this option is not needed

loader: 'html-loader',
options: {
minimize: false,
preprocessor: processNestedHtml
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the preprocessor option of the HtmlBundlerPlugin to specify a template engine.
This rule for .html is not needed anymore.

module.exports = {
entry: entryPoints('./src/js'),
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the HtmlBundlerPlugin, an entry point is HTML, not JS.
All HTML templates you can specify in the entry option of the plugin.

}

return [
MiniCssExtractPlugin.loader,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The HtmlBundlerPlugin extracts generated CSS self,
the MiniCssExtractPlugin is not needed.

function styleLoaders() {
if (isDev) {
return [
"style-loader",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The HtmlBundlerPlugin can inline extracted CSS into HTML,
no style-loader is required.

}

function generateHtmlPlugins(templateDir) {
const templateFiles = fs.readdirSync(path.resolve(__dirname, templateDir), {withFileTypes: true});
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The HtmlBundlerPlugin can automatically handle HTML templates from the entry path option.

"babel-loader": "^8.2.5",
"css-loader": "^6.7.1",
"css-minimizer-webpack-plugin": "^3.4.1",
"filemanager-webpack-plugin": "^7.0.0-beta.0",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove unused filemanager-webpack-plugin

const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlBeautifyPlugin = require('@nurminen/html-beautify-webpack-plugin');
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In production mode use the minfy of HTML (good practice), not beautify to avoid some issues with spaces in HTML.

@webdiscus webdiscus closed this by deleting the head repository Jan 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant