vue项目打包流程
二、打包优化
1.首先在根目录下面创建一个vue.config.js
文件
2.去除.map文件,在vue.config.js文件里面抛出一个对象,然后把下面两段代码写到对象里面,然后在控制台或者终端通过npm run build命令,打包项目
注:如果一次不行,就在执行一次
module.exports = {}//在vue.config.js文件里面抛出一个对象
publicPath: "./",
productionSourceMap: false, //不输出map文件
3.开启CDN加速,把下面这段代码写到vue.config.js
这个文件里面,切记要写到module.exports = {}这个对象上面
/ 是否为生产环境
const isProduction = process.env.NODE_ENV !== 'development';
// 本地环境是否需要使用cdn
const devNeedCdn = false
// cdn链接
const cdn = {
// cdn:模块名称和模块作用域命名(对应window里面挂载的变量名称)
externals: {
vue: 'Vue',
vuex: 'Vuex',
'vue-router': 'VueRouter',
'marked': 'marked',
'highlight.js': 'hljs',
'nprogress': 'NProgress',
'axios': 'axios'
},
// cdn的css链接
css: [
],
// cdn的js链接
js: [
'https://cdn.bootcss.com/vue/2.6.10/vue.min.js',
'https://cdn.bootcss.com/vuex/3.1.2/vuex.min.js',
'https://cdn.bootcss.com/vue-router/3.1.3/vue-router.min.js',
'https://cdn.bootcss.com/axios/0.19.2/axios.min.js'
]
}
3.1:在抛出的那个对象module.exports = {}里面写入下面这段代码
chainWebpack: config => {
// ============注入cdn start============
config.plugin('html').tap(args => {
// 生产环境或本地需要cdn时,才注入cdn
if (isProduction || devNeedCdn) args[0].cdn = cdn
return args
})
// ============注入cdn start============
},
3.2:在public这个文件夹下面,找到index.html这个文件,在title
这个标签下面,head
头标签里面把下面这段代码复制进去
<!-- 使用CDN的CSS文件 -->
<% for (var i in htmlWebpackPlugin.options.cdn &&
htmlWebpackPlugin.options.cdn.css) { %>
<link href="<%= htmlWebpackPlugin.options.cdn.css[i] %>" rel="stylesheet" />
<% } %>
<!-- 使用CDN的CSS文件 -->
<!-- 使用CDN的JS文件 -->
<% for (var i in htmlWebpackPlugin.options.cdn &&
htmlWebpackPlugin.options.cdn.js) { %>
<script src="<%= htmlWebpackPlugin.options.cdn.js[i] %>"></script>
<% } %>
<!-- 使用CDN的JS文件 -->
3.3:上面这些操作步骤就是开始CDN加速的流程,把上面这些步骤操作完以后,我们继续在控制台输入npm run build
这个命令,继续打包,打包完成以后在你的文件夹里面应该会有一个dist这个文件,这个文件就是打包后的文件,你可以鼠标右键,点击属性,就可以看到他的体积已经变小啦
4.我们继续执行下面的步骤
代码压缩:
4.1:首先我们先在控制台安装命令:npm i -D uglifyjs-webpack-plugin
4.2.然后把下面这段代码写到vue.config.js
这个文件里面,这段代码也是要写到抛出的那个对象外面,不能写到抛出的对象里面
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
4.3:接着我们把下面的代码写到configureWebpack
这个对象里面,写完以后继续执行npm run build
命令,继续打包,打包完成以后再dist这个文件夹里面有个index.html,然后双击打开它,看项目是否能跑起来
// 代码压缩
config.plugins.push(
new UglifyJsPlugin({
uglifyOptions: {
//生产环境自动删除console
compress: {
drop_debugger: true,
drop_console: true,
pure_funcs: ['console.log']
}
},
sourceMap: false,
parallel: true
})
)
5.接着操作公共代码抽离,还是在configureWebpack
这个对象里面继续往下写
// 公共代码抽离
config.optimization = {
splitChunks: {
cacheGroups: {
vendor: {
chunks: 'all',
test: /node_modules/,
name: 'vendor',
minChunks: 1,
maxInitialRequests: 5,
minSize: 0,
priority: 100
},
common: {
chunks: 'all',
test: /[\\/]src[\\/]js[\\/]/,
name: 'common',
minChunks: 2,
maxInitialRequests: 5,
minSize: 0,
priority: 60
},
styles: {
name: 'styles',
test: /\.(sa|sc|c)ss$/,
chunks: 'all',
enforce: true
},
runtimeChunk: {
name: 'manifest'
}
}
}
}
6.骨架屏
- 先安装
npm install vue-skeleton-webpack-plugin
这个命令 - 然后在src这个文件夹下面创建一个Skeleton文件夹,这个名字可以自定义,在这个文件夹下面创建两个文件,
index.js
和index.vue
这个两个文件
index.js里面写入下面代码
import Vue from 'vue'
import Skeleton from './index.vue'
export default new Vue({
components: {
Skeleton
},
template: '<Skeleton />'
})
index.vue里面写入:
<template>
<div class="sk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NGNkY2RjYiIHJ4PSI0Ii8+PHBhdGggZmlsbD0iI0Y2RjZGNiIgZD0iTTIzMCAxMThoMzY5djMwSDIzMHpNMjMwIDE4MmgzMjN2MzBIMjMwek04MTIgMTE1aDIzOHYzOUg4MTJ6TTgwOCAxODRoMjQydjMwSDgwOHpNOTE3IDQ4aDEzM3YzN0g5MTd6Ii8+PC9nPjwvc3ZnPg==">
</section>
</div>
</template>
<script>
export default {
name: 'skeleton'
}
</script>
<style scoped>
.skeleton-header {
height: 40px;
background: #1976d2;
padding:0;
margin: 0;
width: 100%;
}
.skeleton-block {
display: flex;
flex-direction: column;
padding-top: 8px;
}
</style>
7.下面在vue.config.js
写入
注:这段代码写到configureWebpack
这个对象里面
config.plugins.push(new SkeletonWebpackPlugin({
webpackConfig: {
entry: {
app: path.join(__dirname, './src/skeleton/indx.js'),
},
},
minimize: true,
quiet: true,
// 如果不设置那么所有的路由都会共享这个骨架屏组件
router: {
mode: 'hash',
// 给对应的路由设置对应的骨架屏组件,skeletonId的值根据组件设置的id
routes: [
{ path: '/home', skeletonId: 'skeleton' }
]
}}))
下面这段代码写到对象外面,不能学到任何的一个对象里面
//骨架屏渲染
const SkeletonWebpackPlugin = require('vue-skeleton-webpack-plugin')
//path引入
const path = require('path')