Skip to content

Add babel plugin to @react-native/babel-preset for console.warn injection under deep react native imports #50802

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

Closed
wants to merge 1 commit into from

Conversation

coado
Copy link
Contributor

@coado coado commented Apr 18, 2025

Summary:
The plugin analyses the source of all import, require, and export statements and injects the console.warn statement for each path targeting deep react-native source code. It runs only on a dev mode so there is no need to keep that in the if (__DEV__) block. It is possible to disable this plugin by setting disableDeepImportWarnings: true and resetting the Metro cache:

module.exports = {
  presets: [['module:react-native/babel-preset', {
    "disableDeepImportWarnings": true
  }]],
};

Changelog:
[General][Internal] - Added plugin to react-native/babel-preset injecting console.warn for each react native deep import in dev mode.

For a given code:

import { Image } from 'react-native';
import View from 'react-native/Libraries/Components/View/View';
const Text = require('react-native/Libraries/Text/Text');
export { PressabilityDebugView } from 'react-native/Libraries/Pressability/PressabilityDebug';

The transformed output should look like:

import { Image } from 'react-native';
import View from 'react-native/Libraries/Components/View/View';
const Text = require('react-native/Libraries/Text/Text');
export { PressabilityDebugView } from 'react-native/Libraries/Pressability/PressabilityDebug';
console.warn("Deep imports from the 'react-native' package are deprecated ('react-native/Libraries/Components/View/View').");
console.warn("Deep imports from the 'react-native' package are deprecated ('react-native/Libraries/Text/Text').");
console.warn("Deep imports from the 'react-native' package are deprecated ('react-native/Libraries/Pressability/PressabilityDebug').");

For more information about why this plugin was needed, please check RFC.

Reviewed By: huntie

Differential Revision: D70783145

@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. p: Software Mansion Partner: Software Mansion Partner labels Apr 18, 2025
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D70783145

@coado coado changed the title Add metro transform for console.warn generation under deep react native imports Add babel plugin to @react-native/babel-preset for console.warn injection under deep react native imports Apr 18, 2025
@coado coado changed the title Add babel plugin to @react-native/babel-preset for console.warn injection under deep react native imports Add babel plugin to @react-native/babel-preset for console.warn injection under deep react native imports Apr 18, 2025
@coado coado force-pushed the export-D70783145 branch from dd63d1f to ea83a9b Compare April 24, 2025 07:35
coado added a commit to coado/react-native that referenced this pull request Apr 24, 2025
…njection under deep react native imports (facebook#50802)

Summary:

The plugin analyses the source of all `import`, `require`, and `export` statements and injects the `console.warn` statement for each path targeting deep react-native source code. It runs only on a dev mode so there is no need to keep that in the `if (__DEV__) ` block. It is possible to disable this plugin by setting `disableDeepImportWarnings: true` and **resetting** the Metro cache:

```js
module.exports = {
  presets: [['module:react-native/babel-preset', {
    "disableDeepImportWarnings": true
  }]],
};
```

Changelog:
[General][Internal] - Added plugin to react-native/babel-preset injecting `console.warn` for each react native deep import in dev mode.

For a given code:
```js
import { Image } from 'react-native';
import View from 'react-native/Libraries/Components/View/View';
const Text = require('react-native/Libraries/Text/Text');
export { PressabilityDebugView } from 'react-native/Libraries/Pressability/PressabilityDebug';
``` 

The transformed output should look like:

```js
import { Image } from 'react-native';
import View from 'react-native/Libraries/Components/View/View';
const Text = require('react-native/Libraries/Text/Text');
export { PressabilityDebugView } from 'react-native/Libraries/Pressability/PressabilityDebug';
console.warn("Deep imports from the 'react-native' package are deprecated ('react-native/Libraries/Components/View/View').");
console.warn("Deep imports from the 'react-native' package are deprecated ('react-native/Libraries/Text/Text').");
console.warn("Deep imports from the 'react-native' package are deprecated ('react-native/Libraries/Pressability/PressabilityDebug').");
```

For more information about why this plugin was needed, please check [RFC](react-native-community/discussions-and-proposals#894).

Reviewed By: huntie

Differential Revision: D70783145
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D70783145

coado added a commit to coado/react-native that referenced this pull request Apr 24, 2025
…njection under deep react native imports (facebook#50802)

Summary:
Pull Request resolved: facebook#50802

The plugin analyses the source of all `import`, `require`, and `export` statements and injects the `console.warn` statement for each path targeting deep react-native source code. It runs only on a dev mode so there is no need to keep that in the `if (__DEV__) ` block. It is possible to disable this plugin by setting `disableDeepImportWarnings: true` and **resetting** the Metro cache:

```js
module.exports = {
  presets: [['module:react-native/babel-preset', {
    "disableDeepImportWarnings": true
  }]],
};
```

Changelog:
[General][Internal] - Added plugin to react-native/babel-preset injecting `console.warn` for each react native deep import in dev mode.

For a given code:
```js
import { Image } from 'react-native';
import View from 'react-native/Libraries/Components/View/View';
const Text = require('react-native/Libraries/Text/Text');
export { PressabilityDebugView } from 'react-native/Libraries/Pressability/PressabilityDebug';
```

The transformed output should look like:

```js
import { Image } from 'react-native';
import View from 'react-native/Libraries/Components/View/View';
const Text = require('react-native/Libraries/Text/Text');
export { PressabilityDebugView } from 'react-native/Libraries/Pressability/PressabilityDebug';
console.warn("Deep imports from the 'react-native' package are deprecated ('react-native/Libraries/Components/View/View').");
console.warn("Deep imports from the 'react-native' package are deprecated ('react-native/Libraries/Text/Text').");
console.warn("Deep imports from the 'react-native' package are deprecated ('react-native/Libraries/Pressability/PressabilityDebug').");
```

For more information about why this plugin was needed, please check [RFC](react-native-community/discussions-and-proposals#894).

Reviewed By: huntie

Differential Revision: D70783145
@coado coado force-pushed the export-D70783145 branch 2 times, most recently from 4282948 to 23dd5f7 Compare April 24, 2025 11:23
coado added a commit to coado/react-native that referenced this pull request Apr 24, 2025
…njection under deep react native imports (facebook#50802)

Summary:

The plugin analyses the source of all `import`, `require`, and `export` statements and injects the `console.warn` statement for each path targeting deep react-native source code. It runs only on a dev mode so there is no need to keep that in the `if (__DEV__) ` block. It is possible to disable this plugin by setting `disableDeepImportWarnings: true` and **resetting** the Metro cache:

```js
module.exports = {
  presets: [['module:react-native/babel-preset', {
    "disableDeepImportWarnings": true
  }]],
};
```

Changelog:
[General][Internal] - Added plugin to react-native/babel-preset injecting `console.warn` for each react native deep import in dev mode.

For a given code:
```js
import { Image } from 'react-native';
import View from 'react-native/Libraries/Components/View/View';
const Text = require('react-native/Libraries/Text/Text');
export { PressabilityDebugView } from 'react-native/Libraries/Pressability/PressabilityDebug';
``` 

The transformed output should look like:

```js
import { Image } from 'react-native';
import View from 'react-native/Libraries/Components/View/View';
const Text = require('react-native/Libraries/Text/Text');
export { PressabilityDebugView } from 'react-native/Libraries/Pressability/PressabilityDebug';
console.warn("Deep imports from the 'react-native' package are deprecated ('react-native/Libraries/Components/View/View').");
console.warn("Deep imports from the 'react-native' package are deprecated ('react-native/Libraries/Text/Text').");
console.warn("Deep imports from the 'react-native' package are deprecated ('react-native/Libraries/Pressability/PressabilityDebug').");
```

For more information about why this plugin was needed, please check [RFC](react-native-community/discussions-and-proposals#894).

Reviewed By: huntie

Differential Revision: D70783145
…njection under deep react native imports (facebook#50802)

Summary:
Pull Request resolved: facebook#50802

The plugin analyses the source of all `import`, `require`, and `export` statements and injects the `console.warn` statement for each path targeting deep react-native source code. It runs only on a dev mode so there is no need to keep that in the `if (__DEV__) ` block. It is possible to disable this plugin by setting `disableDeepImportWarnings: true` and **resetting** the Metro cache:

```js
module.exports = {
  presets: [['module:react-native/babel-preset', {
    "disableDeepImportWarnings": true
  }]],
};
```

Changelog:
[General][Internal] - Added plugin to react-native/babel-preset injecting `console.warn` for each react native deep import in dev mode.

For a given code:
```js
import { Image } from 'react-native';
import View from 'react-native/Libraries/Components/View/View';
const Text = require('react-native/Libraries/Text/Text');
export { PressabilityDebugView } from 'react-native/Libraries/Pressability/PressabilityDebug';
```

The transformed output should look like:

```js
import { Image } from 'react-native';
import View from 'react-native/Libraries/Components/View/View';
const Text = require('react-native/Libraries/Text/Text');
export { PressabilityDebugView } from 'react-native/Libraries/Pressability/PressabilityDebug';
console.warn("Deep imports from the 'react-native' package are deprecated ('react-native/Libraries/Components/View/View').");
console.warn("Deep imports from the 'react-native' package are deprecated ('react-native/Libraries/Text/Text').");
console.warn("Deep imports from the 'react-native' package are deprecated ('react-native/Libraries/Pressability/PressabilityDebug').");
```

For more information about why this plugin was needed, please check [RFC](react-native-community/discussions-and-proposals#894).

Reviewed By: huntie

Differential Revision: D70783145
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D70783145

@facebook-github-bot
Copy link
Contributor

This pull request has been merged in 3e14d16.

@react-native-bot
Copy link
Collaborator

This pull request was successfully merged by @coado in 3e14d16

When will my fix make it into a release? | How to file a pick request?

EvanBacon added a commit to expo/expo that referenced this pull request May 15, 2025
…reset` (#36362)

# Why

<!--
Please describe the motivation for this PR, and link to relevant GitHub
issues, forums posts, or feature requests.
-->

The react-native `0.80` version will introduce [deep import warnings
plugin](facebook/react-native#50802) built in
`@react-native/babel-preset`. This is the first step towards deprecating
deep react native imports in favor of a more stricter root imports. It
should be possible to opt-out of these warnings by passing
`disableDeepImportWarnings` option to the `@react-native/babel-preset`.

# How

<!--
How did you build this feature or fix this bug and why?
-->

This PR adds `disableDeepImportWarnings` property to the
`BabelPresetExpoPlatformOptions` which is passed to the base
react-native preset. The plugin can be disabled by modifying
`babel.config.js`:

```js
module.exports = function (api) {
    api.cache(true);
    return {
      presets: [['babel-preset-expo', { disableDeepImportWarnings: true}]],
    };
  };

```

# Test Plan

<!--
Please describe how you tested this change and how a reviewer could
reproduce your test, especially if this PR does not include automated
tests! If possible, please also provide terminal output and/or
screenshots demonstrating your test/reproduction.
-->

Tested on a new app with Expo by adding this plugin in the
`node_modules`:

| Disabled | Default |
|--------|-------|
| <img width="1199" alt="disabled"
src="https://github.com/user-attachments/assets/a38b67d2-cf6b-4375-8543-1464d6d361ca"
/> | <img width="1199" alt="default"
src="https://github.com/user-attachments/assets/887c2b76-b922-48dc-9472-f1660478c7af"
/> |

# Checklist

<!--
Please check the appropriate items below if they apply to your diff.
-->

- [x] I added a `changelog.md` entry and rebuilt the package sources
according to [this short
guide](https://github.com/expo/expo/blob/main/CONTRIBUTING.md#-before-submitting)
- [x] This diff will work correctly for `npx expo prebuild` & EAS Build
(eg: updated a module plugin).
- [x] Conforms with the [Documentation Writing Style
Guide](https://github.com/expo/expo/blob/main/guides/Expo%20Documentation%20Writing%20Style%20Guide.md)

---------

Co-authored-by: Evan Bacon <bacon@expo.io>
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. fb-exported Merged This PR has been merged. p: Facebook Partner: Facebook p: Software Mansion Partner: Software Mansion Partner
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants