porsager / rollup-plugin-svgo

✂️ A rollup plugin to import svg files processed with svgo
Do What The F*ck You Want To Public License
22 stars 7 forks source link

Send filename in second param of svgo.optimize for plugin use #3

Closed mgdodge closed 4 years ago

mgdodge commented 5 years ago

svgo.optimize accepts a second parameter, which is passed to plugins. rollup-plugin-svgo currently doesn't pass this parameter, which breaks svgo opts that are expecting it.

For example, consider this usage of the prefixIds plugin:

const prefixes = {};

module.exports = {
  plugins: [
    ...
    {
      prefixIds: {
        prefix(_el, filepath) {
          const { path } = filepath;
          if (!prefixes[path]) {
            prefixes[path] = Object.keys(prefixes).length;
          }
          return `svg-${prefixes[path]}`;
        },
      },
    },
    ...
  ],
};

This configuration prefixes IDs to avoid collisions when multiple SVG files are processed to be displayed at the same time. The configuration provided works with the webpack svgo loader because they pass the filename. It breaks with rollup-plugin-svgo.

This PR brings rollup-plugin-svgo into parity with the webpack loader and prevents breakage when plugins/configurations expect this information to be present.

mgdodge commented 4 years ago

Bump

porsager commented 4 years ago

I'm so sorry for not responding sooner. This is great! Thank you!

porsager commented 4 years ago

Also published to npm as v1.1.0 now.