diff --git a/cypress/integration/spec.js b/cypress/integration/spec.js index 82a6e50..c6feff2 100644 --- a/cypress/integration/spec.js +++ b/cypress/integration/spec.js @@ -44,4 +44,12 @@ describe('Vite SVG Loader', () => { it('supports ?raw param', () => { cy.get('#raw').contains('') }) + + it('ignores root files references', () => { + cy.get('#root img') + .should('exist') + .and(($img) => { + expect($img[0].width).to.equal(355) + }) + }) }) diff --git a/examples/vue/public/root.svg b/examples/vue/public/root.svg new file mode 100644 index 0000000..52075fa --- /dev/null +++ b/examples/vue/public/root.svg @@ -0,0 +1,4 @@ + + + + diff --git a/examples/vue/src/App-url.vue b/examples/vue/src/App-url.vue index c619a16..c036c83 100644 --- a/examples/vue/src/App-url.vue +++ b/examples/vue/src/App-url.vue @@ -35,5 +35,9 @@ const Async = defineAsyncComponent(() => import(`./assets/circle.svg?component`) {{ testRaw }} +
+ +
+ diff --git a/examples/vue/src/App.vue b/examples/vue/src/App.vue index 761bfdf..612e7c3 100644 --- a/examples/vue/src/App.vue +++ b/examples/vue/src/App.vue @@ -31,5 +31,9 @@ const Async = defineAsyncComponent(() => import(`./assets/${name}.svg`)) {{ testRaw }} +
+ +
+ diff --git a/index.js b/index.js index a50c007..c7caae0 100644 --- a/index.js +++ b/index.js @@ -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 }