Skip to content
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

Closed
fatso83 opened this issue Feb 21, 2022 · 1 comment
Closed

Custom configs for different paths? #20

fatso83 opened this issue Feb 21, 2022 · 1 comment

Comments

@fatso83
Copy link

fatso83 commented Feb 21, 2022

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:

const svgrOptionsForUnicons = {
  svgProps: {
    height: 24,
    width: 24,
  },
}

module.exports.webpackRules = [
  {
    test: uniconsRegExp,
    use: [
      {
        loader: '@svgr/webpack',
        options: svgrOptionsForUnicons,
      },
    ],
  },
  {
    test: function (path) {
      return /\.svg$/.test(path) && !uniconsRegExp.test(path)
    },
    use: ['@svgr/webpack'],
  },
]

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.

@fatso83
Copy link
Author

fatso83 commented Feb 21, 2022

To scratch my itch I went with implementing a minimal plugin that just delegates to vite-plugin-svgr. If there is a better approach, it would be interesting to know.

using custom svgr options for different paths by wrapping the vite-plugin-svgr plugin
This setup uses a default config for svgr that will be used by any svg file that does not mach any of the more specific ones. You can add custom ones by implementing a matcher function that tells whether or not the custom options should be used or not for that file.

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)
      }
    },
  }
})

@fatso83 fatso83 closed this as completed Feb 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant