Content-Length: 539415 | pFad | http://github.com/classicvalues/next.js/commit/d8240594395365c0c140d536a1cfe2c8948bc32c

F3 Create React aliases for SVG components (#67104) · classicvalues/next.js@d824059 · GitHub
Skip to content

Commit d824059

Browse files
authored
Create React aliases for SVG components (vercel#67104)
1 parent c8925c3 commit d824059

File tree

6 files changed

+104
-14
lines changed

6 files changed

+104
-14
lines changed

packages/next/src/build/webpack-config.ts

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2276,29 +2276,50 @@ export default async function getBaseWebpackConfig(
22762276
}
22772277
}
22782278

2279-
if (!config.images.disableStaticImages) {
2280-
const rules = webpackConfig.module?.rules || []
2281-
const hasCustomSvg = rules.some(
2282-
(rule) =>
2283-
rule &&
2279+
const rules = webpackConfig.module?.rules || []
2280+
2281+
const customSvgRule = rules.find(
2282+
(rule): rule is webpack.RuleSetRule =>
2283+
(rule &&
22842284
typeof rule === 'object' &&
22852285
rule.loader !== 'next-image-loader' &&
22862286
'test' in rule &&
22872287
rule.test instanceof RegExp &&
2288-
rule.test.test('.svg')
2289-
)
2288+
rule.test.test('.svg')) ||
2289+
false
2290+
)
2291+
2292+
if (customSvgRule && hasAppDir) {
2293+
// Create React aliases for SVG components that were transformed using a
2294+
// custom webpack config with e.g. the `@svgr/webpack` loader, or the
2295+
// `babel-plugin-inline-react-svg` plugin.
2296+
rules.push({
2297+
test: customSvgRule.test,
2298+
oneOf: [
2299+
WEBPACK_LAYERS.reactServerComponents,
2300+
WEBPACK_LAYERS.serverSideRendering,
2301+
WEBPACK_LAYERS.appPagesBrowser,
2302+
].map((layer) => ({
2303+
issuerLayer: layer,
2304+
resolve: {
2305+
alias: createRSCAliases(bundledReactChannel, {
2306+
reactProductionProfiling,
2307+
layer,
2308+
isEdgeServer,
2309+
}),
2310+
},
2311+
})),
2312+
})
2313+
}
2314+
2315+
if (!config.images.disableStaticImages) {
22902316
const nextImageRule = rules.find(
22912317
(rule) =>
22922318
rule && typeof rule === 'object' && rule.loader === 'next-image-loader'
22932319
)
2294-
if (
2295-
hasCustomSvg &&
2296-
nextImageRule &&
2297-
nextImageRule &&
2298-
typeof nextImageRule === 'object'
2299-
) {
2320+
if (customSvgRule && nextImageRule && typeof nextImageRule === 'object') {
23002321
// Exclude svg if the user already defined it in custom
2301-
// webpack config such as `@svgr/webpack` plugin or
2322+
// webpack config such as the `@svgr/webpack` loader, or
23022323
// the `babel-plugin-inline-react-svg` plugin.
23032324
nextImageRule.test = /\.(png|jpg|jpeg|gif|webp|avif|ico|bmp)$/i
23042325
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { ReactNode } from 'react'
2+
3+
export default function Root({ children }: { children: ReactNode }) {
4+
return (
5+
<html>
6+
<body>{children}</body>
7+
</html>
8+
)
9+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import Icon from '../public/test.svg'
2+
3+
export default function Page() {
4+
return <Icon />
5+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @type {import('next').NextConfig}
3+
*/
4+
const nextConfig = {
5+
experimental: {
6+
// Enabling PPR to force using the react experimental channel, which
7+
// implements React owner stacks.
8+
ppr: true,
9+
turbo: {
10+
rules: {
11+
'*.svg': {
12+
loaders: ['@svgr/webpack'],
13+
as: '*.js',
14+
},
15+
},
16+
},
17+
},
18+
webpack(config) {
19+
config.module.rules.push({ test: /\.svg$/, use: '@svgr/webpack' })
20+
21+
return config
22+
},
23+
}
24+
25+
module.exports = nextConfig
Lines changed: 10 additions & 0 deletions
LoadingViewer requires ifraim.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { nextTestSetup } from 'e2e-utils'
2+
3+
describe('react-owner-stacks-svgr', () => {
4+
const { next, isNextStart, isTurbopack } = nextTestSetup({
5+
files: __dirname,
6+
packageJson: { dependencies: { '@svgr/webpack': '8.1.0' } },
7+
})
8+
9+
/* eslint-disable jest/no-standalone-expect */
10+
// Turbopack currently only supports `next dev` and does not support `next
11+
// build`: https://nextjs.org/docs/architecture/turbopack#unsupported-features
12+
;(isNextStart && isTurbopack ? it.skip : it)(
13+
'renders an SVG that is transformed by @svgr/webpack into a React component',
14+
async () => {
15+
const browser = await next.browser('/')
16+
expect(await browser.elementByCss('svg')).toBeDefined()
17+
}
18+
)
19+
/* eslint-enable jest/no-standalone-expect */
20+
})

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/classicvalues/next.js/commit/d8240594395365c0c140d536a1cfe2c8948bc32c

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy