1.1 路由組件按需加載css
const router = [ { path: '/index', component: resolve => require.ensure([], () => resolve(require('@/components/index'))) }, { path: '/about', component: resolve => require.ensure([], () => resolve(require('@/components/about'))) } ]
1.2 第三方組件和插件。按需加載需引入第三方組件html
// 引入所有組件 import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' Vue.use(ElementUI) // 按需引入組件 import { Button } from 'element-ui' Vue.component(Button.name, Button)
1.3 對於一些插件,若是隻是在個別組件中用的到,也能夠不要在 main.js 裏面引入,而是在組件中按需引入前端
// 在main.js引入 import Vue from vue import Vuelidate from 'vuelidate' Vue.use(Vuelidate) // 按組件按需引入 import { Vuelidate } from 'vuelidate'
module: { rules: [ { test: /\.js$/, loader: 'babel-loader?cacheDirectory', include: [resolve('src')] } ] }
resolve: { extensions: ['.js', '.vue', '.json'], alias: { 'vue$': 'vue/dist/vue.esm.js', '@': resolve('src'), } },
兩種方法使用以下:vue
plugins: [ new UglifyJsPlugin({ uglifyOptions: { compress: { warnings: false } }, sourceMap: true, parallel: true }), new ParallelUglifyPlugin({ //緩存壓縮後的結果,下次遇到同樣的輸入時直接從緩存中獲取壓縮後的結果並返回, //cacheDir 用於配置緩存存放的目錄路徑。 cacheDir: '.cache/', sourceMap: true, uglifyJS: { output: { comments: false }, compress: { warnings: false } } }) ]
webpack3 使用 CommonsChunkPlugin 的實現:node
plugins: [ new webpack.optimize.CommonsChunkPlugin({ name: 'vendor', minChunks: function(module, count) { console.log(module.resource, `引用次數${count}`) //"有正在處理文件" + "這個文件是 .js 後綴" + "這個文件是在 node_modules 中" return module.resource && /\.js$/.test(module.resource) && module.resource.indexOf(path.join(__dirname, './node_modules')) === 0 } }), new webpack.optimize.CommonsChunkPlugin({ name: 'common', chunks: 'initial', minChunks: 2 }) ]
webpack4 使用 splitChunks 的實現:webpack
module.exports = { optimization: { splitChunks: { cacheGroups: { vendor: { priority: 1, //添加權重 test: /node_modules/, //把這個目錄下符合下面幾個條件的庫抽離出來 chunks: 'initial', //剛開始就要抽離 minChunks: 2 //重複2次使用的時候須要抽離出來 }, common: { //公共的模塊 chunks: 'initial', minChunks: 2 } } } } }
一、將 vue、vue-router、vuex、element-ui 和 axios 這五個庫,所有改成經過 CDN 連接獲取,在 index.html 裏插入 相應連接。ios
<head> <link rel="stylesheet" href="https://cdn.bootcss.com/element-ui/2.0.7/theme-chalk/index.css" /> </head> <body> <div id="app"></div> <script src="https://cdn.bootcss.com/vue/2.6.10/vue.min.js"></script> <script src="https://cdn.bootcss.com/axios/0.19.0-beta.1/axios.min.js"></script> <script src="https://cdn.bootcss.com/vuex/3.1.0/vuex.min.js"></script> <script src="https://cdn.bootcss.com/vue-router/3.0.2/vue-router.min.js"></script> <script src="https://cdn.bootcss.com/element-ui/2.6.1/index.js"></script> <!-- built files will be auto injected --> </body>
二、在 webpack.config.js 配置文件web
module.exports = { ··· externals: { 'vue': 'Vue', 'vuex': 'Vuex', 'vue-router': 'VueRouter', 'element-ui': 'ELEMENT', 'Axios':'axios' } },
三、卸載依賴的 npm 包,npm uninstall axios element-ui vue vue-router vuexvue-router
四、修改 main.js 文件裏以前的引包方式vuex
// import Vue from 'vue' // import ElementUI from 'element-ui' // import 'element-ui/lib/theme-chalk/index.css' // import VueRouter from 'vue-router' import App from './App.vue' import routes from './router' import utils from './utils/Utils' Vue.use(ELEMENT) Vue.use(VueRouter) const router = new VueRouter({ mode: 'hash', //路由的模式 routes }) new Vue({ router, el: '#app', render: h => h(App) })
使用方法以下:
一、HappyPack 插件安裝: npm i -D happypack
二、webpack.base.conf.js 文件對 module.rules 進行配置
module: { rules: [ { test: /\.js$/, use: ['happypack/loader?id=babel'], include: [resolve('src'), resolve('test')], exclude: path.resolve(__dirname, 'node_modules') }, { test: /\.vue$/, use: ['happypack/loader?id=vue'] } ] }
三、在生產環境 webpack.prod.conf.js 文件進行配置
const HappyPack = require('happypack') // 構造出共享進程池,在進程池中包含5個子進程 const HappyPackThreadPool = HappyPack.ThreadPool({ size: 5 }) plugins: [ new HappyPack({ // 用惟一的標識符id,來表明當前的HappyPack是用來處理一類特定的文件 id: 'babel', // 如何處理.js文件,用法和Loader配置中同樣 loaders: ['babel-loader?cacheDirectory'], threadPool: HappyPackThreadPool }), new HappyPack({ id: 'vue', // 用惟一的標識符id,來表明當前的HappyPack是用來處理一類特定的文件 loaders: [ { loader: 'vue-loader', options: vueLoaderConfig } ], threadPool: HappyPackThreadPool }) ]
從零開始構建一個webpack項目
搭建一個vue-cli的移動端H5開發模板
封裝一個toast和dialog組件併發布到npm
一文讀盡前端路由、後端路由、單頁面應用、多頁面應用
總結解決跨域的幾個方法
淺談webscoket原理及其應用