Frequently Asked Questions

How to use both inline and external SVGs?

    <template>
      <div>
        <InlineSVG />
        <img :src="externalSVG" />
        <div class="external-svg" />
      </div>
    </template>
    <script>
    import InlineSVG from './public/inline.svg?inline'; // Note the `?inline` query string
    import ExternalSVG from './public/external.svg';
    
    export {
      name: 'Example',
      components: {
        InlineSVG,
      },
      computed: {
        externalSVG() {
          return ExternalSVG;
        },
      },
    };
    </script>
    <style>
    .external-svg {
      background-image: url("./public/external.svg");
    }
    </style>
    

    How to prefix id attributes?

    To avoid the situation where two or more SVGs are using the same id attribute, you can use the prefixIds option provided by SVGO.

      If you want to customize generated IDs, you can pass a function instead of true to the prefixIds plugin. Here is an example for generating IDs that are prefixed by the file name:

        How to use this loader with TypeScript?

        If you want to use this loader in a project that is written in TypeScript, you will get the "Cannot find module" error. To fix that you need to provide a type definition which is needed by TypeScript to know how to handle SVG components.

        declare module '*.svg' {
          const content: any;
          export default content;
        }
        

        How to use this loader with Jest?

        There is one major issue when it comes to integrating vue-svg-loader with Jest, and it is async behaviour. Jest's transforms are synchronous, webpack loaders can be both. That means we cannot use SVGO to process the SVG files, which can be bad in some cases. It is always good idea to always pass the SVG files through SVGO before putting them in a project (for example using this great tool), so that the end result does not contain:

        • XML declaration,
        • <script> tags,
        • <style> tags.

        If your SVGs are prepared, create a transform file named for example svgTransform.js with:

        const vueJest = require('vue-jest/lib/template-compiler');
        
        module.exports = {
          process(content) {
            const { render } = vueJest({
              content,
              attrs: {
                functional: false,
              },
            });
        
            return `module.exports = { render: ${render} }`;
          },
        };
        

        And then modify your jest.config.js to use the transform file above (note that <rootDir> is injected by Jest):

        module.exports = {
          transform: {
            '^.+\\.svg$': '<rootDir>/path/to/svgTransform.js',
          },
        };