Doesn't work with Vite + React
See original GitHub issueHello ! I’m currently having troubles trying to use this library with my React project.
This is my wdyr.ts
file:
/// <reference types="@welldone-software/why-did-you-render" />
import React from "react";
export default async function load () {
if (process.env.NODE_ENV === "development") {
// Importing because we can't require in Vite.
const { default: whyDidYouRender } = await import("@welldone-software/why-did-you-render");
whyDidYouRender(React, {
trackAllPureComponents: true,
});
}
}
This is how I integrate it in my main.tsx
(mountpoint)
import wdyr from "./wdyr";
await wdyr();
import React from "react";
import ReactDOM from "react-dom";
ReactDOM.render(
<React.StrictMode>
<AppRoutingAndMoreRightHere />
</React.StrictMode>,
document.getElementById("root")
);
When I run it, there isn’t any errors, but still it doesn’t work.
I’ve also tried to add MyComponent.whyDidYouRender = true;
but still have no effect.
By the way, I’m using vite 2.7.13
and react 17.0.2
.
Hope you can help me with this !
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:21
Top Results From Across the Web
Build is not working in react using vite bundler
The thing is that vite does not import React by default into jsx components, so you need to do it manually in every...
Read more >Troubleshooting
Vite cannot handle and does not support code that only runs on non-strict mode (sloppy mode). This is because Vite uses ESM and...
Read more >How To Set Up a React Project with Vite
Vite allows you to bootstrap a range of project types, not just React. Currently, it supports React, Preact, Vue, Lit, Svelte, and vanilla ......
Read more >Vite 3.0 vs. Create React App
How do Vite and Create React App work? Vite and CRA are not as different as you might think. At their core, they...
Read more >4 Reasons Why You Should Prefer Vite Over Create-React ...
To address these issues, there is a new front-end tooling in the ecosystem: Vite! Unlike CRA, Vite does not build your entire application...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Solution: vite.config.ts
wdyr.ts
Edit:
globEager
cannot be used conditionally like I have it below, so for vite purposes my first example is still what we are using.So it looks like instead we can leverage vite’s
globEager
(docs appear out of date sinceglobEager
is mentioned there as an option) usage to have this do what we want. We cannot use require with vite, and this issue is specific to vite.main.tsx
wdyr.ts
That said, I believe that my original suggestion still works the same since the
import './wdyr'
line will be converted to a static require with vite, and then the import of wdyr should be completed synchronously before the rest of the code.