Skip to content

Commit

Permalink
Convert with-why-did-you-render example to TypeScript (#43736)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxproske committed Dec 7, 2022
1 parent 783a3ce commit f56e5c9
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 10 deletions.
2 changes: 1 addition & 1 deletion examples/with-why-did-you-render/README.md
Expand Up @@ -3,7 +3,7 @@
This is a simple example of how to use [why-did-you-render](https://github.com/welldone-software/why-did-you-render) within a Next.js app.

We are essentially extending webpack config to allow the monkey patched `React` version of WDYR in development mode and adding to our application
by importing `wdyr.js` at the top of Next.js `_app.js`.
by importing `wdyr.ts` at the top of Next.js `_app.tsx`.

By default, all pure components will be tracked, but you can add
`Component.whyDidYouRender = true` to regular function components in case you need.
Expand Down
6 changes: 4 additions & 2 deletions examples/with-why-did-you-render/next.config.js
@@ -1,12 +1,12 @@
const path = require('path')

/** @type {import('next').NextConfig} */
module.exports = {
const nextConfig = {
webpack(config, { dev, isServer }) {
if (dev && !isServer) {
const originalEntry = config.entry
config.entry = async () => {
const wdrPath = path.resolve(__dirname, './scripts/wdyr.js')
const wdrPath = path.resolve(__dirname, './scripts/wdyr.ts')
const entries = await originalEntry()

if (entries['main.js'] && !entries['main.js'].includes(wdrPath)) {
Expand All @@ -19,3 +19,5 @@ module.exports = {
return config
},
}

module.exports = nextConfig
6 changes: 5 additions & 1 deletion examples/with-why-did-you-render/package.json
Expand Up @@ -11,6 +11,10 @@
"react-dom": "^18.2.0"
},
"devDependencies": {
"@welldone-software/why-did-you-render": "^7.0.1"
"@types/node": "^18.11.10",
"@types/react": "^18.0.26",
"@types/react-dom": "^18.0.9",
"@welldone-software/why-did-you-render": "^7.0.1",
"typescript": "^4.9.3"
}
}
5 changes: 0 additions & 5 deletions examples/with-why-did-you-render/pages/_app.js

This file was deleted.

6 changes: 6 additions & 0 deletions examples/with-why-did-you-render/pages/_app.tsx
@@ -0,0 +1,6 @@
import { AppProps } from 'next/app'
import '../scripts/wdyr'

export default function App({ Component, pageProps }: AppProps) {
return <Component {...pageProps} />
}
@@ -1,4 +1,4 @@
import Header from '../components/header.js'
import Header from '../components/header'

export default function Home() {
return (
Expand Down
@@ -1,3 +1,4 @@
/// <reference types="@welldone-software/why-did-you-render" />
import React from 'react'

if (process.env.NODE_ENV === 'development') {
Expand Down
20 changes: 20 additions & 0 deletions examples/with-why-did-you-render/tsconfig.json
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}

0 comments on commit f56e5c9

Please sign in to comment.