简体   繁体   中英

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

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:

在此处输入图像描述

Still is not working.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM