Skip to content

Commit e67364d

Browse files
committed
chore: update README with detailed Next.js integration instructions and examples
1 parent 9f7c372 commit e67364d

File tree

1 file changed

+90
-23
lines changed

1 file changed

+90
-23
lines changed

README.md

Lines changed: 90 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# React displayName Plugin
66

7-
**react-display-name-plugin** is a build plugin for both Webpack and Vite that makes your custom
7+
**react-display-name-plugin** is a build plugin for Webpack, Next.js & Vite that makes your custom
88
React components visible within React Dev Tools and other tools that rely on the displayName parameter.
99

1010
_Note: This package supports Webpack 5 and Vite 2+. For older versions (Webpack 4), see the legacy package [@mockingjay-io/webpack-react-component-name](https://github.com/mockingjay-io/webpack-react-component-name)._
@@ -46,8 +46,6 @@ module.exports = {
4646
};
4747
```
4848

49-
**Next.js users** have to add this within `next.config.js`/`next.config.mjs`/`next.config.ts`. Examples available [here](https://github.com/mockingjay-io/react-display-name-plugin/tree/main/examples).
50-
5149
### For Vite
5250

5351
2. Import and add the plugin to your Vite configuration:
@@ -70,6 +68,95 @@ export default defineConfig({
7068

7169
**Note:** The Vite plugin should be placed after the React plugin in your plugins array, as it needs to run after JSX transformation.
7270

71+
### For Next.js
72+
73+
Next.js uses Webpack under the hood, so you'll need to customize the Webpack configuration in your Next.js config file.
74+
75+
> **⚠️ Important:** This plugin currently only works with Webpack. If you're using Turbopack (Next.js's new bundler), you'll need to [opt back into using Webpack](https://nextjs.org/docs/app/api-reference/turbopack#using-webpack-instead) by removing the `--turbo` flag from your dev command or setting `experimental.turbo` to `false` in your config.
76+
77+
#### Using next.config.js (CommonJS)
78+
79+
```js
80+
const ReactDisplayNamePlugin = require('react-display-name-plugin/webpack');
81+
82+
/** @type {import('next').NextConfig} */
83+
const nextConfig = {
84+
webpack: (config) => {
85+
config.plugins.push(
86+
new ReactDisplayNamePlugin({
87+
parseDependencies: true,
88+
})
89+
);
90+
91+
return config;
92+
},
93+
};
94+
95+
module.exports = nextConfig;
96+
```
97+
98+
#### Using next.config.mjs (ES Modules)
99+
100+
```js
101+
import ReactDisplayNamePlugin from 'react-display-name-plugin/webpack';
102+
103+
/** @type {import('next').NextConfig} */
104+
const nextConfig = {
105+
webpack: (config) => {
106+
config.plugins.push(
107+
new ReactDisplayNamePlugin({
108+
parseDependencies: true,
109+
})
110+
);
111+
112+
return config;
113+
},
114+
};
115+
116+
export default nextConfig;
117+
```
118+
119+
#### Suppressing Webpack Cache Warnings (Optional)
120+
121+
Next.js may generate warnings like `[webpack.cache.PackFileCacheStrategy] Skipped not serializable cache item`. These are safe to ignore, but if you want to suppress them:
122+
123+
```js
124+
import ReactDisplayNamePlugin from 'react-display-name-plugin/webpack';
125+
126+
const webpackComponentNamesAppenderCacheWarning =
127+
/Skipped not serializable cache item.*ModuleAppenderDependency/i;
128+
129+
/** @type {import('next').NextConfig} */
130+
const nextConfig = {
131+
webpack: (config) => {
132+
config.plugins.push(
133+
new ReactDisplayNamePlugin({
134+
parseDependencies: true,
135+
})
136+
);
137+
138+
// Suppress cache warnings
139+
config.infrastructureLogging = {
140+
level: 'error',
141+
stream: {
142+
write: (message) => {
143+
if (webpackComponentNamesAppenderCacheWarning.test(message)) {
144+
return;
145+
}
146+
process.stderr.write(message);
147+
},
148+
},
149+
};
150+
151+
return config;
152+
},
153+
};
154+
155+
export default nextConfig;
156+
```
157+
158+
**See working examples:** Check the [examples directory](https://github.com/mockingjay-io/react-display-name-plugin/tree/main/examples) for Next.js 12, 13, and 14 configurations with both App Router and Pages Router.
159+
73160
### Core Utilities (Advanced)
74161

75162
For building custom plugins for other bundlers, you can import the core utilities directly:
@@ -144,26 +231,6 @@ If we are not detecting one of your components, please either file an Issue cont
144231
example source for a component which is not detected, or feel free to open a PR with
145232
the fix.
146233

147-
## Note for Next.js users
148-
149-
In Next.js the plugin may cause warnings like `[webpack.cache.PackFileCacheStrategy] Skipped not serializable cache item` to be generated. These warnings are safe to ignore without any further action. But if you'd like to supress these warnings, as an interim solution, the following snippet can be added to your webpack config.
150-
151-
```js
152-
const webpackComponentNamesAppenderCacheWarning =
153-
/Skipped not serializable cache item.*ModuleAppenderDependency/i;
154-
155-
config.infrastructureLogging = {
156-
stream: {
157-
write: (message) => {
158-
if (webpackComponentNamesAppenderCacheWarning.test(message)) {
159-
return;
160-
}
161-
process.stderr.write(message);
162-
},
163-
},
164-
};
165-
```
166-
167234
## License
168235

169236
This project is licensed under the terms of the MIT license. See `LICENSE.md` for more info.

0 commit comments

Comments
 (0)