UNPKG

3.85 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 - [Caveats](#caveats)
18 - [Contributing](#contributing)
19 - [License](#license)
20
21## Introduction
22
23This 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:
24
25- `file.svg` - normal import using `file-loader`
26- `file.svg?data` - base64 data url import using `url-loader`
27- `file.svg?inline` - inline import using `vue-svg-loader`
28- `file.svg?raw` - raw html import using `raw-loader`
29
30## Installation
31
32```shell
33npm install --save-dev @nuxtjs/svg
34```
35
36```javascript
37// nuxt.config.js
38export default {
39 modules: ["@nuxtjs/svg"],
40};
41```
42
43And that's it! You don't have to install anything else, you're ready to go.
44
45## Usage
46
47The usage examples are documented as:
48
49```html
50<!-- Nuxt.js code -->
51```
52
53```html
54<!-- Outputted html -->
55```
56
57### `file-loader`
58
59```html
60<template>
61 <img src="~assets/nuxt.svg" />
62</template>
63```
64
65```html
66<img src="/_nuxt/9405b9978eb50f73b53ac1326b93f211.svg" />
67```
68
69### `url-loader`
70
71```html
72<template>
73 <img src="~assets/nuxt.svg?data" />
74</template>
75```
76
77```html
78<img src="data:image/svg+xml;base64,P...2h913j1g18h98hr" />
79```
80
81### `vue-svg-loader`
82
83```html
84<template>
85 <NuxtLogo />
86</template>
87
88<script>
89 import NuxtLogo from "~/assets/nuxt.svg?inline";
90
91 export default {
92 components: { NuxtLogo },
93 };
94</script>
95```
96
97```html
98<svg xmlns="http://www.w3.org/2000/svg"><path></path></svg>
99```
100
101### `raw-loader`
102
103Load the raw SVG data as HTML using `raw-loader`:
104
105```html
106<template>
107 <div v-html="rawNuxtLogo" />
108</template>
109
110<script>
111 import rawNuxtLogo from "~/assets/nuxt.svg?raw";
112
113 export default {
114 data() {
115 return { rawNuxtLogo };
116 },
117 };
118</script>
119```
120
121```html
122<div>
123 <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 400 298">
124 <g fill-rule="nonzero" fill="none"><path></path></g>
125 </svg>
126</div>
127```
128
129## Caveats
130
131In 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.
132
133The only difference between this and the handler is that there is no `limit` for when `file-loader` replaces `url-loader`.
134
135So 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`).
136
137## Contributing
138
139As 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!
140
141Also I'll be actively maintaining this project so if you'd rather just make a request for a loader or a feature; I'd be happy to take a look and see what I can do myself :)
142
143## License
144
145[MIT License](./LICENSE)
146
147Copyright (c) Sam Holmes
148
149<!-- Badges -->
150
151[npm-version-src]: https://img.shields.io/npm/v/@nuxtjs/svg/latest.svg?style=flat-square
152[npm-version-href]: https://npmjs.com/package/@nuxtjs/svg
153[npm-downloads-src]: https://img.shields.io/npm/dt/@nuxtjs/svg.svg?style=flat-square
154[npm-downloads-href]: https://npmjs.com/package/@nuxtjs/svg
155[license-src]: https://img.shields.io/npm/l/@nuxtjs/svg.svg?style=flat-square
156[license-href]: https://npmjs.com/package/@nuxtjs/svg