4.2.1 • Published 14 days ago

@poppanator/sveltekit-svg v4.2.1

Weekly downloads
-
License
MIT
Repository
github
Last release
14 days ago

SvelteKit SVG Plugin

This plugin makes it possible to import SVG files as Svelte components, inline SVG code or urls.

Install

  • yarn add --dev @poppanator/sveltekit-svg
  • npm install -D @poppanator/sveltekit-svg

Usage

In your svelte.config.js

import svg from '@poppanator/sveltekit-svg'

/** @type {import('@sveltejs/kit').Config} */
const config = {
  ...,

  kit: {
    ...,
    vite: {
      // Options are optional
      plugins: [svg(options)]
    }
  }
}

export default config

You can also pass multiple svg transformers based on paths if you want to apply different SVGO options for different SVGs

/** @type {import('@sveltejs/kit').Config} */
const config = {
  ...,

  kit: {
    ...,
    vite: {
      plugins: [
        svg({
          includePaths: ["./src/lib/icons/**/*.svg"],
          svgoOptions: {
            multipass: true,
            plugins: ["preset-default", { name: "removeAttrs", params: { attrs: "(fill|stroke)" }}],
          },
        }),
        svg({
          includePaths: ["./src/lib/graphics/**"],
          svgoOptions: {
            multipass: true,
            plugins: ["preset-default" ],
          },
        }),
      ]
    }
  }
}

Svelte usage

Import as a Svelte component:

<script>
import Logo from "./logo.svg";
</script>

<Logo />

When used as a component you can also pass attributes to the SVG

<Logo width="200" />

Import as file path:

<script>
import logoUrl from "./logo.svg?url";
</script>

<img src={logoUrl} />

Import as code:

<script>
import logo from "./logo.svg?src";
</script>

{@html logo}

Options

interface Options {
  /**
   * Output type
   * @default "component"
   */
  type?: 'src' | 'url' | 'component'
  /**
   * Verbatim [SVGO](https://github.com/svg/svgo) options
   */
  svgoOptions?: svgo.OptimizeOptions
  /**
   * Paths to apply the SVG plugin on. This can be useful if you want to apply
   * different SVGO options/plugins on different SVGs.
   *
   * The paths are [minimatch](https://github.com/isaacs/minimatch) globs and
   * should be relative to your `svelte.config.js` file.
   *
   * @example
   * ```
   * {
   *   includePaths: ['src/assets/icons/*.svg']
   * }
   * ```
   */
  includePaths?: string[]
}

Typescript

For Typescript not to complain about file.svg?component et.al, add the following to your application's global.d.ts. (global.d.ts is just an arbitrary file. It can be named whatever and reside wherever as long as your Typescript config recognize it)

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

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

declare module '*.svg?src' {
  const content: string
  export default content
}

declare module '*.svg?url' {
  const content: string
  export default content
}
5.0.0-svelte5.3

14 days ago

5.0.0-svelte5.2

4 months ago

4.2.1

4 months ago

5.0.0-svelte5.1

4 months ago

5.0.0-svelte5.0

4 months ago

4.2.0

4 months ago

4.1.3

9 months ago

4.1.0

10 months ago

4.0.1

10 months ago

4.1.2

9 months ago

4.1.1

9 months ago

3.0.1

1 year ago

4.0.0

11 months ago

3.0.0

1 year ago

2.1.2

1 year ago

2.1.1

1 year ago

2.0.2

1 year ago

2.0.0

1 year ago

1.1.1

1 year ago

1.1.0

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.3.4

2 years ago

0.3.2

2 years ago

0.2.4-1

2 years ago

0.2.4-0

2 years ago

0.2.3

2 years ago

0.3.1

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.8

2 years ago

0.2.2

2 years ago

0.1.7

3 years ago

0.1.6

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago