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

refactor: Convert Animated directory to use ESModule imports/exports #34539

Conversation

gabrieldonadel
Copy link
Collaborator

Summary

This PR refactors the Animated directory to use ESModule imports/exports instead of using a mixture of the 2 module formats, as requested on #34425.

Changelog

[Internal] [Changed] - Convert all files in the Animated directory to use ESModule imports/exports

Test Plan

This doesn't really add or modify any existing features so checking if CI passes should be enough

@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. labels Aug 31, 2022
@gabrieldonadel gabrieldonadel force-pushed the refactor/animated-directory-to-use-es6 branch from 9ed091b to ea74740 Compare August 31, 2022 01:06
@analysis-bot
Copy link

analysis-bot commented Aug 31, 2022

Platform Engine Arch Size (bytes) Diff
ios - universal n/a --

Base commit: b2452ab
Branch: main

@gabrieldonadel gabrieldonadel force-pushed the refactor/animated-directory-to-use-es6 branch from ea74740 to be02361 Compare August 31, 2022 01:29
@analysis-bot
Copy link

analysis-bot commented Aug 31, 2022

Platform Engine Arch Size (bytes) Diff
android hermes arm64-v8a 7,641,961 +380
android hermes armeabi-v7a 7,054,187 +385
android hermes x86 7,943,538 +385
android hermes x86_64 7,915,532 +378
android jsc arm64-v8a 9,515,017 +295
android jsc armeabi-v7a 8,290,641 +310
android jsc x86 9,454,346 +295
android jsc x86_64 10,045,412 +293

Base commit: b2452ab
Branch: main

Copy link
Member

@javache javache left a comment

Choose a reason for hiding this comment

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

Looks like CI tests are still failing and there's a lint warning.

ScrollView: AnimatedScrollView,
SectionList: AnimatedSectionList,
Text: AnimatedText,
View: AnimatedView,
...Animated,
};
Copy link
Member

Choose a reason for hiding this comment

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

I think we'll want to keep this, as it enables lazy initialization of these modules (if you don't have the inline-requires transform enabled)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Got it, I'll revert this

Copy link
Contributor

Choose a reason for hiding this comment

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

@javache What do you mean by this if you don't have the inline-requires transform enabled ? Could you explain it a little bit more ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Reverted

Copy link
Member

Choose a reason for hiding this comment

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

@TMaszko see https://reactnative.dev/docs/ram-bundles-inline-requires for details on inline-requires. It's an important transform we apply to improve bundle init time.

Copy link
Contributor

@TMaszko TMaszko Sep 1, 2022

Choose a reason for hiding this comment

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

@javache I know that optimization technique. My question is why getters do enable lazy initialization when inline-requires transform is disabled? I thought it will work only when this transform is enabled that's why I wanted you to explain it ;)

Copy link
Member

Choose a reason for hiding this comment

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

Ah, sorry for misunderstanding. In this case the require is manually inlined, and we would only invoke the module factory for that module once the getter is invoked. When using import syntax, that manual inlining is not possible.

When the inline-requires transform is enabled, you get this behaviour for free (as long as you're using a getter in this case)

Libraries/Animated/NativeAnimatedHelper.js Outdated Show resolved Hide resolved
@gabrieldonadel gabrieldonadel force-pushed the refactor/animated-directory-to-use-es6 branch from be02361 to f24ae20 Compare August 31, 2022 15:02
@@ -21,7 +21,7 @@ jest.mock('../../BatchedBridge/NativeModules', () => ({
},
}));

let Animated = require('../Animated');
let Animated = require('../Animated').default;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Kept it like this because using ES module imports here breaks jest mocks

Comment on lines +29 to +30
const Animated = require('../Animated').default;
const NativeAnimatedHelper = require('../NativeAnimatedHelper').default;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Kept it like this because using ES module imports here breaks jest mocks

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@javache please let me know if this approach is ok for test files of if we should do something different

Copy link
Member

Choose a reason for hiding this comment

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

It should be possible to "fix" jest mocks to work with ES module imports too, but I'm no expert.

Copy link
Contributor

Choose a reason for hiding this comment

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

If you have to dynamically import module and reset it for some reason after each test you have to stick to the require unfortunately :(. In this case though ES module imports should work just fine.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I believe we won't be able to replace any of these requires from jest test files, on these tests we rely on jest.resetModules and that seems to only work with require() (jestjs/jest#3236).
Any thoughts on what we should do? @javache @TMaszko @necolas

Copy link
Contributor

Choose a reason for hiding this comment

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

Tests don't matter so much. It's the Animated module syntax that I have to edit every time I sync code to React Native for Web.

Copy link
Contributor

Choose a reason for hiding this comment

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

@gabrieldonadel Sorry, but I haven't spotted jest.resetModules calls. In this case in my opinion we have to just deal with require.

@@ -11,9 +11,9 @@

'use strict';

const createAnimatedComponent = require('../createAnimatedComponent');
const createAnimatedComponent = require('../createAnimatedComponent').default;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Using import here also breaks jest

@gabrieldonadel gabrieldonadel requested review from javache and TMaszko and removed request for javache and TMaszko August 31, 2022 15:14
@gabrieldonadel gabrieldonadel force-pushed the refactor/animated-directory-to-use-es6 branch from a2155bb to f99174d Compare September 1, 2022 16:24
@facebook-github-bot
Copy link
Contributor

@javache has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@gabrieldonadel gabrieldonadel force-pushed the refactor/animated-directory-to-use-es6 branch from f99174d to f45fb04 Compare September 8, 2022 18:59
@javache
Copy link
Member

javache commented Sep 9, 2022

This is currently blocked on merging internally, since it breaks the flow-type export of Animated.Numeric

@gabrieldonadel
Copy link
Collaborator Author

This is currently blocked on merging internally, since it breaks the flow-type export of Animated.Numeric

Is there something that I can help with?

@react-native-bot
Copy link
Collaborator

This pull request was successfully merged by @gabrieldonadel in f63d4e7.

When will my fix make it into a release? | Upcoming Releases

@react-native-bot react-native-bot added the Merged This PR has been merged. label Sep 20, 2022
@gabrieldonadel gabrieldonadel deleted the refactor/animated-directory-to-use-es6 branch September 20, 2022 17:19
OlimpiaZurek pushed a commit to OlimpiaZurek/react-native that referenced this pull request May 22, 2023
…acebook#34539)

Summary:
This PR refactors the Animated directory to use ESModule imports/exports instead of using a mixture of the 2 module formats, as requested on facebook#34425.

## Changelog

[Internal] [Changed] - Convert all files in the Animated directory to use ESModule imports/exports

Pull Request resolved: facebook#34539

Test Plan: This doesn't really add or modify any existing features so checking if CI passes should be enough

Reviewed By: yungsters

Differential Revision: D39235720

Pulled By: yungsters

fbshipit-source-id: 84b4c0a71dc9fca1ab7053263f1cf7c336df58c1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants