Writing require() at each point of use by hand gets tedious across a large codebase. When you build with the React Native CLI you don't have to, because require calls are inlined for you, both in your own code and in the packages under node_modules. The price is that modules stop running in the order the source suggests, which starts to matter as soon as one of them has side effects.

Key terms

  • inlineRequires: the Metro transform option that turns automatic require inlining on or off, or takes a blockList of files to leave alone.
  • getTransformOptions: the async function under transformer in a Metro config that returns the transform options, including inlineRequires.
  • nonInlinedRequires: a transform option listing module names whose require calls are never inlined, in any file.
  • RAM bundle: a random access module bundle, which stores each module as a separate string or file and parses it only when the module has to run.

What inlining moves

Inlining applies to require calls. import statements are left where they are. A top-level line such as const PdfViewer = require('./PdfViewer').default; still sits at the top of the file in your source, but it is evaluated lazily, at the place the component uses PdfViewer.

A new app gets this from its Metro config. The template's metro.config.js exports mergeConfig(getDefaultConfig(__dirname), config) with an empty config, and getDefaultConfig from @react-native/metro-config returns a getTransformOptions that sets inlineRequires: true. Frameworks can differ. Expo projects do not inline require calls by default, and you turn the optimization on there by setting inlineRequires: true in getTransformOptions.

A module that had to run early

Inlining changes the order in which modules are evaluated, and it can leave some modules never evaluated at all. That is usually harmless, because JavaScript modules are often written without side effects. A module that does have them, such as one that initializes a logging mechanism or patches a global API the rest of your code relies on, can produce unexpected behavior or even crashes.

Picture an index.js that loads a crash reporter with const crashReporter = require('./src/crashReporter'); and only calls crashReporter.capture() inside an error handler. If evaluating crashReporter also wraps the global fetch to tag requests, inlining moves that evaluation to the first error. Until then requests go out untagged, and in a session with no error the module never runs.

Opting files and modules out

You can exclude specific modules from inlining or turn inlining off. Turning it off takes one value in the transform options, inlineRequires: false. Excluding is finer grained and uses one of two options:

  • inlineRequires.blockList is an object keyed by resolved file paths, with entries such as [require.resolve('./index.js')]: true. require calls inside a listed file are not inlined. Calls everywhere else still are, unless they match nonInlinedRequires.
  • nonInlinedRequires is an array of module names. A require of a listed name is never inlined, whichever file it appears in.

A blockList entry is about where a require call is written. Listing index.js keeps every require in index.js at its position, which fixes the crash reporter. Listing src/crashReporter.js instead would only affect the require calls inside the reporter.

The Metro config example keeps the template's mergeConfig call and adds a getTransformOptions that blocks inlining in index.js and never inlines a tracing package. Its function also returns experimentalImportSupport: false, so the returned options match the defaults apart from the inlining settings.

RAM bundles belong to JavaScriptCore apps

RAM bundles work alongside the techniques above to limit how much JavaScript is parsed and loaded into memory. Each module is stored separately and parsed only when it has to run. They are not supported when the app uses Hermes. Hermes bytecode is not compatible with the RAM bundle format, and it gives the same or better performance in every use case. Since Hermes is the default, RAM bundles only come into play for an app that has opted out of it, and switching a Hermes app to them is not an option for startup or for any inlining problem.

Code Examples

javascript
const {getDefaultConfig, mergeConfig} = require('@react-native/metro-config');

const config = {
  transformer: {
    getTransformOptions: async () => ({
      transform: {
        experimentalImportSupport: false,
        inlineRequires: {
          blockList: {
            // require() calls written in index.js are not inlined
            [require.resolve('./index.js')]: true,
          },
        },
        nonInlinedRequires: [
          // require('@acme/request-tracing') is not inlined in any file
          '@acme/request-tracing',
        ],
      },
    }),
  },
};

module.exports = mergeConfig(getDefaultConfig(__dirname), config);
✓ Completed