UNPKG

4.76 kBMarkdownView Raw
1# @nuxtjs/svg
2
3[![npm version][npm-version-src]][npm-version-href]
4[![npm downloads][npm-downloads-src]][npm-downloads-href]
5[![License][license-src]][license-href]
6
7_Super simple svg loading module for Nuxt.js_
8
9- [@nuxtjs/svg](#nuxtjssvg)
10 - [Introduction](#introduction)
11 - [Installation](#installation)
12 - [Usage](#usage)
13 - [`file-loader`](#file-loader)
14 - [`url-loader`](#url-loader)
15 - [`vue-svg-loader`](#vue-svg-loader)
16 - [`raw-loader`](#raw-loader)
17 - [`svg-sprite-loader`](#svg-sprite-loader)
18 - [Caveats](#caveats)
19 - [Contributing](#contributing)
20 - [License](#license)
21
22## Introduction
23
24This package is for loading SVG's into Nuxt.js pages. It allows you to import `.svg` files in multiple ways depending on the [resource query](https://webpack.js.org/configuration/module/#rule-resourcequery) you provide. Currently, this allows you to do the following:
25
26- `file.svg` - normal import using `file-loader`
27- `file.svg?data` - base64 data url import using `url-loader`
28- `file.svg?inline` - inline import using `vue-svg-loader`
29- `file.svg?raw` - raw html import using `raw-loader`
30- `file.svg?sprite` - SVG sprite using `svg-sprite-loader`
31
32## Installation
33
34```shell
35npm install --save-dev @nuxtjs/svg
36```
37
38```javascript
39// nuxt.config.js
40export default {
41 buildModules: ["@nuxtjs/svg"],
42};
43```
44
45And that's it! You don't have to install anything else, you're ready to go.
46
47## Configuration
48
49```javascript
50// nuxt.config.js
51export default {
52 svg: {
53 vueSvgLoader: {
54 // vue-svg-loader options
55 },
56 svgSpriteLoader: {
57 // svg-sprite-loader options
58 }
59 }
60};
61```
62
63
64## Usage
65
66The usage examples are documented as:
67
68```html
69<!-- Nuxt.js code -->
70```
71
72```html
73<!-- Outputted html -->
74```
75
76### `file-loader`
77
78```html
79<template>
80 <img src="~assets/nuxt.svg" />
81</template>
82```
83
84```html
85<img src="/_nuxt/9405b9978eb50f73b53ac1326b93f211.svg" />
86```
87
88### `url-loader`
89
90```html
91<template>
92 <img src="~assets/nuxt.svg?data" />
93</template>
94```
95
96```html
97<img src="data:image/svg+xml;base64,P...2h913j1g18h98hr" />
98```
99
100### `vue-svg-loader`
101
102```html
103<template>
104 <NuxtLogo />
105</template>
106
107<script>
108 import NuxtLogo from "~/assets/nuxt.svg?inline";
109
110 export default {
111 components: { NuxtLogo },
112 };
113</script>
114```
115
116```html
117<svg xmlns="http://www.w3.org/2000/svg"><path></path></svg>
118```
119
120### `raw-loader`
121
122Load the raw SVG data as HTML using `raw-loader`:
123
124```html
125<template>
126 <div v-html="rawNuxtLogo" />
127</template>
128
129<script>
130 import rawNuxtLogo from "~/assets/nuxt.svg?raw";
131
132 export default {
133 data() {
134 return { rawNuxtLogo };
135 },
136 };
137</script>
138```
139
140```html
141<div>
142 <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 400 298">
143 <g fill-rule="nonzero" fill="none"><path></path></g>
144 </svg>
145</div>
146```
147
148### `svg-sprite-loader`
149
150```html
151<template>
152 <svg :viewBox="spriteNuxtLogo.viewBox">
153 <use :xlink:href="'#' + spriteNuxtLogo.id"></use>
154 </svg>
155</template>
156
157<script>
158 import spriteNuxtLogo from "~/assets/nuxt.svg?sprite";
159
160 export default {
161 data() {
162 return { spriteNuxtLogo };
163 },
164 };
165</script>
166```
167
168```html
169<svg viewBox="0 0 400 298">
170 <use xlink:href="#nuxt--sprite"></use>
171</svg>
172```
173
174## Dynamic imports
175
176To dynamically import an SVG, you can use the inline `require()` syntax.
177
178```html
179<template>
180 <div v-html="require(`../assets/${name}.svg?raw`)" />
181</template>
182
183<script>
184 export default {
185 props: {
186 name: { type: String, default: "image" },
187 },
188 };
189</script>
190```
191
192> This example uses `raw-loader`.
193
194## Caveats
195
196In order for this module to work correctly, the [default `.svg` Nuxt.js webpack rule](https://nuxtjs.org/guide/assets/#webpack) gets replaced with this handler.
197
198The only difference between this and the handler is that there is no `limit` for when `file-loader` replaces `url-loader`.
199
200So when using the `?data` query, it will _always_ use `url-loader` regardless of file size, and when not using either resource query, it will always use `file-loader`).
201
202## Contributing
203
204As this loader attempts to abstract webpack configuration from the process and make it easier to use multiple svg loaders, any contributions that add more svg loader methods to the configuration will be accepted wholeheartedly!
205
206## License
207
208[MIT License](./LICENSE)
209
210Copyright (c) Sam Holmes
211
212<!-- Badges -->
213
214[npm-version-src]: https://img.shields.io/npm/v/@nuxtjs/svg/latest.svg?style=flat-square
215[npm-version-href]: https://npmjs.com/package/@nuxtjs/svg
216[npm-downloads-src]: https://img.shields.io/npm/dt/@nuxtjs/svg.svg?style=flat-square
217[npm-downloads-href]: https://npmjs.com/package/@nuxtjs/svg
218[license-src]: https://img.shields.io/npm/l/@nuxtjs/svg.svg?style=flat-square
219[license-href]: https://npmjs.com/package/@nuxtjs/svg