Skip to content

Commit 61a7a62

Browse files
authored
Merge pull request #46 from nxmad/main
Skip public dir imports
2 parents 1a2b719 + df456c1 commit 61a7a62

File tree

5 files changed

+28
-1
lines changed

5 files changed

+28
-1
lines changed

cypress/integration/spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,12 @@ describe('Vite SVG Loader', () => {
4444
it('supports ?raw param', () => {
4545
cy.get('#raw').contains('<?xml version="1.0"?>')
4646
})
47+
48+
it('ignores root files references', () => {
49+
cy.get('#root img')
50+
.should('exist')
51+
.and(($img) => {
52+
expect($img[0].width).to.equal(355)
53+
})
54+
})
4755
})

examples/vue/public/root.svg

Lines changed: 4 additions & 0 deletions
Loading

examples/vue/src/App-url.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,9 @@ const Async = defineAsyncComponent(() => import(`./assets/circle.svg?component`)
3535
{{ testRaw }}
3636
</div>
3737

38+
<div id="root">
39+
<img src="/root.svg" />
40+
</div>
41+
3842
<HelloWorld msg="Hello Vue 3 + Vite" />
3943
</template>

examples/vue/src/App.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,9 @@ const Async = defineAsyncComponent(() => import(`./assets/${name}.svg`))
3131
{{ testRaw }}
3232
</div>
3333

34+
<div id="root">
35+
<img src="/root.svg" />
36+
</div>
37+
3438
<HelloWorld msg="Hello Vue 3 + Vite" />
3539
</template>

index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,21 @@ const { optimize: optimizeSvg } = require('svgo')
55
module.exports = function svgLoader (options = {}) {
66
const { svgoConfig, svgo, defaultImport } = options
77

8+
let viteConfig = {}
89
const svgRegex = /\.svg(\?(raw|component))?$/
910

1011
return {
1112
name: 'svg-loader',
1213
enforce: 'pre',
1314

15+
configResolved (config) {
16+
viteConfig = config
17+
},
18+
1419
async load (id) {
15-
if (!id.match(svgRegex)) {
20+
const isRootRef = viteConfig.command === 'build' && !id.startsWith(viteConfig.root)
21+
22+
if (!id.match(svgRegex) || isRootRef) {
1623
return
1724
}
1825

0 commit comments

Comments
 (0)