redux
的入口模塊就是src/index.js
。這個文件的代碼十分簡單。主要就作兩件事:javascript
production
環境下壓縮代碼,給予警告。下面是模塊的源碼(只包含本身對代碼的理解,並不包含原註釋。)java
// 引入createStore模塊,這個模塊就是`createStore`方法的實現 import createStore from './createStore' // 引入combineReducers模塊,這個模塊就是`combineReducers`方法的實現 import combineReducers from './combineReducers' // 引入bindActionCreators模塊,這個模塊就是`bindActionCreators`方法的實現 import bindActionCreators from './bindActionCreators' // 引入applyMiddleware模塊,這個模塊就是`applyMiddleware`方法的實現 import applyMiddleware from './applyMiddleware' // 引入compose模塊,這個模塊就是`compose`方法的實現 import compose from './compose' // warning在支持console對象的瀏覽器中能夠看做是對console.error方法的一個便捷方法,不然就是一個Error的實例對象。 import warning from './utils/warning' // 這個函數惟一的做用就是:判斷代碼是否是處於壓縮模式下,若是代碼處於壓縮模式下,函數的名稱會改變,即 // isCrushed.name === 'isCrushed' 爲false function isCrushed() {} // 若是在非production模式下壓縮咱們的js代碼,會拋出warning。 // if的判斷條件其實就是告訴咱們,在production的模式下,必定要設置process.env.NODE_ENV爲production if ( process.env.NODE_ENV !== 'production' && typeof isCrushed.name === 'string' && isCrushed.name !== 'isCrushed' ) { warning( 'You are currently using minified code outside of NODE_ENV === \'production\'. ' + 'This means that you are running a slower development build of Redux. ' + 'You can use loose-envify (https://github.com/zertosh/loose-envify) for browserify ' + 'or DefinePlugin for webpack (http://stackoverflow.com/questions/30030031) ' + 'to ensure you have the correct code for your production build.' ) } // 這就是咱們一般使用的redux的幾個經常使用的方法 export { createStore, combineReducers, bindActionCreators, applyMiddleware, compose }
怎麼設置環境變量NODE_ENV
的值呢?對於咱們使用webpack
進行開發的同窗來講,咱們能夠經過以下方式設置。webpack
plugins: [ new webpack.DefinePlugin({ 'process.env': { NODE_ENV: JSON.stringify(process.env.NODE_ENV) } }) ]
其中,JSON.stringify(process.env.NODE_ENV)
是直接獲取的咱們bash
終端而言的。因此,在運行咱們的項目以前,咱們必須確保制定了這個環境變量(注:NODE_ENV
並非不可變的,你也能夠指定其餘的名字,可是須要和本身項目中的獲取保持一致)git
假設咱們項目的啓動腳本是
yarn run start
github
Mac or Linuxweb
能夠經過下面兩種方式設置:redux
export NODE_ENV=production yarn run start # 或者 NODE_ENV=production yarn run start
Windows瀏覽器
能夠經過下面這種方式指定:bash
set NODE_ENV=production yarn run start
這就是對redux源碼主模塊的一個總體解讀,水平有限,歡迎拍磚。後續的源碼解讀和測試例子能夠關注:redux源碼解讀倉庫app