简体   繁体   English

从React / Babel / Webpack中的外部文件加载配置

[英]Loading configs from an external file in React / Babel / Webpack

I have defined a config module that merges defaultConfig which I've defined within the module, along with loaded configs config = JSON.parse(fs.readFileSync(path.join(process.cwd(), argv.config))) . 我定义了一个配置模块,该模块合并了我在模块中定义的defaultConfig以及加载的配置config = JSON.parse(fs.readFileSync(path.join(process.cwd(), argv.config))) So basically I merge my configs like so config = defaultsDeep(config, defaultConfig) then export exports = module.exports = config . 所以基本上我像config = defaultsDeep(config, defaultConfig)那样合并我的config = defaultsDeep(config, defaultConfig)然后导出export exports = module.exports = config

The question that I have is, how can I access configurations at the component level? 我的问题是,如何在组件级别访问配置? I'm using React / Webpack / Babel. 我正在使用React / Webpack / Babel。 What I'm experiencing at the moment is that even though in my test environment I pass the config file as an arg "nodejs\\node.exe" server --config ../conf/sqrweb.config.json , when I define configs intended for React components those aren't overriding the default configs, instead what's defined in the defaultConfig are being loaded. 我现在遇到的是,即使在测试环境中,我在定义配置时将配置文件作为arg "nodejs\\node.exe" server --config ../conf/sqrweb.config.json适用于React组件,这些组件不会覆盖默认配置,而是会加载defaultConfig中定义的内容。

What am I missing here?! 我在这里想念什么?

Webpack is run in Node.js, so you can read system variable in its config file. Webpack在Node.js中运行,因此您可以在其配置文件中读取系统变量。 And there is definitely a way to pass that value to bundle file 绝对有一种方法可以将该值传递到捆绑文件

Not sure what you exactly want. 不知道你到底想要什么。 But if you want to pass a variable from webpack config file to React component, look this https://github.com/AngularClass/angular-starter/issues/386 但是如果要将变量从webpack配置文件传递给React组件,请查看此https://github.com/AngularClass/angular-starter/issues/386

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

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