Skip to content

Commit 9df6300

Browse files
3b3zizTheAlexLichter
authored andcommitted
feat: proper loading svg used as background url (#50)
1 parent e9e2b6f commit 9df6300

File tree

6 files changed

+72
-5
lines changed

6 files changed

+72
-5
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ A live demo is available through the [CodeSandBox](https://codesandbox.io/s/gith
3737
```
3838

3939
- Now you can use your svg files like regular Vue components
40+
- You can use inline svg as well by adding `?inline` at the end of the file path
4041
```
4142
<template>
4243
<nav>
@@ -52,6 +53,14 @@ A live demo is available through the [CodeSandBox](https://codesandbox.io/s/gith
5253
<WebpackLogo />
5354
webpack
5455
</a>
56+
<!-- Inline svg -->
57+
<a class="with-background-svg" href="https://github.com/webpack/webpack">
58+
webpack
59+
</a>
60+
<a href="https://github.com/webpack/webpack">
61+
<img src="../components/NuxtTwo.svg?inline">
62+
webpack>
63+
</a>
5564
</nav>
5665
</template>
5766
<script>
@@ -68,6 +77,11 @@ export default {
6877
}
6978
};
7079
</script>
80+
<style>
81+
.with-background-svg {
82+
background: url('@/assets/svg/vue.svg?inline')
83+
}
84+
</style>
7185
```
7286

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

lib/module.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,22 @@ const setupVueSvgLoader = options => (config) => {
2828
// Add a new rule for .svg file
2929
config.module.rules.push({
3030
test: /\.svg$/,
31-
use: [
32-
'vue-loader',
31+
oneOf: [
3332
{
34-
loader: 'svg-to-vue-component/loader',
35-
options
33+
resourceQuery: /inline/,
34+
loader: 'file-loader',
35+
query: {
36+
name: '[name].[ext]'
37+
}
38+
},
39+
{
40+
use: [
41+
'vue-loader',
42+
{
43+
loader: 'svg-to-vue-component/loader',
44+
options
45+
}
46+
]
3647
}
3748
]
3849
})

test/__snapshots__/module.test.js.snap

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`ssr correctly load SVG as background image 1`] = `
4+
"<!doctype html>
5+
<html data-n-head-ssr data-n-head=\\"\\">
6+
<head data-n-head=\\"\\">
7+
<title data-n-head=\\"true\\"></title><style data-vue-ssr-id=\\"17cfdfa9:0\\">.nuxt-progress{position:fixed;top:0;left:0;right:0;height:2px;width:0;opacity:1;transition:width .1s,opacity .4s;background-color:#000;z-index:999999}.nuxt-progress.nuxt-progress-notransition{transition:none}.nuxt-progress-failed{background-color:red}</style><style data-vue-ssr-id=\\"7bcfa2f0:0\\">h1{background:url(/_nuxt/NuxtTwo.svg)}</style>
8+
</head>
9+
<body data-n-head=\\"\\">
10+
<div data-server-rendered=\\"true\\" id=\\"__nuxt\\"><!----><div id=\\"__layout\\"><div><h1>-</h1> <h2>-</h2></div></div></div><script>window.__NUXT__={layout:\\"default\\",data:[{}],error:null,serverRendered:true};</script><script src=\\"/_nuxt/runtime.js\\" defer></script><script src=\\"/_nuxt/pages/background-image.js\\" defer></script><script src=\\"/_nuxt/commons.app.js\\" defer></script><script src=\\"/_nuxt/app.js\\" defer></script>
11+
</body>
12+
</html>
13+
"
14+
`;
15+
316
exports[`ssr correctly load two lazy-loaded SVGs 1`] = `
417
"<!doctype html>
518
<html data-n-head-ssr data-n-head=\\"\\">
619
<head data-n-head=\\"\\">
720
<title data-n-head=\\"true\\"></title><style data-vue-ssr-id=\\"17cfdfa9:0\\">.nuxt-progress{position:fixed;top:0;left:0;right:0;height:2px;width:0;opacity:1;transition:width .1s,opacity .4s;background-color:#000;z-index:999999}.nuxt-progress.nuxt-progress-notransition{transition:none}.nuxt-progress-failed{background-color:red}</style>
821
</head>
922
<body data-n-head=\\"\\">
10-
<div data-server-rendered=\\"true\\" id=\\"__nuxt\\"><!----><div id=\\"__layout\\"><div><svg xmlns=\\"http://www.w3.org/2000/svg\\" viewBox=\\"1 2 3 4\\" width=\\"100\\"><g id=\\"Nuxt_svg__nuxt\\"><path d=\\"M317.9 852H3.7l408.1-704 408.1 704H507.7\\" fill=\\"#41b883\\"></path><path d=\\"M779.8 852h216.5l-354-608.5-351 608.5h216.5\\" fill=\\"#328170\\"></path><path d=\\"M651.2 852h159.5L549.9 403.8 291.3 852h159.5\\" fill=\\"#35495e\\"></path></g></svg> <svg xmlns=\\"http://www.w3.org/2000/svg\\" viewBox=\\"0 0 1000 1000\\"><g id=\\"NuxtTwo_svg__nuxt\\"><path d=\\"M317.9 852H3.7l408.1-704 408.1 704H507.7\\" fill=\\"#41b883\\"></path><path d=\\"M779.8 852h216.5l-354-608.5-351 608.5h216.5\\" fill=\\"#328170\\"></path><path d=\\"M651.2 852h159.5L549.9 403.8 291.3 852h159.5\\" fill=\\"#35495e\\"></path></g></svg></div></div></div><script>window.__NUXT__={layout:\\"default\\",data:[{}],error:null,serverRendered:true};</script><script src=\\"/_nuxt/runtime.js\\" defer></script><script src=\\"/_nuxt/pages/two.js\\" defer></script><script src=\\"/_nuxt/6.js\\" defer></script><script src=\\"/_nuxt/commons.app.js\\" defer></script><script src=\\"/_nuxt/app.js\\" defer></script>
23+
<div data-server-rendered=\\"true\\" id=\\"__nuxt\\"><!----><div id=\\"__layout\\"><div><svg xmlns=\\"http://www.w3.org/2000/svg\\" viewBox=\\"1 2 3 4\\" width=\\"100\\"><g id=\\"Nuxt_svg__nuxt\\"><path d=\\"M317.9 852H3.7l408.1-704 408.1 704H507.7\\" fill=\\"#41b883\\"></path><path d=\\"M779.8 852h216.5l-354-608.5-351 608.5h216.5\\" fill=\\"#328170\\"></path><path d=\\"M651.2 852h159.5L549.9 403.8 291.3 852h159.5\\" fill=\\"#35495e\\"></path></g></svg> <svg xmlns=\\"http://www.w3.org/2000/svg\\" viewBox=\\"0 0 1000 1000\\"><g id=\\"NuxtTwo_svg__nuxt\\"><path d=\\"M317.9 852H3.7l408.1-704 408.1 704H507.7\\" fill=\\"#41b883\\"></path><path d=\\"M779.8 852h216.5l-354-608.5-351 608.5h216.5\\" fill=\\"#328170\\"></path><path d=\\"M651.2 852h159.5L549.9 403.8 291.3 852h159.5\\" fill=\\"#35495e\\"></path></g></svg></div></div></div><script>window.__NUXT__={layout:\\"default\\",data:[{}],error:null,serverRendered:true};</script><script src=\\"/_nuxt/runtime.js\\" defer></script><script src=\\"/_nuxt/pages/two.js\\" defer></script><script src=\\"/_nuxt/7.js\\" defer></script><script src=\\"/_nuxt/commons.app.js\\" defer></script><script src=\\"/_nuxt/app.js\\" defer></script>
1124
</body>
1225
</html>
1326
"

test/fixture/modules/main.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
h2 {
2+
background: url('../components/NuxtTwo.svg?inline')
3+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<template>
2+
<div>
3+
<h1>-</h1>
4+
<h2>-</h2>
5+
</div>
6+
</template>
7+
8+
<script>
9+
import '../modules/main.css'
10+
export default {
11+
loading: false,
12+
}
13+
</script>
14+
15+
<style>
16+
h1 {
17+
background: url('../components/NuxtTwo.svg?inline')
18+
}
19+
</style>
20+

test/module.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ describe('ssr', () => {
4545
expect(html).toMatchSnapshot()
4646
})
4747

48+
test('correctly load SVG as background image', async () => {
49+
const nuxt = await setupNuxt(require('./fixture/configs/default'))
50+
const { html } = await nuxt.renderRoute('/background-image')
51+
expect(html).toMatchSnapshot()
52+
})
53+
4854
afterEach(async () => {
4955
if (nuxt) {
5056
await nuxt.close()

0 commit comments

Comments
 (0)