Unverified Commit c35b01a6 authored by Steven's avatar Steven Committed by GitHub

Omit svg static imports if custom webpack config is defined (#26281)

This PR does a couple things:

1. Omit svg static imports if the user has defined custom webpack config with svg rule
2. Change TS type to `any` for svg imports to avoid conflicts with other plugins

The idea is that some users want to use `next/image` with static imports for most image types but not for svg and instead inline those images with a plugin.

- Fixes #25950  
- Fixes #26130 
- Fixes #26176 
- Fixes #26196 
- Fixes #26067 


## Bug

- [x] Related issues linked using Fixes #26130 
- [x] Integration tests added
parent 14f01a1a
...@@ -43,6 +43,7 @@ ...@@ -43,6 +43,7 @@
"@babel/preset-react": "7.12.10", "@babel/preset-react": "7.12.10",
"@fullhuman/postcss-purgecss": "1.3.0", "@fullhuman/postcss-purgecss": "1.3.0",
"@mdx-js/loader": "0.18.0", "@mdx-js/loader": "0.18.0",
"@svgr/webpack": "5.5.0",
"@testing-library/react": "11.2.5", "@testing-library/react": "11.2.5",
"@types/cheerio": "0.22.16", "@types/cheerio": "0.22.16",
"@types/fs-extra": "8.1.0", "@types/fs-extra": "8.1.0",
......
...@@ -1030,16 +1030,6 @@ export default async function getBaseWebpackConfig( ...@@ -1030,16 +1030,6 @@ export default async function getBaseWebpackConfig(
] ]
: defaultLoaders.babel, : defaultLoaders.babel,
}, },
...(!config.images.disableStaticImages && isWebpack5
? [
{
test: /\.(png|svg|jpg|jpeg|gif|webp|ico|bmp)$/i,
loader: 'next-image-loader',
issuer: { not: regexLikeCss },
dependency: { not: ['url'] },
},
]
: []),
].filter(Boolean), ].filter(Boolean),
}, },
plugins: [ plugins: [
...@@ -1468,6 +1458,30 @@ export default async function getBaseWebpackConfig( ...@@ -1468,6 +1458,30 @@ export default async function getBaseWebpackConfig(
} }
} }
if (!config.images.disableStaticImages && isWebpack5) {
if (!webpackConfig.module) {
webpackConfig.module = { rules: [] }
}
const hasSvg = webpackConfig.module.rules.some(
(rule) =>
'test' in rule && rule.test instanceof RegExp && rule.test.test('.svg')
)
// Exclude svg if the user already defined it in custom
// webpack config such as `@svgr/webpack` plugin or
// the `babel-plugin-inline-react-svg` plugin.
webpackConfig.module.rules.push({
test: hasSvg
? /\.(png|jpg|jpeg|gif|webp|ico|bmp)$/i
: /\.(png|svg|jpg|jpeg|gif|webp|ico|bmp)$/i,
loader: 'next-image-loader',
dependency: { not: ['url'] },
// @ts-ignore
issuer: { not: regexLikeCss },
})
}
if ( if (
config.experimental.craCompat && config.experimental.craCompat &&
webpackConfig.module?.rules && webpackConfig.module?.rules &&
......
This diff is collapsed.
...@@ -40,7 +40,12 @@ declare module '*.png' { ...@@ -40,7 +40,12 @@ declare module '*.png' {
} }
declare module '*.svg' { declare module '*.svg' {
const content: StaticImageData /**
* Use `any` to avoid conflicts with
* `@svgr/webpack` plugin or
* `babel-plugin-inline-react-svg` plugin.
*/
const content: any
export default content export default content
} }
......
module.exports = {
webpack(config, options) {
config.module.rules.push({
test: /\.svg$/,
use: ['@svgr/webpack'],
})
return config
},
}
import Icon from '../public/test.svg'
export default function Home() {
return <Icon />
}
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg version="1.0" xmlns="http://www.w3.org/2000/svg" width="400" height="400" viewBox="0 0 400 400" preserveAspectRatio="xMidYMid meet">
<g transform="translate(0.000000,400.000000) scale(0.100000,-0.100000)" fill="#000000" stroke="none">
<path d="M0 2000 l0 -2000 2000 0 2000 0 0 2000 0 2000 -2000 0 -2000 0 0
-2000z m2401 118 l396 -693 -398 -3 c-220 -1 -578 -1 -798 0 l-398 3 396 693
c217 380 398 692 401 692 3 0 184 -312 401 -692z"/>
</g>
</svg>
/* eslint-env jest */
import { join } from 'path'
import {
renderViaHTTP,
findPort,
launchApp,
nextBuild,
killApp,
} from 'next-test-utils'
jest.setTimeout(1000 * 60 * 2)
const appDir = join(__dirname, '..')
let appPort
let app
let devOutput
describe('svgo-webpack with Image Component', () => {
describe('next build', () => {
it('should not fail to build invalid usage of the Image component', async () => {
const { stderr, code } = await nextBuild(appDir, [], { stderr: true })
expect(stderr).toBeFalsy()
expect(code).toBe(0)
})
})
describe('next dev', () => {
beforeAll(async () => {
devOutput = { stdout: '', stderr: '' }
appPort = await findPort()
app = await launchApp(appDir, appPort, {
onStdout: (msg) => {
devOutput.stdout += msg
},
onStderr: (msg) => {
devOutput.stderr += msg
},
})
})
afterAll(() => killApp(app))
it('should print error when invalid Image usage', async () => {
await renderViaHTTP(appPort, '/', {})
expect(devOutput.stderr).toBeFalsy()
})
})
})
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve"
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}
import React from 'react' import React from 'react'
import Image from 'next/image' import Image from 'next/image'
import testTall from '../public/tall.png' import testTall from '../public/tall.png'
import svg from '../public/test.svg'
const Page = () => { const Page = () => {
return ( return (
...@@ -73,6 +74,7 @@ const Page = () => { ...@@ -73,6 +74,7 @@ const Page = () => {
src={testTall} src={testTall}
placeholder="blur" placeholder="blur"
/> />
<Image id="object-src-with-svg" src={svg} />
<p id="stubtext">This is valid usage of the Image component</p> <p id="stubtext">This is valid usage of the Image component</p>
</div> </div>
) )
......
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg version="1.0" xmlns="http://www.w3.org/2000/svg" width="400" height="400" viewBox="0 0 400 400" preserveAspectRatio="xMidYMid meet">
<g transform="translate(0.000000,400.000000) scale(0.100000,-0.100000)" fill="#000000" stroke="none">
<path d="M0 2000 l0 -2000 2000 0 2000 0 0 2000 0 2000 -2000 0 -2000 0 0
-2000z m2401 118 l396 -693 -398 -3 c-220 -1 -578 -1 -798 0 l-398 3 396 693
c217 380 398 692 401 692 3 0 184 -312 401 -692z"/>
</g>
</svg>
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment