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

Vite still outputs SVG assets during build #8

Open
jods4 opened this issue May 11, 2021 · 7 comments
Open

Vite still outputs SVG assets during build #8

jods4 opened this issue May 11, 2021 · 7 comments

Comments

@jods4
Copy link

jods4 commented May 11, 2021

This plugins works great, but I noticed that when you build, SVG assets that were handled by vite-plugin-svg-sprite are still all copied into the output assets directory, although they're not needed.

Any idea how to avoid that?

@meowtec
Copy link
Owner

meowtec commented May 11, 2021

I will take a look

@jods4
Copy link
Author

jods4 commented May 11, 2021

FYI I couldn't find a good way to influence Vite or Rollup to modify this.
I ended up adding the following hand-made rollup plugin:

{
  name: 'svg-asset-remover',
  generateBundle(_, bundle) {
    // do not emit svg assets as they have been packed by svgPlugin
    for (const file in bundle) {
      if (bundle[file].type === 'asset' && file.endsWith('.svg'))
        delete bundle[file]
    }
  }
}

You could re-use this but instead of removing all files ending with .svg, maybe keep a map of files that have been processed by vite-plugin-svg-sprite during build?

The big defect with this approach is that it potentially could remove an SVG asset that was added another way and was not handled by vite-plugin-svg-sprite... but that's the best idea I could come up with.

@gburning
Copy link

Thank you @jods4! I'm developing an extension for Azure DevOps which doesn't allow svg files for some reason so the fact that they are still being created was a major pain in my back.

@meowtec Would it be possible to get something like what @jods4 suggested into the main plugin? I'm not super knowledgeable in rollup/vite plugins but would be willing to help out on a pull request if needed.

@jakewhiteley
Copy link

@meowtec any movement on this?

@meowtec
Copy link
Owner

meowtec commented Oct 24, 2022

Follow #8 (comment), the code should be like

const transformedFilePaths = new Set<string>();

const plugin: Plugin = {
  name: 'svg-sprite',

  async transform(src, filepath) {
    if (!micromatch.isMatch(filepath, match, {
      dot: true,
    })) {
      return undefined;
    }

    transformedFilePaths.add(filepath);
  },
  generateBundle(_, bundle) {
    for (const file in bundle) {
      // file or bundle[file] can not match transformedFilePaths, they have difference formats
      if (hasTransformed(file, bundle[file]))
        delete bundle[file]
    }
  },
}

There is no good way to detect whether a svg file (the bundle[file]) has been transformed by the plugin.

The item of transformedFilePaths is absolute file path, while bundle[file].name is relative. And can not find the base dir of them.

@AFine970
Copy link

Maybe you can try this plugin vite-plugin-svg-icons. This plugin can solve this problem @jods4

@jods4
Copy link
Author

jods4 commented Jan 12, 2023

@AFine970 thanks, I'll keep that plugin in mind.
For now everything works ok for me thanks to the clean-up plugin I wrote above.

One thing I like in vite-plugin-svg-sprite is its ability to generate components for icons based on a custom template.

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

5 participants