简体   繁体   English

如何使用 Gulp process.env.NODE_ENV 让 AWS Amplify 在生产模式下构建?

[英]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:我正在使用具有以下配置的 AWS Amplify:

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

In gulpfile.js I have rules applied for production (minification etc using)gulpfile.js我有适用于生产的规则(缩小等使用)

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

This is my amplify.yaml这是我的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.我尝试将 prod 应用于 ONLY 大师。 I only want the prod mode to be applied and ran for the master.我只希望为 master 应用和运行 prod 模式。 When I run the build.. dev branches shows unminified HTML and so does production.当我运行构建时.. dev 分支显示未缩小的 HTML,生产也是如此。 It is working and building but the prod mode is not being reflected in production so it is not executing my build prod command.它正在工作和构建,但 prod 模式没有反映在生产中,因此它没有执行我的 build prod 命令。

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:我在网上搜索并应用了环境变量并遵循了这个文档,该文档显示了构建过程“https://docs.aws.amazon.com/amplify/latest/userguide/build-settings.html”,如下所示:

在此处输入图像描述

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.它可能会引导你走向正确的方向,或帮助他人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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