Further split the React Native code based on Metro build to improve performance, providing `Dll` and `Dynamic Imports` features

Overview

React-Native Code Splitting

Further split the React Native code based on Metro build to improve performance, providing Dll and Dynamic Imports features

Features & Solve issue

  • Dll The split 820KB + common code (react、react-native) can be built into the App, similar to the Webpack DLLPlugin
  • Dynamic Imports issue

Compatibility with Metro versions

  • dependencies react-native -> @react-native-community/cli -> metro

  • metro-code-split -> metro

    metro version metro-code-split version
    0.64.0 - 0.66.0 0.1.x

How to use it?

  1. Install the package that matches the Metro version
  npm i metro-code-split -D | yarn add metro-code-split -D
  1. Add the scripts in package.json
  "scripts": {
    "start": "mcs-scripts start -p 8081",
    "build:dllJson": "mcs-scripts build -t dllJson -od public/dll",
    "build:dll": "mcs-scripts build -t dll -od public/dll",
    "build": "mcs-scripts build -t busine -e index.js"
  }
  • More command details
  npx mcs-scripts --help
  1. Modify the metro.config.js
  const Mcs = require('metro-code-split')

  const mcs = new Mcs({
    output: {
      publicPath: 'https://a.cdn.com/a-rn-project',
    },
    dll: {
      entry: ['react-native', 'react'], // which three - party library into dll
      referenceDir: './public/dll', // the JSON address to reference for the build DLL file, also the npm run build:dllJson output directory
    },
    dynamicImports: {}, // DynamicImports can also be set to false to disable this feature if it is not required
  })

  const busineConfig = {
    transformer: {
      getTransformOptions: async () => ({
        transform: {
          experimentalImportSupport: false,
          inlineRequires: true,
        },
      }),
    },
  }

  module.exports = process.env.NODE_ENV === 'production' ? mcs.mergeTo(busineConfig) : busineConfig
  1. Execute the command
  npm run build:dllJson // build the reference json file for the Dll
  npm run build:dll // build the Dll file (the generated Dll file is usually built into the APP)
  npm run build // build busine code

Custom logic

  • Mcs options provides plugins. You can write plugins in the Mcs as if you were writing Webpack plugins

    hooks type parameter
    beforeInit SyncHook ['bundleOutputInfos']
    beforeCheck SyncHook ['freezeFields']
    afterCheck SyncHook ['freezeFields']
    beforeCustomSerializer SyncBailHook ['entryPoint', 'prepend', 'graph', 'bundleOptions']
    beforeOutputChunk SyncHook ['chunkInfo']
    afterCustomSerializer SyncHook ['bundle']

Attention to issue

  • This package is only used in production environments! (There are currently no plans to be compatible with development)

  • The add scripts are equivalent to The following (The main purpose is to optimize the build:dllJson build:dll long instructions, if you intend to provide commands using React-Native, be sure to add NODE_ENV=xxx)

      "scripts": {
        "start": "NODE_ENV=production react-native start --port 8081",
        "build:dllJson": "NODE_ENV=production react-native bundle --platform ios --entry-file node_modules/.cache/metro-code-split/dll-entry.js --bundle-output public/dll/_dll.ios.json --dev false & NODE_ENV=production react-native bundle --platform android --entry-file node_modules/.cache/metro-code-split/dll-entry.js --bundle-output public/dll/_dll.android.json --dev false",
        "build:dll": "NODE_ENV=production react-native bundle --platform ios --entry-file node_modules/.cache/metro-code-split/dll-entry.js --bundle-output public/dll/_dll.ios.bundle --dev false & NODE_ENV=production react-native bundle --platform android --entry-file node_modules/.cache/metro-code-split/dll-entry.js --bundle-output public/dll/_dll.android.bundle --dev false",
        "build": "NODE_ENV=production react-native bundle --platform ios --entry-file index.js --bundle-output dist/buz.ios.bundle --dev false & NODE_ENV=production react-native bundle --platform android --entry-file index.js --bundle-output dist/buz.android.bundle --dev false"
      }

Rendering

rendering

Example

TODO

You might also like...

Skia based squircle for React Native. draws with Figma squirce style

Skia based squircle for React Native. draws with Figma squirce style

react-native-squircle-skia Disclaimer @shopify/react-native-skia is alpha release. Use with caution. This library dependes on figma's blog article. Wh

Dec 16, 2022

📋 React Hooks for forms validation (Web + React Native)

📋 React Hooks for forms validation (Web + React Native)

English | 繁中 | 简中 | 日本語 | 한국어 | Français | Italiano | Português | Español | Русский | Deutsch | Türkçe Features Built with performance and DX in mind

Dec 29, 2022

An open source application to create, manage and embed contact forms on static/dynamic sites with no code

An open source application to create, manage and embed contact forms on static/dynamic sites with no code

Formify is an open source application that lets you create, manage and embed contact forms on any site without writing code. This project is done as a

Dec 26, 2022

React features to enhance using Rollbar.js in React Applications

Rollbar React SDK React features to enhance using Rollbar.js in React Applications. This SDK provides a wrapper around the base Rollbar.js SDK in orde

Jan 3, 2023

Recipe providing mobile app, User selects ingredients in pantry and is then provided recipes for those ingredients. App contains a signup/login, meal planner and grocery list pages.

Recipog Student Information Name Connor de Bruyn Username Destiro Assignment SWEN325 A2 Description “Recipog” is a recipe providing app that allows th

Dec 26, 2021

HTML CSS & React - Client side dynamic e-commerce website (stripe integrated)

HTML CSS & React - Client side dynamic e-commerce website (stripe integrated)

Furniture E-Commerce Project Description React front-end full operating dynamic and responsive E-Commerce shop including payment connection (stripe) B

Dec 27, 2022

The CryptoVerse is a Cryptocurrency web application developed using Reactjs for providing the latest updates, value statistics, market cap, supply and news regarding the Cryptocurrency market.

The CryptoVerse is a Cryptocurrency web application developed using Reactjs for providing the latest updates, value statistics, market cap, supply and news regarding the Cryptocurrency market.

CryptoVerse - A Crptocurrency Web Application Getting Started with Create React App This project was bootstrapped with Create React App. Available Scr

Oct 26, 2022

A npm package to increase the developer experience and consistency by providing a set of hooks that can be opted-in the development lifecycle.

@jeliasson/husky-hooks This npm package aims to increase the developer experience and consistency by providing a set of hooks that can be opted-in the

Dec 6, 2022

Utilities library built on top of Next.js providing feature extensions and helpers for common patterns

nextjs-utilites This library provides many helpful utilities for use in Next.js projects. Prerequisites This project requires NodeJS (version 8 or lat

Sep 7, 2022
Comments
  • Dynamic import with template strings does not work.

    Dynamic import with template strings does not work.

    Does this package give the possibility to make it work? In the documentation, I only found an example with components being lazy-loaded. The following code will throw an error.

    App.tsx

    import React from 'react';
    import { View, Text } from 'react-native';
    
    const names = {
      container: 'Container'
    }
    
    const Container = React.lazy(() => import(`./src/components/${names.container}`))
    
    export default function App() {
      return (
        <View>
          <React.Suspense fallback={<Text>Loading...</Text>}>
            <Container />
          </React.Suspense>
        </View>
      );
    }
    

    Container.tsx

    import { View, Text } from "react-native";
    
    export const Container = () => (
      <View>
        <Text>This is a container.</Text>
      </View>
    )
    
    opened by Willaiem 2
  • demo run android failure

    demo run android failure

    yarn android

    > Task :app:processDebugResources
    > Task :react-native-community_masked-view:generateDebugBuildConfig UP-TO-DATE
    > Task :react-native-community_masked-view:javaPreCompileDebug UP-TO-DATE
    > Task :react-native-community_masked-view:compileDebugJavaWithJavac
    > Task :react-native-gesture-handler:generateDebugBuildConfig UP-TO-DATE
    > Task :react-native-gesture-handler:javaPreCompileDebug UP-TO-DATE
    > Task :react-native-community_masked-view:bundleLibCompileToJarDebug
    > Task :react-native-gesture-handler:compileDebugJavaWithJavac UP-TO-DATE
    > Task :react-native-gesture-handler:bundleLibCompileToJarDebug UP-TO-DATE
    > Task :react-native-reanimated:generateDebugBuildConfig UP-TO-DATE
    > Task :react-native-reanimated:javaPreCompileDebug UP-TO-DATE
    > Task :react-native-reanimated:packageNdkLibs NO-SOURCE
    
    > Task :react-native-reanimated:compileDebugJavaWithJavac FAILED
    75 actionable tasks: 5 executed, 70 up-to-date
    ~/Workspace/metro-code-split/Example/node_modules/react-native-reanimated/android/src/main/java/com/swmansion/reanimated/layoutReanimation/ReanimatedUIImplementation.java:13: 错误: 找不到符号
          ViewManagerResolver viewManagerResolver,
          ^
      符号:   类 ViewManagerResolver
      位置: 类 ReanimatedUIImplementation
    ~/Workspace/metro-code-split/Example/node_modules/react-native-reanimated/android/src/main/java/com/swmansion/reanimated/layoutReanimation/ReanimatedUIImplementation.java:11: 错误: 递归构造器调用
      public ReanimatedUIImplementation(
             ^
    注: 某些输入文件使用或覆盖了已过时的 API。
    注: 有关详细信息, 请使用 -Xlint:deprecation 重新编译。
    注: 某些输入文件使用了未经检查或不安全的操作。
    注: 有关详细信息, 请使用 -Xlint:unchecked 重新编译。
    2 个错误
    
    FAILURE: Build failed with an exception.
    
    * What went wrong:
    Execution failed for task ':react-native-reanimated:compileDebugJavaWithJavac'.
    > Compilation failed; see the compiler error output for details.
    
    * Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
    
    
    opened by krmao 0
Releases(v0.1.7)
Owner
Wuba
Wuba Open Source
Wuba
Metro 4 Components Library

Impressive component library for expressive web development! Build responsive projects on the web with the first front-end component library in Metro Style. And now there are even more opportunities every day!

Serhii Pimenov 7k Dec 30, 2022
A react native component that lets you build a dynamic expandable chips list.

React Native Expandable Chips List A react native component that lets you build a dynamic expandable chips list. Installation Run npm install react-na

Daniel Cocos 13 Sep 23, 2022
null 136 Dec 30, 2022
🎉 toastify-react-native allows you to add notifications to your react-native app (ios, android) with ease. No more nonsense!

toastify-react-native ?? toastify-react-native allows you to add notifications to your react-native app (ios, android) with ease. No more nonsense! De

Zahid Ali 29 Oct 11, 2022
Discover people willing to split the cost for LeetCode Premium accounts with you @BITS-ACM.

Litecode litecode.bitsacm.in About ~ 2 months before every internship season, every single CS student in the world has felt the pain of the sinking re

BITS-ACM 26 Aug 9, 2022
This compress library was made with Brotli and Gzip help, for React users who want to make website more performance and reduce JS bundle code

React-compress This compress library was made with Brotli and Gzip help, for React users who want to make website more performance and reduce JS bundl

Koma Human 30 Jan 6, 2023
Recoil is an experimental state management library for React apps. It provides several capabilities that are difficult to achieve with React alone, while being compatible with the newest features of React.

Recoil · Recoil is an experimental set of utilities for state management with React. Please see the website: https://recoiljs.org Installation The Rec

Facebook Experimental 18.2k Jan 8, 2023
Boilerplate to build Cross-Platform Apps with Expo and React Native

Expo and React Native Boilerplate Boilerplate to build Cross-Platform Apps with Expo and React Native What are you going to find in this boilerplate E

José Ferrer 26 Apr 29, 2022
🏁 High performance subscription-based form state management for React

You build great forms, but do you know HOW users use your forms? Find out with Form Nerd! Professional analytics from the creator of React Final Form.

Final Form 7.2k Jan 7, 2023
A personal project, made with React, React Native, Typescript and some of the most used front-end libraries.

A personal project, made with React, React Native, Typescript and some of the most used front-end libraries.

Alvaro Junior 1 Jul 23, 2022