React,
React-redux,
React-router,
Typescript,
antd,
Immutable,
...css
由於項目是一個SPA頁面,隨着項目的不斷迭代,入口文件逐漸增大(app.js),因此想減小入口文件的體積。html
方案一:react
webpack(require.ensure)+ react-routerwebpack
由於使用require.ensure須要足夠深的文件層級而且在對應文件須要定義index.ts文件做爲require.ensure入口,書寫起來比較麻煩,並且相對於工做量來講也是大有增長(具體的實現方案網上也比較多),故沒有選擇。web
方案二:npm
webpack(bundle-loader)+ react-router(lazyLoadComponent)json
優點:
一、不用過多的文件層級,保持文件的prue。
二、不用本身定義require.ensureredux
npm install bundle-loader --save-dev
export default { entry: entries, output: { path: __dirname, filename: `${dist}/js/[name].js`, // 這裏的dist是我定義的變量 publicPath: 'https://127.0.0.1/', // 這裏很關鍵=>單獨拿出來解釋 }, resolve: { extensions: [ '.js', '.jsx', '.ts', '.tsx', '.css', '.less', '.json', '.gif', '.html', '.png', '.webp', '.jpg', ], }, module: { rules: [ { test: /\.jsx?$/, loader: 'babel-loader', }, { test: /\.(tsx|ts)/, exclude: [ path.resolve(__dirname, "src/pages/") ], loader: 'ts-loader', }, { test: /src\\pages(\\.*).(tsx|ts)/, use: [ 'bundle-loader?lazy', 'ts-loader', ], }, { test: /\.(less)?$/, use: [ 'style-loader', 'css-loader', 'less-loader', ], // loader: 'style!css!less' }, { test: /.css$/, loader: extractCSS.extract({ fallbackLoader: 'style-loader', loader: 'css-loader', }), }, { test: /\.(jpg|png|gif|jpeg)?$/, loader: `url-loader?limit=20480&name=${dist}/images/[name].[hash:8].[ext]`, }, { test: /\.(eot|woff(2)?|ttf|svg)?(@.+)*$/, loader: `url-loader?limit=20480&name=${dist}/other/[name].[hash:8].[ext]`, } ], }, plugins: [ /** * DllReferencePlugin */ new DllReferencePlugin({ context: __dirname, manifest, }), /** * CommonsChunkPlugin */ new CommonsChunkPlugin({ name: 'common', filename: `${dist}/common.js`, minChunks: 2, chunks: entriesKey }), ], devtool: 'cheap-module-source-map', devServer: { port, https, contentBase: ROOTPATH, compress: true, inline: true, quiet: false, stats: { colors: true }, watchOptions: { aggregateTimeout: 300, poll: true, }, headers: { 'Access-Control-Allow-Origin': '*', }, } };
{ test: /src\\pages(\\.*).(tsx|ts)/, use: [ 'bundle-loader?lazy', 'ts-loader', ], },
test:是我寫的正則用來匹配我須要異步加載的文件,而且使用use中的bundle-loader->ts-loader。
(ps:在ts-loader編譯全局文件時,須要exclude[須要異步加載的路徑])api
// router.tsxbabel
import * as EffectiveCustomer from './pages/wuyoubao/customerManagement/effectiveCustomer'; function lazyLoadComponent(lazyModule) { return (location, cb) => { lazyModule(module => cb(null, module.default)); } } <Router history={history}> <Route path="/admindev/basic/common" {...this.props} {...routerProps}> <IndexRoute getComponent={lazyLoadComponent(Index)}/> <Route path="/api/wuyoubao/effective" getComponent={lazyLoadComponent(EffectiveCustomer)}/> </Route> </Router>
由於router.tsx中引用的tsx所有是被bundle-loader編譯的,而bundle-loader對ES6特性支持不是很好,好比:
ES6能夠解析
import { OrderInfoMessage } from './fundConfirmCom/fundConfirmInfoOrder'; console.log(OrderInfoMessage) // 能夠解析
可是這種方式bundle-loader解析不了,它所能解析的方式:
bundle-loader解析module
import { OrderInfoMessage } from './fundConfirmCom/fundConfirmInfoOrder'; console.log(OrderInfoMessage) // undefined // bundle-loader能夠識別的方式: import * as OrderInfoMessage from './fundConfirmCom/fundConfirmInfoOrder'; console.log(OrderInfoMessage) // 能夠解析