Skip to content

Instantly share code, notes, and snippets.

@mkpoli
Created March 11, 2021 17:19
Show Gist options
  • Select an option

  • Save mkpoli/8ae86a7a16191bde608cc27ebb33f2ee to your computer and use it in GitHub Desktop.

Select an option

Save mkpoli/8ae86a7a16191bde608cc27ebb33f2ee to your computer and use it in GitHub Desktop.
Replace <i> tag for <svg> with icon name replaced for @mdi/js path string in Webpack
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const HtmlReplaceWebpackPlugin = require('html-replace-webpack-plugin');
module.exports = {
entry: './src/index.js',
plugins: [
new HtmlWebpackPlugin({
title: "Webpack",
}),
new HtmlReplaceWebpackPlugin([
{
pattern: /<i>(.+?)<\/i>/g,
replacement: function(match, icon) {
const mdiJS = require('@mdi/js')
const _ = require('lodash');
return `<svg style="width:24px;height:24px" viewBox="0 0 24 24"><path fill="currentColor" d="${mdiJS[_.camelCase(icon)]}"></path></svg>`
}
}
])
],
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
clean: true,
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment