Skip to content

Commit 5fbecbc

Browse files
committed
Allow user to override next-image-loader
1 parent 1012630 commit 5fbecbc

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

packages/next/build/webpack-config.ts

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,16 @@ export default async function getBaseWebpackConfig(
10301030
]
10311031
: defaultLoaders.babel,
10321032
},
1033+
...(!config.images.disableStaticImages && isWebpack5
1034+
? [
1035+
{
1036+
test: /\.(png|jpg|jpeg|gif|webp|ico|bmp|svg)$/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-
? /\.(png|jpg|jpeg|gif|webp|ico|bmp)$/i
1477-
: /\.(png|svg|jpg|jpeg|gif|webp|ico|bmp)$/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 = /\.(png|jpg|jpeg|gif|webp|ico|bmp)$/i
1488+
}
14831489
}
14841490

14851491
if (

0 commit comments

Comments
 (0)