2017-04-18 62 views
1

我向应用程序添加了一个config.json。如何创建配置文件以进行反应并防止webpack将其捆绑?

在webpack.config.js

我在我的应用所需的配置定义的配置

externals: { 
    'Config': JSON.stringify(production ? require('./config.prod.json') : require('./config.dev.json')) 
}, 

,并用它

var Config = require('Config'); 

然而,束的WebPack我的配置文件导入到index.js(我的WebPack输出文件),我不想要这个。 我想保持我的config.json从index.js分离为了实现这一点,我排除了我的config.json,但它没有奏效。

exclude: [/node_modules/, path.resolve(__dirname, 'config.dev.json'), path.resolve(__dirname, 'config.prod.json')] 

如果我错过了一些东西,你能帮我吗? 感谢

+0

你想单独加载config.json吗?或者你根本不需要config.json? – iamsaksham

+0

我需要config.json,但我不想捆绑到webpack输出文件 – Barny

回答

-1
+0

想了解为什么这是downvoted – thedude

+0

是啊我也是。它是一个正确的解决方案 – iamsaksham

+0

我不认为这是正确的解决方案,因为它仍然将配置捆绑到输出文件中。 OP希望保持配置文件完全独立于webpack输出(大概是这样配置可以很容易地更改,而不必重新整理应用程序) –

0

由于descibed通过@thedude可以使用的WebPack的代码分裂功能。 而不是简单地做import config from 'config.json'你可以使用一个非常酷的代码分割功能。

require.ensure([], function(require) { 
    let _config = require("config.json"); 
    this.setState({ 
    _configData: _config 
    }); 
}); 

,当你想使用的config数据,做到这一点通过检查状态

if(this.state._configData) { // this checks _configData is not undefined as well 
    // use the data of config json 
} 

当你使用的WebPack然后双束状文件将被创建,即bundle.js0.bundle.js将编译代码。这0.bundle.js有你的代码config.json文件。

相关问题