Reputation: 13
My index.html uses a svg in this way:
<button class="button--settings" id="button_settings">
<svg role="img">
<use href="assets/images/icons.svg#settings-icon"></use>
</svg>
</button>
I'm using Webpack to bundle my project. I need to have the svg inline in the compiled index.html.
So <use href="assets/images/icons.svg#settings-icon"></use>
should be replaced with the actual svg data. How do I achieve this?
This is my current webpack.config setup:
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
module.exports = {
entry: './src/scripts/index.ts', // Entry point of your application
devtool: 'inline-source-map',
output: {
filename: 'scripts/[name].bundle.js', // Output bundle file name
path: path.resolve(__dirname, 'dist'), // Output directory
clean: true,
},
module: {
rules: [
{
test: /\.ts$/,
use: 'ts-loader',
exclude: /node_modules/,
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader'],
},
{
test: /\.svg$/,
type: 'asset/resource',
generator: {
filename: 'assets/images/[name][ext]',
},
},
{
test: /\.(woff|woff2|eot|ttf|otf)$/,
type: 'asset/resource',
generator: {
filename: 'assets/fonts/[name][ext]',
},
},
{
test: /\.html$/,
use: 'html-loader',
},
],
},
resolve: {
extensions: ['.ts', '.js'],
},
optimization: {
minimize: true,
minimizer: [
new TerserPlugin({
terserOptions: {
format: {
comments: false, // Remove comments
},
},
extractComments: false, // Disable extracting comments into a separate file
}),
],
},
plugins: [
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
template: './public/index.html', // Template HTML file
filename: 'index.html', // Output HTML file name
}),
],
};
I'm already researching for 4 hours now but I can't make it work.
Upvotes: 0
Views: 32