how to add hashed names icons and pictures from res folder into the build with webpack?…
- User
how to add hashed names icons and pictures from res folder into the build with webpack? I cant use index.js because its mot recognize the css file and its imported there.
index.html:
webpack:
const path = require('path'); const fs = require('fs'); const MiniCssExtractPlugin = require('mini-css-extract-plugin'); const HtmlWebpackPlugin = require('html-webpack-plugin'); const {CleanWebpackPlugin} = require('clean-webpack-plugin'); const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const DOMAIN = 'genie.li'; const LOCALE_DEFAULT = 'he'; const LOCALES_RTL = ['he', 'ar'];
const localeFiles = fs.readdirSync(path.resolve(__dirname, 'locales')).filter(f => f.endsWith('.json')); const locales = localeFiles.map(f => path.basename(f, '.json')); const localeLinks =
<link rel="alternate" hreflang="x-default" href="https://${DOMAIN}/${LOCALE_DEFAULT}/"/>+ locales.map(locale =><link rel="alternate" hreflang="${locale}" href="https://${DOMAIN}/${locale}/"/>).join('\n'); const localeOptions = locales.map(locale =><li data-value="${locale}">${locale}</li>).join('\n');module.exports = { entry: "./src/index.js", output: { filename: '[name].[contenthash].js', path: path.resolve(__dirname, 'out'), clean: true, }, module: { rules: [ { test: /.js/, exclude: /node_modules/, use: 'babel-loader' }, { test: /\.css/, use: [ 'style-loader', 'css-loader' ] }, { test: /.(png|jpe?g|svg|gif|webp|woff2?|ttf|eot|ico)/, include: path.resolve(__dirname, 'res'), exclude: /\.DS_Store/, type: 'asset/resource', generator: { filename: '[name].[contenthash][ext]' } } ] }, plugins: [ new CleanWebpackPlugin(), new MiniCssExtractPlugin({ filename: '[name].[contenthash].css' }), ...localeFiles.map((file,i) => { const locale = locales[i]; const translations = JSON.parse(fs.readFileSync(path.resolve(__dirname, 'locales', file), 'utf8')); return new HtmlWebpackPlugin({ filename: locale === LOCALE_DEFAULT ? 'index.html' :
index.${locale}.html, template: './src/index.html', inject: 'body', minify: { collapseWhitespace: true, removeComments: true, removeAttributeQuotes: true, minifyJS: true, minifyCSS: true, }, templateParameters: { LANG: locale, DIR: LOCALES_RTL.includes(locale) ? 'rtl' : 'ltr', URL_LINKS: localeLinks, LANG_OPTIONS: localeOptions, ...translations } }); }) ], optimization: { minimize: true, minimizer: [ '...', new CssMinimizerPlugin(), ], } }; - Assistant
To add hashed names for icons and pictures from your
resfolder into the build with Webpack without importing them inindex.js(since you said you can't useindex.jsfor CSS imports), you need a way to:- Copy and hash the assets from
resfolder. - Reference those hashed assets in your HTML or CSS.
- Make sure CSS files are processed and linked correctly.
Key points and solution outline:
-
Asset handling with Webpack:
Your current config already usesasset/resourcefor images/fonts inresfolder, which hashes and copies them toout/. -
CSS handling:
You usestyle-loaderandcss-loaderbut alsoMiniCssExtractPlugin.style-loaderinjects CSS into JS (not suitable if you want separate CSS files).MiniCssExtractPluginextracts CSS into separate files, but you need to replacestyle-loaderwithMiniCssExtractPlugin.loaderin your CSS rule.
-
Referencing assets in CSS:
If your CSS references images/icons viaurl(...), Webpack will resolve and hash them if CSS is processed bycss-loader. -
Referencing assets in HTML:
Since you useHtmlWebpackPluginwith a template, you can pass the hashed asset paths as template parameters or use Webpack'srequireorimportin your JS or a separate asset manifest.
Step-by-step fix:
1. Fix CSS loader to extract CSS files properly
Change your CSS rule from:
{ test: /\.css$/, use: [ 'style-loader', 'css-loader' ] }to:
{ test: /\.css$/, use: [ MiniCssExtractPlugin.loader, 'css-loader' ] }This will extract CSS into separate files instead of injecting into JS.
2. Import CSS in your entry or create a separate CSS entry
If you can't import CSS in
index.js(because it doesn't recognize CSS), create a separate entry for CSS:entry: { main: './src/index.js', styles: './src/styles.css' // your main CSS file },Webpack will build
styles.[contenthash].cssandmain.[contenthash].js.Then in your
HtmlWebpackPlugintemplate, inject the CSS file:new HtmlWebpackPlugin({ // ... chunks: ['main', 'styles'], // include both JS and CSS chunks // ... })
3. Reference images/icons in CSS or JS
- In your CSS, reference images like:
.icon { background-image: url('../res/icon.png'); }Webpack will process this and output hashed files.
-
If you need to reference images in HTML, you can:
- Import images in your JS and pass their URLs to the template via
HtmlWebpackPluginparameters. - Or use a plugin like
copy-webpack-pluginto copy assets with hashes and generate a manifest.
- Import images in your JS and pass their URLs to the template via
Example in JS:
import iconUrl from '../res/icon.png'; // Pass iconUrl to HtmlWebpackPlugin via DefinePlugin or other means
4. Alternative: Use
copy-webpack-pluginwith hashingIf you want to copy all files from
resfolder with hashed names without importing them:const CopyWebpackPlugin = require('copy-webpack-plugin'); plugins: [ // other plugins... new CopyWebpackPlugin({ patterns: [ { from: 'res', to: 'res/[name].[contenthash][ext]', noErrorOnMissing: true, } ] }), ]This copies and hashes files, but you need a way to get the hashed filenames to reference in your HTML or CSS.
Summary
- Use
MiniCssExtractPlugin.loaderinstead ofstyle-loaderto extract CSS files. - Import CSS in a separate entry if you can't import in
index.js. - Reference images/icons in CSS with relative paths so Webpack hashes them.
- Use
HtmlWebpackPluginto inject CSS and JS chunks. - Optionally use
copy-webpack-pluginto copy and hash assets without importing.
Example minimal CSS rule fix:
{ test: /\.css$/, use: [ MiniCssExtractPlugin.loader, 'css-loader' ] }Example entry:
entry: { main: './src/index.js', styles: './src/styles.css' },Example HtmlWebpackPlugin usage:
new HtmlWebpackPlugin({ filename: 'index.html', template: './src/index.html', chunks: ['main', 'styles'], // other options... })
If you want me to help you generate a full working example or handle specific asset references in your HTML template, just ask!
- Copy and hash the assets from
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.