在.serverless.yml中的next.config.js中使用环境变量

时间:2019-09-30 06:15:48

标签: next.js next serverless

我正在使用Next.js Framework部署Serverless

并且,我尝试划分部署模式developmentstageproduction

为此,我配置了serverless.yml

provider:
  name: aws
  runtime: nodejs8.10
  region: ${file(./sls-variables.yml):region}
  stage: ${opt:stage, 'development'}
  environment:
    NODE_ENV: ${opt:stage, 'development'}
    NODE_SLS: true

然后我编辑了next.config.js

const path = require("path");
// next.config.js
const withSass = require("@zeit/next-sass");
module.exports = withSass({
  /* config options here */
  assertPrefix: "SOMETHING",
  sassLoaderOptions: {
    includePaths: ["absolute/path/a", "absolute/path/b"]
  },
  webpack(config, options) {
    config.module.rules.push({
      test: /\.(eot|woff|woff2|ttf|svg|png|jpg|gif)$/,
      use: {
        loader: 'url-loader',
        options: {
          limit: 100000,
          name: '[name].[ext]'
        }
      }
    })
    config.resolve.alias["components"] = path.join(__dirname, "components");
    config.resolve.alias["utils"] = path.join(__dirname, "utils");
    return config;
  },
  env: {
    'iamport': {
      'merchant_id': process.env.NODE_ENV === "production" ? "PROD_ID" : "DEV_ID"
    },
    'baseUrl': process.env.NODE_ENV === "production" ? "PROD_URL" : "DEV_URL"
  }
});

并且我在process.env.NODE_ENV中管理了app.js,我只能看到“生产”

我的问题是如何触摸process.env.NODE_ENV或设置的其他内容。

0 个答案:

没有答案