How to prevent node from running out of memory when bundling js for React Native

前端 未结 5 1927
误落风尘
误落风尘 2021-01-02 06:01

When bundling js for React Native using ClojureScript I got the following error. It seems that node runs out of memory when bundling the javascript bundle. This is probably

相关标签:
5条回答
  • 2021-01-02 06:04

    For android, this can also be set in android/app/build.gradle by adding:

    project.ext.react = [
       // override which node gets called and with what additional arguments
       nodeExecutableAndArgs: ["node", "--max-old-space-size=4096"]
    ]
    

    note that this must be added above the following line:

    apply from: "../../node_modules/react-native/react.gradle"
    
    0 讨论(0)
  • 2021-01-02 06:07

    For iOS, with react-native 0.59.0 works by editing on ios/YourProjectName.xcodeproj/project.pbxproj

    this line

    shellScript = "export NODE_BINARY=node\n../node_modules/react-native/packager/react-native-xcode.sh";
    

    with

    shellScript = "export NODE_OPTIONS=--max_old_space_size=4096\n../node_modules/react-native/scripts/react-native-xcode.sh";
    
    0 讨论(0)
  • 2021-01-02 06:12

    The solution for iOS is to edit the following file: ios/YourProjectName.xcodeproj/project.pbxproj and change the following line (~600)

    shellScript = "export NODE_BINARY=node\n../node_modules/react-native/packager/react-native-xcode.sh";
    

    to

    shellScript = "export NODE_BINARY='node --max_old_space_size=4092'\n../node_modules/react-native/packager/react-native-xcode.sh";
    
    0 讨论(0)
  • 2021-01-02 06:12

    Another workaround is to disable optimizations by setting --dev true for production builds. This has performance drawbacks, but in my experience they are acceptable. Dev mode also enables a number of runtime checks. You can disable them by changing the DEV constant at the top of the bundle output, like so:

    #!/usr/bin/env python
    
    # Patch jsbundle to set __DEV__ to false
    
    import sys, re
    
    print(re.sub(r'__DEV__\s*=\s*true;', "__DEV__=false;",
            sys.stdin.read()))
    
    0 讨论(0)
  • 2021-01-02 06:22

    I have found way to generate Signed APK in re-natal project. For this we must edit file in node_modules/react-native/react.gradle

    Change 84-th row in this file. From

    commandLine(*nodeExecutableAndArgs, "node_modules/react-native/local-cli/cli.js", "bundle", "--platform", "android", "--dev", "${devEnabled}",
        "--reset-cache", "--entry-file", entryFile, "--bundle-output", jsBundleFile, "--assets-dest", resourcesDir, *extraPackagerArgs)
    

    To this

    commandLine(*nodeExecutableAndArgs, "--max-old-space-size=4096", "node_modules/react-native/local-cli/cli.js", "bundle", "--platform", "android", "--dev", "${devEnabled}",
        "--reset-cache", "--entry-file", entryFile, "--bundle-output", jsBundleFile, "--assets-dest", resourcesDir, *extraPackagerArgs)
    

    To create production build you must use

    lein prod-build
    

    After compiling of ClojureScript use this commands:

    cd android && ./gradlew assembleRelease
    

    The generated APK can be found under android/app/build/outputs/apk/app-release.apk, and is ready to be distributed.

    0 讨论(0)
提交回复
热议问题