Skip to content

Babel plugin: a namespace or default import of react-native-paper fails the build #5227

Open

Description

@Doberjohn

Current behaviour

With Paper's optional Babel plugin enabled (react-native-paper/babel, as in the Bundle size section of Getting Started), any file that imports Paper as a namespace or with a default import fails to compile:

App.js: Cannot read properties of undefined (reading 'name')

Both of these crash:

import * as Paper from 'react-native-paper';
import Paper, { Button } from 'react-native-paper';

The plugin rewrites each import specifier with mappings[specifier.imported.name] (src/babel/index.js, line 20). Only named import specifiers have imported. Namespace and default specifiers don't, so specifier.imported is undefined. A named import such as import { Button } from 'react-native-paper' works.

Since the plugin usually runs only in the production Babel env, the app works in development and the crash only shows up in a release build.

Expected behaviour

The plugin leaves namespace and default imports of react-native-paper as they are, or rewrites only what it can, and the file compiles. Tests cover a namespace import, a default import and a default import mixed with named ones.

How to reproduce?

  1. Add the plugin as Getting Started's Bundle size section shows:
    module.exports = {
      presets: ['module:@react-native/babel-preset'],
      env: {
        production: {
          plugins: ['react-native-paper/babel'],
        },
      },
    };
  2. In any file, write import * as Paper from 'react-native-paper'; and use Paper.Button.
  3. Make a release bundle, for example npx react-native bundle --platform android --dev false --entry-file index.js --bundle-output out.js.

The same error comes from running the plugin alone through @babel/core on either line above.

What have you tried so far?

Using only named imports avoids it. #3817 reported the namespace case for release bundles in 2023 and was closed in 2024, but the plugin still reads specifier.imported.name without a check on main. #4874 (re-exports aren't rewritten) and its PR #4969 touch the same code, so they fit in the same fix.

Your Environment

software version
react-native-paper 6.0.0-alpha.3 (main at 8787d4b); 5.x has the same code
@babel/core 7.x
react-native 0.88.1
node 24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions