如何在 ReasonReact 应用程序中使用环境变量?

How do you use environment variables in a ReasonReact app?

我尝试将 .env 文件添加到目录的根目录,并尝试使用

访问内容

[@bs.val] external graphqlUrl : string = "process.env.GRAPHQL_URL";

但是当我 Js.log(graphqlUrl); 它是 undefined!

.env 文件:

GRAPHQL_URL=https://api.example.com

如何访问它?

谢谢!

我跟随 Rem Lampa's suggestion and installed dotenv-webpack 先生完成了我的项目。然后我按照 dotenv-webpack 文档中的说明进行操作。

webpack.config.js 文件现在看起来像这样:

const path = require('path');
const outputDir = path.join(__dirname, "build/");

const isProd = process.env.NODE_ENV === 'production';

const Dotenv = require('dotenv-webpack');

module.exports = {
  entry: './src/Index.bs.js',
  mode: isProd ? 'production' : 'development',
  output: {
    path: outputDir,
    publicPath: outputDir,
    filename: 'Index.js',
  },
  plugins: [
    new Dotenv()
  ]
};