Developmint / nuxt-svg-loader

SVGs as components, also on the server side!
MIT License
144 stars 14 forks source link
nuxt-module nuxt-svg-loader nuxtjs svg vue

Nuxt SVG Loader - SVGs as components, also on the server side!

npm (scoped with tag) npm Build Status codecov Dependencies js-standard-style thanks

📖 Release Notes

Features

Demo

A live demo is available through the CodeSandBox of the repo.

Setup

{
  modules: [
    'nuxt-svg-loader',
  ]
}

export default { name: 'Example', components: { VueLogo, SVGOLogo, WebpackLogo, } };


- No more options are needed. It'll simply work

## Configuration

The plugin will work seamlessly out of the box.
It will also include SVGO defaults to avoid collisions between your optimized SVG files!

If you want to configure the underlying loader (or SVGO), you can do that easily as well.
(All options available [here](https://github.com/egoist/svg-to-vue-component#loader-options))

```js
// file: nuxt.config.js

export default {
  // ...
  // Your loader options as svgLoader object
  svgLoader: {
    svgoConfig: {
      plugins: [
        { prefixIds: false } // Disables prefixing for SVG IDs
      ]
    }
  }
}

Migrating from 0.x

  1. Update the deps (of course!)
  2. Rename svgo to svgoConfig
  3. If you used id prefixing manually before, you can delete the config:
export default {
  svgLoader: {
    svgo: { //Rename to svgoConfig  
      plugins: [
        { prefixIds: true } // Delete that line (or the whole svgLoader object if you don't have any other configurations)
      ]
    }
  }
}

How to fix Eslint auto lint error

If you turn on Eslint on save by server, you should exclude .svg files of eslint-loader.

Example:

// nuxt.config.js
build: {
    extend(config, ctx) {
    // Run ESLint on save
    if (ctx.isDev && ctx.isClient) {
      config.module.rules.push({
        enforce: 'pre',
        test: /\.(js|vue)$/,
        loader: 'eslint-loader',
        exclude: /(node_modules)|(\.svg$)/ /* <--- here */
      })
    }
  }
}

Development

License

MIT License

Copyright (c) Alexander Lichter