File tree Expand file tree Collapse file tree 5 files changed +28
-1
lines changed
Expand file tree Collapse file tree 5 files changed +28
-1
lines changed Original file line number Diff line number Diff 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} )
Original file line number Diff line number Diff 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 >
Original file line number Diff line number Diff 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 >
Original file line number Diff line number Diff line change @@ -5,14 +5,21 @@ const { optimize: optimizeSvg } = require('svgo')
55module . exports = function svgLoader ( options = { } ) {
66 const { svgoConfig, svgo, defaultImport } = options
77
8+ let viteConfig = { }
89 const svgRegex = / \. s v g ( \? ( r a w | c o m p o n e n t ) ) ? $ /
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
You can’t perform that action at this time.
0 commit comments