Skip to content
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

Update to CRA 5 #4802

Merged
merged 3 commits into from
May 31, 2022
Merged

Update to CRA 5 #4802

merged 3 commits into from
May 31, 2022

Conversation

antgamdia
Copy link
Contributor

@antgamdia antgamdia commented May 30, 2022

Description of the change

Today, I performed a yarn install to update my local deps with the latest dependabot updates. However, I got an error related to webpack 4 not being able to parse some new(ish) JS features like nullish coalescing or optional chaining. The offending dependency was the latest version of yaml dependency that we recently updated.

Instead of work around this issue, I decided to invest some time to look into #4018.

This PR updates to CRA >=5 adding the usual workaround I mentioned in the aforementioned issue: adding the polyfills manually.
However, instead of adding them independently, I've added node-polyfill-webpack-plugin, which should add the required ones more transparently.

The webpack tweak is performed using craco a tool to inject some webpack configuration without ejecting the react-scripts way.

Benefits

We will get rid of many security notices related to CRA < 5.

Possible drawbacks

Not "pure" react-scripts way, but... using craco is way less intrusive than ejecting.

Applicable issues

Additional information

Working locally, but let's see if our CI thinks otherwise :P

Edit: it seems some eslint rules have been added, I'll fix the offending lines in the tests and will push again. DONE

Signed-off-by: Antonio Gamez Diaz <agamez@vmware.com>
@netlify
Copy link

netlify bot commented May 30, 2022

Deploy Preview for kubeapps-dev canceled.

Name Link
🔨 Latest commit 22d3c57
🔍 Latest deploy log https://app.netlify.com/sites/kubeapps-dev/deploys/629547e9ad2870000872b010

@@ -122,7 +124,7 @@
"typescript": "^4.7.2"
},
"resolutions": {
"@babel/parser": "^7.15.3",
"@babel/parser": "^7.18.4",
Copy link
Contributor Author

@antgamdia antgamdia May 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick reminder (as I also had to think twice): this resolutions object is there to prevent react 18 to be selected by default. Our app is not still prepared to run React 18.
Just upgrading to the latest minor version.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation here - sounds like it could be worth a comment in the file at this point to our future selves?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, yarn/npm doesn't accept comments as part of the JSON file (even if jsonc exists...). See https://stackoverflow.com/questions/14221579/how-do-i-add-comments-to-package-json-for-npm-install to see some hacky ways to work around it.
Not sure if we should do something like that, but happy to explore some ways anyway in next PRs.

webpack: {
configure: {
plugins: [
new NodePolyfillPlugin(), // add the required polyfills (not included in webpack 5)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is preventing us from doing something like:

        fallback: {
          process: require.resolve("process/browser"),
          stream: require.resolve("stream-browserify"),
          buffer: require.resolve("buffer"),
          timers: require.resolve("timers-browserify")
          ... // a line per each polyfill we (not really "we" but any random transitive dependency we use) need
        },

antgamdia added 2 commits May 31, 2022 00:39
Signed-off-by: Antonio Gamez Diaz <agamez@vmware.com>
Signed-off-by: Antonio Gamez Diaz <agamez@vmware.com>
Comment on lines +783 to 789
let wrapper: any;
await act(async () => {
wrapper = mountWrapper(defaultStore, <AppRepoForm {...defaultProps} repo={repo} />);
});
await waitFor(() => {
wrapper.update();
expect(wrapper.find("textarea").at(0).prop("value")).toBe("nginx");
Copy link
Contributor Author

@antgamdia antgamdia May 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if the tests were passing, they were throwing a message like (taken from any green CI step)

image

Since mounting AppRepoForm triggers an state update via useEffect in an async fetch function, it has to be wrapped into an act. Then, a .update() call is often required.

I've performed this change in the remaining test cases.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent thanks. Yeah, we'd started using act for async tests in other places, but only as errors were raised in tests. Thanks for doing them all here.

Copy link
Contributor

@absoludity absoludity left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent, thanks for finding a better alternative. cra-configuration override makes it much simpler for us.

Comment on lines +783 to 789
let wrapper: any;
await act(async () => {
wrapper = mountWrapper(defaultStore, <AppRepoForm {...defaultProps} repo={repo} />);
});
await waitFor(() => {
wrapper.update();
expect(wrapper.find("textarea").at(0).prop("value")).toBe("nginx");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent thanks. Yeah, we'd started using act for async tests in other places, but only as errors were raised in tests. Thanks for doing them all here.

@antgamdia antgamdia merged commit 08860ac into vmware-tanzu:main May 31, 2022
@antgamdia antgamdia deleted the update-cra branch May 31, 2022 09:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Update to CRA react-scripts 5.x.x
3 participants