我希望在浏览器中使用https://github.com/flatiron/nconf。我曾尝试将其与browserify一起使用,但由于nconf在需要扫描目录中的配置时会调用fs.readdirSync,因此在浏览器中失败。
// config.js
'use strict';
var nconf = require('nconf'); // this triggers the fs.readdirSync
var globalConfig = { my: { global: 1 }};
nconf.use('global', { type: 'literal', store: globalConfig });
var envConfig = { my: { env: 2 }};
nconf.use('env', { type: 'literal', store: envConfig });
module.exports = nconf;有没有可能使用某种browserify转换(我没有看到一种方法让它在nconf中强制使用BRFS )或使用nconf(或其他类似的库)来管理客户端配置?
如果不是nconf本身,那么只需要一些东西,让我可以做类似下面的事情:
config.load('user-settings', { my : { user: 1 } });
config.load('global-settings', { my: { global: 2 } } );
config.get('my.user'); // 1
config.get('my.global'); // 2
config.unload('global-settings');
config.get('my.global'); // undefined发布于 2020-06-03 05:54:54
最近我自己也遇到了这个问题。我决定只把我自己的配置文件放在一起,而不是放入另一个库。这是我最终得到的结论:
/*
This file determines which environment we're in, and provides the app with the appropriate config
*/
export const defaults = {
searchURI: 'http://www.google.com/',
anotherURI: 'https://stackoverflow.com/',
};
export const dev = {
searchURI: 'https://www.bing.com/',
};
export const test = {
searchURI: 'https://www.yahoo.com/',
};
export const prod = {
searchURI: 'https://duckduckgo.com/',
};
const config = () => {
switch (process.env.YOUR_ENV_NAME) {
case 'dev':
return dev;
case 'test':
return test;
default:
return prod;
}
};
export default {
...defaults,
...config(),
};使用此模式,您可以像nconf一样导入配置
import config from './path/to/above/file.js';
const { searchURI, anotherURI } = config;
// OR
const searchURI = config.searchURI;
const anotherURI = config.anotherURI;https://stackoverflow.com/questions/27297709
复制相似问题