-
Notifications
You must be signed in to change notification settings - Fork 199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support create-react-app/react-scripts 4 #154
Comments
React 17 support is incoming in version 6 which is in progress. |
I saw that, but cra 4 also breaks with react 16. |
Probably you are using the new JSX transform, I don't think it's supported right now. |
" cra 4 also breaks with react 16." |
It is, they released a new version to support it. |
oh right right. i hope ill get to fixing it asap |
EDIT: This is a good idea, but can't be implemented in CRA4. |
How can I import it before react hot loader in Create React App? |
It seems to me like the library will not currently support CRA. |
sadly, CRA is not flexible enough to support the patching of the new JSX transformation.
|
Any chance you could use Babel macros to overcome the limitation? |
Hi @vzaidman, wondering what's the issue with CRA everyone is mentioning here? The new React update should be concerned only with the JSX factory, no? What changed in CRA? |
it's seems to be kind of impossible to reconfigure it's webpack and babel. And it's needed in order to use WDYR with the new JSX. |
@vzaidman Are you able to use https://github.com/gsoft-inc/craco to achieve what you're trying to do? |
It seems like it doesn't support CRA4 as well: |
Bump? I can't believe Babel can't help here. |
what i need is to reconfigure the JSX transformation plugin (in
do you think it's possible with CRA 4? |
Would it be enough to alias |
It might work. |
In that case, maybe it's not very ergonomic, but one could use the |
ok i got it to work with Step 1: Step 2: const presetReact = require('@babel/preset-react').default;
const presetCRA = require('babel-preset-react-app');
module.exports = {
babel: {
loaderOptions: (babelLoaderOptions, { env, paths }) => {
const origBabelPresetReactAppIndex = babelLoaderOptions.presets.findIndex(preset => {
return preset[0].includes('babel-preset-react-app')
})
if (origBabelPresetReactAppIndex === -1) {
return babelLoaderOptions;
}
const overridenBabelPresetReactApp = (...args) => {
const babelPresetReactAppResult = presetCRA(...args);
const origPresetReact = babelPresetReactAppResult.presets.find(preset => {
return preset[0] === presetReact;
});
Object.assign(origPresetReact[1], {
importSource: '@welldone-software/why-did-you-render',
});
return babelPresetReactAppResult;
}
babelLoaderOptions.presets[origBabelPresetReactAppIndex] = overridenBabelPresetReactApp;
return babelLoaderOptions;
}
},
}; |
It sounds too intrusive to me. |
I just realized the If we use that in edit: can confirm it works!!! The following is enough to make CRA work: /** @jsxImportSource @welldone-software/why-did-you-render */
import React from 'react';
if (process.env.NODE_ENV === 'development') {
const whyDidYouRender = require('@welldone-software/why-did-you-render');
whyDidYouRender(React, {
trackAllPureComponents: true,
});
} |
Wait, does it changes the |
I suppose it only does that for the one in the wdyr.js module, but from what I could observe everything is working good all around the app |
it seems like it needs to be added to all files. |
The code I provided is enough, why you say it's not? I tested it in my app and all the components were properly instrumented |
@nicolas-rohricht When I have had that in the past (unrelated to this project) it’s due to my component’s displayName not being known. In the main case that I saw it, it was when I was using a => anonymous function within React.memo, causing React to not be able to get the displayName. Solutions were to explicitly set the displayName, or to use a named function instead. If you Google something like “why is my react component showing as unknown in dev tools” or something like that I believe there should be a whole pile of useful resources on the topic. |
see the solutions proposed there: as a rule of law, better wrap components with memo after creating them:
but there are some more options in the bug linked above |
@bmoore235 I followed your post and it worked perfectly 👍🏼 . Thanks for the update. |
|
I managed to make it working with
|
@vzaidman Please update the link on the front page to the new solution, since the old one doesn't work (at least it didn't work me), or even better, incorporate the solution to the front page installation manual (CRA is a very common). Please change the line or something similar, because the optional chaining operator didn't work for me (syntax error, so I guess parsing with older version of ECMAScript) and the solution should be copy/paste if possible for lowest common denominator. |
updated. thanks |
@vzaidman Thank you for Your solution works for me |
I wanted to track my re-renders but unfortunately am missing something. I cant see the wdyr logs. Here is the installation process.
Please let me know what is wrong. I really need to test my app. |
you don't need to touch the config in the comment #154 (comment) is a comment for https://github.com/gsoft-inc/craco. so install https://github.com/gsoft-inc/craco and use this code in it's config |
Thanks @vzaidman! I started seeing some logs but not all. Steps used:
When I tried adding I got this error.
|
Great! Regarding the workaround- It's not possible as long as we can't pass options to |
Instead of relying on craco, I'd like to use patch-package to just change the code inside Is this the correct code to use? [
require('@babel/preset-react').default,
{
// Adds component stack to warning messages
// Adds __self attribute to JSX which React will use for some warnings
development: isEnvDevelopment || isEnvTest,
// Will use the native built-in instead of trying to polyfill
// behavior for any plugins that require one.
...(opts.runtime !== 'automatic' ? { useBuiltIns: true } : {}),
// runtime: opts.runtime || 'classic',
// See https://github.com/welldone-software/why-did-you-render/issues/154#issuecomment-732711993
runtime: 'automatic',
development: process.env.NODE_ENV === 'development',
importSource: '@welldone-software/why-did-you-render',
},
], Here's what it will be replacing from https://github.com/facebook/create-react-app/blob/main/packages/babel-preset-react-app/create.js#L90-L101: [
require('@babel/preset-react').default,
{
// Adds component stack to warning messages
// Adds __self attribute to JSX which React will use for some warnings
development: isEnvDevelopment || isEnvTest,
// Will use the native built-in instead of trying to polyfill
// behavior for any plugins that require one.
...(opts.runtime !== 'automatic' ? { useBuiltIns: true } : {}),
runtime: opts.runtime || 'classic',
},
], |
It seems correct. Tell us if it works, please. |
It seems to be working. |
@justingrant To make it easier for others trying this approach out, can you reference your patch file, and the patch command you ended up writing (i.e. did you end up leveraging the Dev-only patches capability, etc). |
Sure, here's the patch: https://gist.github.com/justingrant/f8553e52ec9ce26f15009e73f54e609e I used just plain vanilla
Isn't this package is only used in development? ow would the patched code get into the production bundle? Patching only in dev sounds like a good idea in general, but just not sure why it matters in this case. |
The library is not tested in prod. It also makes the bundle size bigger and most importantly may cause significant errors in certain edge cases. I wouldn't get it near production. Only turn it on here and there to debug performance. |
Oh, I was only referring to the patch to |
Is there a macro for why-did-you-render? |
There's no macro. From my humble knowledge of macros, we can't implement many WDYR features with it. Do you think there is a way? |
@vzaidman After upgrading to CRA v5, I am running into one of the flavours of the "getter" errors I've seen people post in this and other threads. I was already using the CRACO fix and don't use Redux, any idea what might be causing it? |
The sandbox doesn't seem to work for me (problem with deps fetching). I'll try again later. |
#231 config-overrides.js /* config-overrides.js */
// @link https://github.com/timarney/react-app-rewired/
const getLogger = require('webpack-log');
const rewireTypingsForCssModule = require("react-app-rewire-typings-for-css-module");
const {
override,
addWebpackPlugin, getBabelLoader,
} = require('customize-cra')
const log = getLogger({name: 'config-overrides.js'});
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const {
defaultGetLocalIdent
} = require("css-loader/dist/utils")
module.exports = override(
addWebpackPlugin(new MiniCssExtractPlugin()),
function (config, _env) {
log.info('webpack env', process.env.NODE_ENV)
// what's going on here? @link https://stackoverflow.com/questions/73551420/removing-hash-from-react-css-modules
// noinspection JSUnusedGlobalSymbols
config = rewireTypingsForCssModule.factory({
modules: {
exportLocalsConvention: 'camelCase',
mode: 'local',
localIdentName: 'production' === process.env.NODE_ENV ? '[hash:base64:5]' : '[path][name]__[local]--[hash:base64:5]',
exportGlobals: true,
getLocalIdent: (context, localIdentName, localName, options) => {
if (false === context.resourcePath.endsWith('/react/src/variables/bootstrap.module.scss')) {
localName = defaultGetLocalIdent(context, localIdentName, localName, options)
.replace("[local]", localName)
}
return localName;
}
}
})(config); // do not pass env as it will cause the production build will not be modified
// log.info('webpack configuration post config-overrides.js', JSON.stringify(config))
return config;
},
(config) => {
const options = getBabelLoader(config).options;
const originalPreset = options.presets.find((preset) => preset[0].includes('babel-preset-react-app'));
if (originalPreset) {
Object.assign(originalPreset[1], {
development: true,
importSource: '@welldone-software/why-did-you-render',
});
}
return config;
}
); |
I'm not sure what part of the system causes things to break, but using create-react-app 4 seems to effectively disable why-did-you-render.
Switching back to react-scripts 3.4.4 fixes things.
The text was updated successfully, but these errors were encountered: