2017-04-18 63 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文件。

相關問題