【问题标题】:Reading config files in create react app setup in craco在 craco 中读取配置文件创建反应应用程序设置
【发布时间】:2021-11-27 03:45:05
【问题描述】:

我希望阅读我的配置文件,这取决于我的环境,而不是弹出我的 create-react-app 的 webpack。

我找到了一些StackOverflow references 来更改Webpack config 以读取配置文件取决于环境。

externals: {
  'Config': JSON.stringify(process.env.NODE_ENV === 'production' ? require('./config.prod.json') : require('./config.dev.json'))
}

我在src/config/globals/ 目录下有以下文件。

prod.js
dev.js
test.js

我只是尝试根据环境选择正确的配置文件。 我的 craco 配置文件内容,

module.exports = {
    externals: {
      config: process.env.NODE_ENV === 'production' ? require('./src/config/globals/prod.js') : require('./src/config/globals/dev.js'),
    },
  }

但是当我尝试在我的组件中导入配置文件时,我收到了Module not found 错误。

Module not found: Can't resolve 'config' 

我的导入声明,

import GlobalConfig from 'config'

配置文件对象的导出方式是,

module.exports = GlobalConfig;

如何读取配置文件取决于 craco config 设置的环境?

【问题讨论】:

    标签: webpack environment-variables config create-react-app craco


    【解决方案1】:

    以下 craco.config.js 对我有用:

    module.exports = {
        webpack: {
            configure: (webpackConfig) => {
                webpackConfig.externals = {
                    'Config': JSON.stringify(process.env.NODE_ENV === 'production' ? require('./config.prod.json') : require('./conf/config.dev.json'))
                };
                return webpackConfig;
            },
        }
    };
    

    【讨论】:

      猜你喜欢
      • 2018-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多