software-mansion / react-native-reanimated

React Native's Animated library reimplemented
https://docs.swmansion.com/react-native-reanimated/
MIT License
8.59k stars 1.26k forks source link

fix empty export on web #6171

Closed EvanBacon closed 2 days ago

EvanBacon commented 5 days ago

Summary

React Native Babel preset uses a loose plugin to convert imports to commonjs, we're investigating switching to a more standard approach in Expo CLI here https://github.com/expo/expo/pull/30005 which aligns with how Metro is used at Meta.

When running on the kitchen sink, I found that there was an invalid expression in reanimated where it exports null and then destructures null. This error didn't occur previously because the commonjs plugin deferred access to the null object until later.

Before

var _Math = _$$_REQUIRE(_dependencyMap[0]);

// Later ...

_Math.add

This coincidentally didn't throw because the deferred code is never accessed.

After

var add = _$$_REQUIRE(_dependencyMap[0]).add;

This throws because the null is accessed when the module is loaded.

Test plan

Bundle a Metro web project with experimentalImportSupport enabled:

config.transformer.getTransformOptions = async () => ({
  transform: {
    experimentalImportSupport: true,
    inlineRequires: false,
  },
});

Then run npx serve dist -> Error was there but not anymore.

piaskowyk commented 3 days ago

Thank you for the fix and detailed description!