Skip to content

Commit

Permalink
Merge pull request #46 from nxmad/main
Browse files Browse the repository at this point in the history
Skip public dir imports
  • Loading branch information
jpkleemans committed Apr 24, 2022
2 parents 1a2b719 + df456c1 commit 61a7a62
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
8 changes: 8 additions & 0 deletions cypress/integration/spec.js
Expand Up @@ -44,4 +44,12 @@ describe('Vite SVG Loader', () => {
it('supports ?raw param', () => {
cy.get('#raw').contains('<?xml version="1.0"?>')
})

it('ignores root files references', () => {
cy.get('#root img')
.should('exist')
.and(($img) => {
expect($img[0].width).to.equal(355)
})
})
})
4 changes: 4 additions & 0 deletions examples/vue/public/root.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions examples/vue/src/App-url.vue
Expand Up @@ -35,5 +35,9 @@ const Async = defineAsyncComponent(() => import(`./assets/circle.svg?component`)
{{ testRaw }}
</div>

<div id="root">
<img src="/root.svg" />
</div>

<HelloWorld msg="Hello Vue 3 + Vite" />
</template>
4 changes: 4 additions & 0 deletions examples/vue/src/App.vue
Expand Up @@ -31,5 +31,9 @@ const Async = defineAsyncComponent(() => import(`./assets/${name}.svg`))
{{ testRaw }}
</div>

<div id="root">
<img src="/root.svg" />
</div>

<HelloWorld msg="Hello Vue 3 + Vite" />
</template>
9 changes: 8 additions & 1 deletion index.js
Expand Up @@ -5,14 +5,21 @@ const { optimize: optimizeSvg } = require('svgo')
module.exports = function svgLoader (options = {}) {
const { svgoConfig, svgo, defaultImport } = options

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

return {
name: 'svg-loader',
enforce: 'pre',

configResolved (config) {
viteConfig = config
},

async load (id) {
if (!id.match(svgRegex)) {
const isRootRef = viteConfig.command === 'build' && !id.startsWith(viteConfig.root)

if (!id.match(svgRegex) || isRootRef) {
return
}

Expand Down

0 comments on commit 61a7a62

Please sign in to comment.