Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip public dir imports #46

Merged
merged 1 commit into from Apr 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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