Transform plugins for Metro bundler that provide code optimization and platform-specific transformations for React Native applications
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Inlines platform-specific checks, development flags, and environment variables for React Native optimization, enabling platform-specific code paths and development vs production builds.
Performs compile-time inlining of platform checks and environment variables for React Native optimization.
/**
* Creates a Babel plugin that inlines platform-specific code and environment variables
* @param context - Babel plugin context with types utility
* @param options - Configuration options for inlining behavior
* @returns Babel plugin object with platform and environment optimization
*/
function inlinePlugin(
context: { types: Types },
options: InlinePluginOptions
): PluginObj<State>;
interface InlinePluginOptions {
/** Whether this is a development build */
dev: boolean;
/** Whether to inline Platform.OS and Platform.select() calls */
inlinePlatform: boolean;
/** Whether the module is wrapped (affects scope detection) */
isWrapped: boolean;
/** Custom require function name (default: 'require') */
requireName?: string;
/** Target platform name (e.g., 'ios', 'android', 'web') */
platform: string;
}
interface State {
opts: InlinePluginOptions;
}Usage Examples:
const babel = require("@babel/core");
const { inlinePlugin } = require("metro-transform-plugins");
const code = `
import { Platform } from 'react-native';
if (__DEV__) {
console.log('Development mode');
}
const platformSpecific = Platform.select({
ios: 'iOS specific code',
android: 'Android specific code',
default: 'Fallback code'
});
const isProduction = process.env.NODE_ENV === 'production';
`;
const transformed = babel.transformSync(code, {
plugins: [
[inlinePlugin, {
dev: false,
inlinePlatform: true,
isWrapped: false,
platform: 'ios'
}]
]
});
// Result for iOS production build:
// import { Platform } from 'react-native';
//
// if (false) { // __DEV__ inlined to false
// console.log('Development mode');
// }
//
// const platformSpecific = 'iOS specific code'; // Platform.select inlined
//
// const isProduction = 'production'; // process.env.NODE_ENV inlinedReplaces __DEV__ global with boolean literals based on build configuration:
/**
* __DEV__ global inlining
* Replaces __DEV__ identifier with boolean literal
*/
// When dev: true
if (__DEV__) { /* code */ } // → if (true) { /* code */ }
// When dev: false
if (__DEV__) { /* code */ } // → if (false) { /* code */ }
// Note: Combined with constant folding, this enables dead code eliminationInlines process.env.NODE_ENV to string literals:
/**
* Process environment inlining
* Replaces process.env.NODE_ENV with string literals
*/
// When dev: true
process.env.NODE_ENV === 'development' // → 'development' === 'development'
// When dev: false
process.env.NODE_ENV === 'production' // → 'production' === 'production'Inlines Platform.OS member expressions to platform string literals:
/**
* Platform.OS inlining
* Replaces Platform.OS with target platform string
*/
// Configuration: platform: 'ios'
Platform.OS === 'ios' // → 'ios' === 'ios'
Platform.OS === 'android' // → 'ios' === 'android'
// Works with various Platform import patterns:
// import { Platform } from 'react-native';
// import * as ReactNative from 'react-native'; // ReactNative.Platform.OS
// const Platform = require('react-native').Platform;Inlines Platform.select() calls to platform-specific values:
/**
* Platform.select() call inlining
* Replaces Platform.select() with platform-specific value
*/
// Original code
Platform.select({
ios: 'iOS value',
android: 'Android value',
web: 'Web value',
native: 'Native fallback',
default: 'Default fallback'
});
// When platform: 'ios' → 'iOS value'
// When platform: 'android' → 'Android value'
// When platform: 'web' → 'Web value'
// When platform: 'windows' → 'Native fallback' (if present)
// When platform: 'unknown' → 'Default fallback' (if present)
// When no match → undefinedUses sophisticated AST analysis to detect platform-related code patterns:
/**
* Platform detection utilities
* Identifies platform-related expressions and calls
*/
interface PlatformChecks {
/** Detects Platform.OS member expressions */
isPlatformNode(
node: MemberExpression,
scope: Scope,
isWrappedModule: boolean
): boolean;
/** Detects Platform.select() call expressions */
isPlatformSelectNode(
node: CallExpression,
scope: Scope,
isWrappedModule: boolean
): boolean;
}Recognizes various import patterns for React Native Platform:
// Direct import
import { Platform } from 'react-native';
// Namespace import
import * as ReactNative from 'react-native';
// Usage: ReactNative.Platform.OS
// CommonJS require
const { Platform } = require('react-native');
// Full namespace require
const ReactNative = require('react-native');
// Usage: ReactNative.Platform.OS
// Alternative React import (mapped internally)
import * as React from 'react';
// Usage: React.Platform.OS (mapped to 'react-native')Properly handles variable scope to avoid false positives:
/**
* Scope checking prevents incorrect inlining
* Only inlines global or Flow-declared variables
*/
// Global Platform - Will be inlined
Platform.OS
// Local Platform variable - Will NOT be inlined
function component() {
const Platform = { OS: 'custom' };
return Platform.OS; // Not inlined - local binding
}
// Wrapped module context detection
// Handles Metro's module wrapper function scope correctlyEnsures Platform.select objects have static properties before inlining:
/**
* Static property validation
* Only inlines Platform.select() with static object properties
*/
// Valid for inlining - static properties
Platform.select({
ios: 'static',
android: 'values'
});
// NOT inlined - dynamic properties
Platform.select({
[dynamicKey]: 'value',
...spread
});
// NOT inlined - computed property access
Platform.select({
['computed']: 'value'
});Prevents inlining when variables are used in assignment contexts:
// Will NOT be inlined - left-hand side of assignment
Platform.OS = 'modified';
// Will be inlined - right-hand side usage
const current = Platform.OS;Supports custom require function names for advanced bundling setups:
/**
* Custom require function support
* @param requireName - Custom require function name (default: 'require')
*/
// With requireName: 'customRequire'
// Detects: customRequire('react-native').Platform.OS
// Instead of: require('react-native').Platform.OS