Babel Plugin for React Native Web Alias and Optimization

0.21.2 · active · verified Sun Apr 19

The `babel-plugin-react-native-web` package is a Babel plugin designed to optimize applications using `react-native-web`. It works by aliasing `react-native` imports to `react-native-web` and performing tree-shaking to exclude unused modules, thereby reducing bundle size. The current stable version is 0.21.2. This plugin is part of the `react-native-web` ecosystem, which generally sees frequent updates, often tied to new React and React Native versions, indicating an active development and release cadence. Its primary differentiator is enabling existing React Native codebases to run efficiently on the web without significant code changes, by providing a targeted build-time optimization layer. It's crucial for projects aiming for a single codebase across native and web platforms.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates configuring the `babel-plugin-react-native-web` plugin in a Babel configuration file to enable aliasing and tree-shaking for React Native for Web projects.

// .babelrc or babel.config.js
module.exports = {
  presets: ['module:metro-react-native-babel-preset'], // Or other React/TypeScript presets
  plugins: [
    // ... other plugins
    ["react-native-web", {
      // Set to true if your bundler (e.g., Webpack 4 without module: false) 
      // resolves CommonJS modules by default. Most modern bundlers use ES modules.
      commonjs: false 
    }]
  ]
};

view raw JSON →