To convert classNames in HTML into hashed classNames in a VITE configuration with multiple pages, you can follow these steps:
posthtml-hash
plugin:npm i posthtml-hash --save-dev
posthtml.config.js
file with the following contents:const posthtmlHash = require("posthtml-hash");
module.exports = {
plugins: [
posthtmlHash({
selector: "[class]",
algorithm: "md5",
hashLength: 8,
manifest: "./dist/assets/hash-manifest.json",
}),
],
};
Html
plugin and configure it to use the posthtml.config.js
file:// vite.config.js
import { defineConfig } from "vite";
import { createHtmlPlugin } from "vite-plugin-html";
export default defineConfig({
plugins: [
createHtmlPlugin({
template: "public/index.html",
filename: "index.html",
minify: true,
inject: true,
hash: true,
posthtml: {
plugins: require("./posthtml.config"),
},
}),
createHtmlPlugin({
template: "public/page1.html",
filename: "page1.html",
minify: true,
inject: true,
hash: true,
posthtml: {
plugins: require("./posthtml.config"),
},
}),
// more pages here
],
});
<!DOCTYPE html>
<html>
<head>
...
</head>
<body>
<div class="my-class">Hello World</div>
...
<script src="/assets/main.js"></script>
<% if (htmlWebpackPlugin.options.hash) { %>
<script>window.__HASH_MANIFEST__ = <%= JSON.stringify(htmlWebpackPlugin.options.hash) %>;</script>
<% } %>
</body>
</html>
This will automatically hash the classNames in your HTML and update the references in your CSS and JavaScript files. The hashed classNames will be stored in a hash-manifest.json
file in the ./dist/assets/
directory.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2022-01-14 11:00:00 +0000
Seen: 9 times
Last updated: Dec 27 '21
Is it feasible to utilize a Toggle Button to switch among multiple classes exceeding 2?
What is the process for generating a dynamic subdomain/URL using vue.js?
How can I create a transition on click using CSS without the need for JavaScript?
How can the style of the loader be modified while the form submission is being processed?
I'm attempting to develop a Javascript-based comments section for my website.
What are some feasible methods to enable MIDI file playback on a web browser?