How do I get AWS Amplify to Build in production mode using Gulp process.env.NODE_ENV?

Viewed 262

I am using AWS Amplify with the following configuration:

  1. https://dev.example.com/ - AWS Amplify - branch = dev
  2. https://example.com/ - AWS Amplify - branch = master

In gulpfile.js I have rules applied for production (minification etc using)

const isProd = process.env.NODE_ENV === 'prod'

This is my amplify.yaml

version: 1
frontend:
  phases:
    # IMPORTANT - Please verify your build commands
    build: 
      # commands: []
      commands:
        npm run build:$BUILD_ENV
  artifacts:
    # IMPORTANT - Please verify your build output directory
    baseDirectory: /docs
    files:
      - '**/*'
  cache:
    paths: []

I tried applying prod to ONLY master. I only want the prod mode to be applied and ran for the master. When I run the build.. dev branches shows unminified HTML and so does production. It is working and building but the prod mode is not being reflected in production so it is not executing my build prod command.

I searched online and also applied Environment variables AND followed this documentation which shows the build process "https://docs.aws.amazon.com/amplify/latest/userguide/build-settings.html" as follows:

enter image description here

Still is not working.

1 Answers

I ran into a similar problem. I'm not sure this is the most elegant solution:

    build:
      commands:
        - if [ "${AWS_BRANCH}" = "master" ]; then echo "master branch"; npm run build; fi
        - if [ "${AWS_BRANCH}" = "dev" ]; then echo "dev branch"; npm run build -- --mode dev; fi

This worked for me using, though my stack is different than yours. It might lead you in the right direction, or help others.

Related