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

Open
wants to merge 1 commit into
base: main
Choose a base branch
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

…ve imports

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 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
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 p: Facebook Partner: Facebook p: Software Mansion Partner: Software Mansion Partner
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants