webpack+react+antd腳手架優(yōu)化的方法
在之前的文章中了解了webpack v4的特性,現(xiàn)在將知識(shí)用于實(shí)踐,對(duì)之前寫(xiě)的一個(gè)react腳手架進(jìn)行打包性能的優(yōu)化。
優(yōu)化類目
- 樣式分離
- 第三方資源分離
- 區(qū)分開(kāi)發(fā)環(huán)境
- 熱更新
- 提取公共代碼
1. CSS分離
npm install extract-text-webpack-plugin -D
webpack.config.js
將css、less、sass文件單獨(dú)從打包文件中分離
+ let cssExtract = new ExtractTextWebpackPlugin({ + filename: 'css.css', + allChunks: true + }); + let sassExtract = new ExtractTextWebpackPlugin('sass.css') + let lessExtract = new ExtractTextWebpackPlugin('less.css')
在webpack.config.js中單獨(dú)添加規(guī)則,
- test:匹配處理文件的擴(kuò)展名的正則表達(dá)式
- include/exclude手動(dòng)指定必須處理的文件夾或屏蔽不需要處理的文件夾
{ test: /\.css$/, use: cssExtract.extract({ fallback: "style-loader", use: ['css-loader?minimize','postcss-loader'], publicPath: "/dist" }), include:path.join(__dirname,'./src'), exclude:/node_modules/ }, { test: /\.scss$/, use: sassExtract.extract({ fallback: "style-loader", use: ["css-loader?minimize","sass-loader"], publicPath: "/dist" }), include:path.join(__dirname,'./src'), exclude:/node_modules/ }, { test: /\.less$/, loader: lessExtract.extract({ use: ["css-loader?minimize", "less-loader"] }), include:path.join(__dirname,'./src'), exclude:/node_modules/ },
然后運(yùn)行webpack命令時(shí)報(bào)錯(cuò)
compilation.mainTemplate.applyPluginsWaterfall is not a function
Use Chunks.groupsIterable and filter by instanceof Entrypoint instead
研究得出結(jié)論:webpack升級(jí)到v4然而響應(yīng)的插件沒(méi)有升級(jí)的原故。
解決方法:安裝指定版本的依賴
"html-webpack-plugin": "^3.0.4" "extract-text-webpack-plugin": "^4.0.0-beta.0"
resolve
指定extension之后可以不用在require或是import的時(shí)候加文件擴(kuò)展名,會(huì)依次嘗試添加擴(kuò)展名進(jìn)行匹配
resolve: { //引入模塊的時(shí)候,可以不用擴(kuò)展名 extensions: [".js", ".less", ".json"], alias: {//別名 "bootstrap": "bootstrap/dist/css/bootstrap.css" } }
監(jiān)聽(tīng)文件修改
webpack模式下使用,webpack-dev-server模式不用,可以將watch改為false
watchOptions: { ignored: /node_modules/, aggregateTimeout: 300, //監(jiān)聽(tīng)到變化發(fā)生后等300ms再去執(zhí)行動(dòng)作,防止文件更新太快導(dǎo)致編譯頻率太高 poll: 1000 //通過(guò)不停的詢問(wèn)文件是否改變來(lái)判斷文件是否發(fā)生變化,默認(rèn)每秒詢問(wèn)1000次 }
提取公共代碼
optimization: { splitChunks: { cacheGroups: { commons: { chunks: "initial", minChunks: 2, maxInitialRequests: 5, // The default limit is too small to showcase the effect minSize: 0 // This is example is too small to create commons chunks }, vendor: { test: /node_modules/, chunks: "initial", name: "vendor", priority: 10, enforce: true } } } }
分離react react-dom ant公共代碼
方法一:externals
在頁(yè)面上引入第三方資源庫(kù),然后使用externals防止將某些 import 的包(package)打包到 bundle 中,而是在運(yùn)行時(shí)(runtime)再去從外部獲取這些擴(kuò)展依賴(external dependencies)。
<script src="https://cdn.bootcss.com/react/16.4.0-alpha.0911da3/cjs/react.production.min.js"></script> <script src="https://cdn.bootcss.com/react-dom/16.4.0-alpha.0911da3/cjs/react-dom-server.browser.production.min.js"></script> externals: { 'react': 'React', 'react-dom': 'ReactDOM', // 提出ant design的公共資源, }
方法二:DLL
DLL在上篇文章中寫(xiě)過(guò),但是打包后一直出現(xiàn)
后來(lái)才發(fā)現(xiàn)是頁(yè)面上沒(méi)有引入資源。。。。(我一直以為會(huì)webpack自動(dòng)生成在頁(yè)面上....)
在index.html文件中引入
<script src="./vendor/react.dll.js"></script>
分離成功!上代碼
webpack.base.js
var path = require('path'); var webpack = require('webpack'); var ExtractTextWebpackPlugin = require("extract-text-webpack-plugin"); const HtmlWebpackPlugin = require("html-webpack-plugin"); const CleanWebpackPlugin = require('clean-webpack-plugin'); const CopyWebpackPlugin = require('copy-webpack-plugin') let cssExtract = new ExtractTextWebpackPlugin({ filename: 'css.css', allChunks: true }); let sassExtract = new ExtractTextWebpackPlugin('sass.css') let lessExtract = new ExtractTextWebpackPlugin('less.css') module.exports = { entry:'./src/index.js', output: { path: path.resolve(__dirname, './dist'), filename: 'bundle.[hash:8].js', publicPath: '' }, resolve: { //引入模塊的時(shí)候,可以不用擴(kuò)展名 extensions: [".js", ".less", ".json"], alias: {//別名 "bootstrap": "bootstrap/dist/css/bootstrap.css" }, modules: [path.resolve(__dirname, 'node_modules')] }, /* externals: { 'react': 'React', 'react-dom': 'ReactDOM', // 提出ant design的公共資源 //'antd': 'antd', },*/ devtool: 'source-map', devServer: { contentBase:path.resolve(__dirname,'dist'), publicPath: '/', port: 8080, hot:true, compress:true, historyApiFallback: true, inline: true }, watch: false, //只有在開(kāi)啟監(jiān)聽(tīng)模式時(shí),watchOptions才有意義 watchOptions: { ignored: /node_modules/, aggregateTimeout: 300, //監(jiān)聽(tīng)到變化發(fā)生后等300ms再去執(zhí)行動(dòng)作,防止文件更新太快導(dǎo)致編譯頻率太高 poll: 1000 //通過(guò)不停的詢問(wèn)文件是否改變來(lái)判斷文件是否發(fā)生變化,默認(rèn)每秒詢問(wèn)1000次 }, optimization: { splitChunks: { cacheGroups: { commons: { chunks: "initial", minChunks: 2, maxInitialRequests: 5, // The default limit is too small to showcase the effect minSize: 0 // This is example is too small to create commons chunks }, vendor: { test: /node_modules/, chunks: "initial", name: "vendor", priority: 10, enforce: true } } } }, module: { rules:[ { test: /\.js$/, use: { loader:'babel-loader', options: { presets: ['env','es2015', 'react'], } }, include:path.join(__dirname,'./src'), exclude:/node_modules/ }, { test: /\.css$/, use: cssExtract.extract({ fallback: "style-loader", use: ['css-loader?minimize','postcss-loader'], publicPath: "/dist" }), include:path.join(__dirname,'./src'), exclude:/node_modules/ }, { test: /\.scss$/, use: sassExtract.extract({ fallback: "style-loader", use: ["css-loader?minimize","sass-loader"], publicPath: "/dist" }), include:path.join(__dirname,'./src'), exclude:/node_modules/ }, { test: /\.less$/, loader: lessExtract.extract({ use: ["css-loader?minimize", "less-loader"] }), include:path.join(__dirname,'./src'), exclude:/node_modules/ }, { test: /\.(html|htm)/, use: 'html-withimg-loader' }, { test: /\.(png|jpg|gif|svg|bmp|eot|woff|woff2|ttf)/, use: { loader:'url-loader', options:{ limit: 5 * 1024, //指定拷貝文件的輸出目錄 outputPath: 'images/' } } } ] }, plugins: [ //定義環(huán)境變量 new webpack.DefinePlugin({ __development__: JSON.stringify(process.env.NODE_ENV) }), new CleanWebpackPlugin(['dist']), cssExtract, lessExtract, sassExtract, new HtmlWebpackPlugin({ title: 'React Biolerplate by YuanYuan', template: './src/index.html', filename: `index.html`, hash: true }), new webpack.DllReferencePlugin({ manifest: path.join(__dirname, 'vendor', 'react.manifest.json') }), new CopyWebpackPlugin([{ from: path.join(__dirname,'vendor'),//靜態(tài)資源目錄源地址 to:'./vendor' //目標(biāo)地址,相對(duì)于output的path目錄 }]), /* new webpack.optimize.CommonsChunkPlugin({ name: 'common' // 指定公共 bundle 的名稱。 + })*/ new webpack.HotModuleReplacementPlugin(), // 熱替換插件 new webpack.NamedModulesPlugin() // 執(zhí)行熱替換時(shí)打印模塊名字 ] };
webpack.config.js
const path = require('path'); const webpack = require('webpack'); const merge = require('webpack-merge');//用來(lái)合并配置文件 const base = require('./webpack.base'); let other = ''; //console.log(process.env.NODE_ENV ) if (process.env.NODE_ENV == 'development') { other = require('./webpack.dev.config'); } else { other = require('./webpack.prod.config'); } //console.log(merge(base, other)); module.exports = merge(base, other); webpack.prod.config.js const path = require('path'); const webpack = require('webpack'); const UglifyJSPlugin = require('uglifyjs-webpack-plugin') module.exports = { output: { filename: 'bundle.min.js', }, plugins: [ new UglifyJSPlugin({sourceMap: true}) ] }
打包速度優(yōu)化,打包文件細(xì)化,改造成功~
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
react使用css module無(wú)法重寫(xiě)bootstrap樣式問(wèn)題及解決
這篇文章主要介紹了react使用css module無(wú)法重寫(xiě)bootstrap樣式問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11React使用context進(jìn)行跨級(jí)組件數(shù)據(jù)傳遞
這篇文章給大家介紹了React使用context進(jìn)行跨級(jí)組件數(shù)據(jù)傳遞的方法步驟,文中通過(guò)代碼示例給大家介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)React context組件數(shù)據(jù)傳遞有一定的幫助,感興趣的小伙伴跟著小編一起來(lái)學(xué)習(xí)吧2024-01-01React使用Canvas繪制大數(shù)據(jù)表格的實(shí)例代碼
之前一直想用Canvas做表格渲染的,最近發(fā)現(xiàn)了一個(gè)很不錯(cuò)的Canvas繪圖框架Leafer,api很友好就試著寫(xiě)了一下,文中有詳細(xì)的代碼示例供大家參考,感興趣的小伙伴可以自己動(dòng)手試試2023-09-09react實(shí)現(xiàn)動(dòng)態(tài)增減表單項(xiàng)的示例代碼
在做項(xiàng)目的時(shí)候,甲方給的信息有限,網(wǎng)頁(yè)的備案信息寫(xiě)成固定的,之后驗(yàn)收的時(shí)候,甲方要求把這個(gè)備案信息寫(xiě)成動(dòng)態(tài)的,可以自增減,下面通過(guò)實(shí)例代碼給大家介紹react實(shí)現(xiàn)動(dòng)態(tài)增減表單項(xiàng)的示例,感興趣的朋友跟隨小編一起看看吧2024-05-05React-Native之截圖組件react-native-view-shot的介紹與使用小結(jié)
這篇文章主要介紹了React-Native之截圖組件react-native-view-shot的介紹與使用小結(jié),需本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,要的朋友可以參考下2021-08-08React從插槽、路由、redux的詳細(xì)過(guò)程
React需要自己開(kāi)發(fā)支持插槽功能,原理:父組件組件中寫(xiě)入的HTML,可以傳入子組件的props中,這篇文章主要介紹了React從插槽、路由、redux的詳細(xì)過(guò)程,需要的朋友可以參考下2022-10-10react-native中路由頁(yè)面的跳轉(zhuǎn)與傳參的實(shí)例詳解
這篇文章主要介紹了react-native中路由頁(yè)面的跳轉(zhuǎn)與傳參,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08使用useMutation和React Query發(fā)布數(shù)據(jù)demo
這篇文章主要為大家介紹了使用useMutation和React Query發(fā)布數(shù)據(jù)demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12