@@ -1030,6 +1030,16 @@ export default async function getBaseWebpackConfig(
10301030 ]
10311031 : defaultLoaders . babel ,
10321032 } ,
1033+ ...( ! config . images . disableStaticImages && isWebpack5
1034+ ? [
1035+ {
1036+ test : / \. ( p n g | j p g | j p e g | g i f | w e b p | i c o | b m p | s v g ) $ / i,
1037+ loader : 'next-image-loader' ,
1038+ issuer : { not : regexLikeCss } ,
1039+ dependency : { not : [ 'url' ] } ,
1040+ } ,
1041+ ]
1042+ : [ ] ) ,
10331043 ] . filter ( Boolean ) ,
10341044 } ,
10351045 plugins : [
@@ -1459,27 +1469,23 @@ export default async function getBaseWebpackConfig(
14591469 }
14601470
14611471 if ( ! config . images . disableStaticImages && isWebpack5 ) {
1462- if ( ! webpackConfig . module ) {
1463- webpackConfig . module = { rules : [ ] }
1464- }
1465-
1466- const hasSvg = webpackConfig . module . rules . some (
1472+ const rules = webpackConfig . module ?. rules || [ ]
1473+ const hasCustomSvg = rules . some (
14671474 ( rule ) =>
1468- 'test' in rule && rule . test instanceof RegExp && rule . test . test ( '.svg' )
1475+ rule . loader !== 'next-image-loader' &&
1476+ 'test' in rule &&
1477+ rule . test instanceof RegExp &&
1478+ rule . test . test ( '.svg' )
14691479 )
1470-
1471- // Exclude svg if the user already defined it in custom
1472- // webpack config such as `@svgr/webpack` plugin or
1473- // the `babel-plugin-inline-react-svg` plugin.
1474- webpackConfig . module . rules . push ( {
1475- test : hasSvg
1476- ? / \. ( p n g | j p g | j p e g | g i f | w e b p | i c o | b m p ) $ / i
1477- : / \. ( p n g | s v g | j p g | j p e g | g i f | w e b p | i c o | b m p ) $ / i,
1478- loader : 'next-image-loader' ,
1479- dependency : { not : [ 'url' ] } ,
1480- // @ts -ignore
1481- issuer : { not : regexLikeCss } ,
1482- } )
1480+ const nextImageRule = rules . find (
1481+ ( rule ) => rule . loader === 'next-image-loader'
1482+ )
1483+ if ( hasCustomSvg && nextImageRule ) {
1484+ // Exclude svg if the user already defined it in custom
1485+ // webpack config such as `@svgr/webpack` plugin or
1486+ // the `babel-plugin-inline-react-svg` plugin.
1487+ nextImageRule . test = / \. ( p n g | j p g | j p e g | g i f | w e b p | i c o | b m p ) $ / i
1488+ }
14831489 }
14841490
14851491 if (
0 commit comments