-
Notifications
You must be signed in to change notification settings - Fork 55
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
Custom configs for different paths? #20
Comments
To scratch my itch I went with implementing a minimal plugin that just delegates to using custom svgr options for different paths by wrapping the vite-plugin-svgr plugin import customSvgrPluginSupportingOverrides from '../common/custom-svgr-plugin'
const uniconsRegExp = new RegExp('@iconscout/unicons/svg/line/.*svg$')
const svgrOptionsForUnicons = {
namedExport: 'ReactComponent',
svgProps: {
height: 24,
width: 24,
className: 'iconscout_unicons',
},
}
export default defineConfig({
plugins: [
reactRefresh(),
customSvgrPluginSupportingOverrides({
defaultConfig: {
svgrOptions: {
// ...svgr options (https://react-svgr.com/docs/options/)
},
},
customConfigs: [
{
matcher(id) {
return uniconsRegExp.test(id)
},
svgrOptions: svgrOptionsForUnicons,
},
// add additional overrides for specific packages or file patterns here
],
}),
],
} custom-svgr-plugin.js const svgrPlugin = require('vite-plugin-svgr')
const debug = require('debug')('myapp-custom-svgr-plugin')
// wrap the default svgr loader to enable processing different paths differently possible
module.exports = function ({ defaultConfig, customConfigs = [] }) {
const defaultPlugin = svgrPlugin(defaultConfig)
const customConfiguredPluginsWithMatchers = customConfigs.map((config) => ({
matcher: config.matcher,
plugin: svgrPlugin(config),
}))
return {
name: 'vite:custom-svgr',
async transform(code, id) {
if (id.endsWith('.svg')) {
const plugin = customConfiguredPluginsWithMatchers.find(({ matcher, plugin }) => matcher(id)).plugin || defaultPlugin
if (plugin === defaultPlugin) {
debug(`No custom plugin matching ${id} found. Falling back to default config`)
} else {
debug(`Found custom plugin setup for ${id}!`)
}
return plugin.transform(code, id)
}
},
}
}) |
Is there a way I can use this plugin so that it treats different paths differently? I would like to treat an icon package with some custom options and one way I would do that in webpack is like this, using two matchers:
This allows me to have most icons with a set height and width, while leaving all other svgs untouched.
I see the current approach is kind of crude, but maybe one could wrap the loader somehow? Everything is possible, of course, but I would like to stick to the most conventional way, if possible.
The text was updated successfully, but these errors were encountered: