Trung BìnhReact Native iconReact Native

Metro bundler — khác Webpack ra sao?

Metro là bundler default của RN, viết bởi Meta. Tối ưu cho dev experience mobile (hot reload nhanh, source map cho native).

Khác Webpack:

MetroWebpack
TargetRN bundle (JS+assets cho mobile)Web bundle (JS+CSS+HTML)
EntryMột entry duy nhấtMulti-entry chunking
TransformBabel onlyBabel/SWC/esbuild loader
HMRFast Refresh tích hợpHMR plugin riêng
Tree shakingHạn chế (tốt hơn từ 0.73+)Mạnh, sideEffects flag
Code splittingKhông (single bundle)Built-in dynamic import
Source mapNative + JS combinedJS only

Config (metro.config.js):

js
const { getDefaultConfig } = require('expo/metro-config')

const config = getDefaultConfig(__dirname)
config.resolver.assetExts.push('lottie', 'glb') // custom asset
config.transformer.babelTransformerPath = require.resolve('react-native-svg-transformer')
module.exports = config

Cải tiến 2026:
- Metro 0.81+ tree-shaking ESM tốt hơn (giảm bundle size 10–20%).
- Lazy bundle loading — bundle splitting cho RN screens.
- Hermes-aware optimization (drop unused worklets).

Khi nào thay Metro:
- Hiện tại không có alternative production-ready.
- re.pack (project Callstack) port Webpack cho RN — dùng cho monorepo lớn cần code splitting cross-bundle, nhưng còn experimental.

Xem toàn bộ React Native cùng filter theo level & chủ đề con.

Mở danh sách React Native