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

chore: optimise webpack config for using html-bundler-webpack-plugin #164

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

webdiscus
Copy link

Hello @d3ward,

You have very useful tools, thanks.

But in your project is used outdated Webpack loader template-ejs-loader which is doesn't maintained over 2 years.

I'm the author of the html-bundler-webpack-plugin and I want help you to optimise and simplify the Webpack configuration.

Using the Bundler Plugin, an entry point is a template.
All your source asset files (scripts, styles, images, etc.) can be specified directly in the HTML template.
The plugin resolves source files of assets in templates and replaces them with correct output URLs in the generated HTML.
The resolved assets will be processed via Webpack plugins/loaders and placed into the output directory.

The Bundler Plugin replaces the functionality of many plugins and loaders such as:

  • html-webpack-plugin
  • mini-css-extract-plugin
  • ejs-loader
  • template-ejs-loader
  • html-loader
  • style-loader
  • copy-webpack-plugin

How to test

You can see how it works in the forked branch:

git clone https://github.com/webdiscus/demo-toolz.git
cd demo-toolz
npm i
npm run dev


module.exports = {
context: config.src,
entry: {
Copy link
Author

Choose a reason for hiding this comment

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

the source JavaScript files can be defined directly in EJS template using <script src="..."> tag

output: {
filename: 'js/[name].js',
Copy link
Author

Choose a reason for hiding this comment

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

the filename option should be defined in the HtmlBundlerPlugin options

path: config.build,
clean: false,
assetModuleFilename: '[path][name][ext]',
assetModuleFilename: 'assets/[name].[hash:8][ext]',
Copy link
Author

Choose a reason for hiding this comment

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

generates unique image filenames, e.g. assets/icon.1234abcd.svg

publicPath: '/toolz/'
},
plugins: [
new CopyWebpackPlugin({
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 resolves all source assets (images, fonts, etc) used in templates and saves into output directory.
So you don't need to copy the source images.

}),
...pages.map(
(page) =>
new HTMLWebpackPlugin({
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 is best modern alternative to HTMLWebpackPlugin

],
module: {
rules: [
{
test: /\.(png|svg|jpg|jpeg|gif)$/i,
type: 'asset/resource'
},
{
test: /\.ejs$/i,
use: ['html-loader', 'template-ejs-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 replaces the functionality of both the html-loader and template-ejs-loader

@@ -70,7 +69,6 @@ module.exports = {
{
test: /\.(sa|sc|c)ss$/,
use: [
MiniCssExtractPlugin.loader, // Extract CSS from commonjs
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 replaces the functionality of MiniCssExtractPlugin

"html-loader": "^4.2.0",
"html-webpack-plugin": "^5.5.3",
"mini-css-extract-plugin": "^2.7.6",
"node-sass": "^9.0.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 packages

<%- include('partials/head.ejs',
{
page: '404',
title:'Toolz by d3ward - 404',
description:'Page not found',
url: 'd3ward.github.io/toolz/',
preview_thumbnail:'https://d3ward.github.io/toolz/src/preview_toolz.png',
preview_thumbnail:'./assets/preview_toolz.png',
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 relative path to the source image file, so that it will be coped to output directory automatically

keywords:'toolz,web tools,tools,browser testing,font testing,viewport testing,adblock testing, ad blocker testing,performance,toolkit,web design,open source'
}) %>
<link href="./sass/404.sass" rel="stylesheet" />
<script src="./js/404.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 define source files of script and styles directly in EJS template

<body class="_overflowhidden">

<%- include('partials/adblock/results.ejs') %>
<% const adblock_compatibility = require('./data/adblock_compatibility.json') %>
Copy link
Author

Choose a reason for hiding this comment

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

pass the external data from adblock_compatibility.json into EJS template via data option of the HtmlBundlerPlugin

@@ -1,4 +1,3 @@
import '../sass/404.sass'
Copy link
Author

Choose a reason for hiding this comment

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

the source SASS files can be defined directly in EJS template using <link href="..." rel="stylesheet"> tag

<!-- Google / Search Engine Tags -->
<meta itemprop="name" content="<%= title %>">
<meta itemprop="description" content="<%= description %>">
<meta itemprop="image" content="https://d3ward.github.io/toolz/assets/preview_toolz.png">
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 source file of the image, the plugin resolves the image and copy into output directory

<meta name="twitter:title" content="<%= title %>">
<meta name="twitter:description" content="<%= description %>">
<meta name="twitter:image" content="<%= preview_thumbnail %>">

Copy link
Author

Choose a reason for hiding this comment

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

fix formatting: mixed tabs and spaces

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