iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
😎

Dynamically generating aws-exports.js in EAS Build environment

に公開

When creating a React Native app incorporating Amplify Libraries with Expo and trying to build it with EAS Build, an error occurs.

> Task :app:bundleReleaseJsAndAssets
warning: the transform cache was reset.
                    Welcome to Metro!
              Fast - Scalable - Integrated
[stderr] error Unable to resolve module ./src/aws-exports from /root/workingdir/build/App.js:
[stderr] None of these files exist:
[stderr]   * src/aws-exports(.native|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx|.android.js|.native.js|.js|.android.jsx|.native.jsx|.jsx|.android.json|.native.json|.json)
[stderr]   * src/aws-exports/index(.native|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx|.android.js|.native.js|.js|.android.jsx|.native.jsx|.jsx|.android.json|.native.json|.json)

Since aws-exports.js is included in .gitignore, it is not checked out to the EAS Build environment and is missing during the build.

This can be resolved by setting aws-exports.js as an environment variable using eas secret and writing it out during the build.

eas secret:create --name=AWS_EXPORTS_JSON_BASE64 --value=$(base64 -i ./src/aws-exports.js)

Encoding to Base64 seems to be a common practice in infrastructure-related tools to handle strings with line breaks properly within environment variables.

Decode this in the build environment and write it out to a file. Define the build pre-install hook eas-build-pre-install in package.json.

"scripts": {
    "eas-build-pre-install": "echo $AWS_EXPORTS_JSON_BASE64 | base64 -d > ./src/aws-exports.js",
    ...
  },

The base64 command differs slightly between the macOS version and the Linux GNU coreutils version; in the EAS environment, options like base64 -o ./src/aws-exports.js cannot be used.

If you update the Amplify project configuration, overwrite the secret value as well.

eas secret:create --name=AWS_EXPORTS_JSON_BASE64 --value=$(base64 -i ./src/aws-exports.js) --force

Discussion