2.项目根目录下build/webpack.base.config.js
中添加externals
externals: { vue: 'Vue', 'element': 'element-ui', 'axios':'axios', },
3.mian.js中import ... from ...与Vue.use(...)就可以去掉了,若没去掉webpack还是会把对应的依赖进行打包。
2019.01.12补充
vue-cli 2.x
在项目config/index.js中可以开启gzip压缩,对打包优化也有很大的帮助
1.首先安装插件 compression-webpack-plugin
cnpm install --save-dev compression-webpack-plugin
2.设置productionGzip: true
// Gzip off by default as many popular static hosts such as
// Surge or Netlify already gzip all static assets for you.
// Before setting to `true`, make sure to:
// npm install --save-dev compression-webpack-plugin
productionGzip: true,
productionGzipExtensions: ['js', 'css'],
// Run the build command with an extra argument to
// View the bundle analyzer report after build finishes:
// `npm run build --report`
// Set to `true` or `false` to always turn it on or off
bundleAnalyzerReport: process.env.npm_config_report
3.npm run build
执行后会发现每个js和css文件会压缩一个gz后缀的文件夹,浏览器如果支持g-zip 会自动查找有没有gz文件 找到了就加载gz然后本地解压 执行。
vue-cli 3.x
1.安装插件compression-webpack-plugin
npm install --save-dev compression-webpack-plugin
2.修改vue.config.js
文件
...
// gzip压缩插件
const CompressionWebpackPlugin = require('compression-webpack-plugin')
...
// webpack配置
configureWebpack: config => {
let plugins = [
new CompressionWebpackPlugin({
filename: '[path].gz[query]',
algorithm: 'gzip',
test: new RegExp(
'\\.(' +
['js', 'css'].join('|') +
')$',
),
threshold: 10240,
minRatio: 0.8,
}),
]
if (process.env.NODE_ENV !== 'development') {
config.plugins = [...config.plugins, ...plugins]
}
},
...
vue.config.js:
module.exports = {
pages,
devServer: {
host: "localhost",
port: `5001`,
// 代理链接配置
proxy: {
"/api": {
target: proxyHost,
changeOrigin: true,
pathRewrite: {
"^/api": ""
}
}
}
},
configureWebpack: config=>{
return {
plugins:[new webpack.BannerPlugin({
banner:"hash:[hash],chunkhash:[chunkhash],filename:[name],author:'lllomh',time:"+new Date()
}),
new CompressionWebpackPlugin({
filename: '[path].gz[query]',
algorithm: 'gzip',
test: new RegExp(
'\\.(' +
['js', 'css'].join('|') +
')$',
),
threshold: 10240,
minRatio: 0.8,
})
]
}
}
}
3.npm run build
执行后会发现每个js和css文件会压缩一个gz后缀的文件夹,浏览器如果支持g-zip 会自动查找有没有gz文件 找到了就加载gz然后本地解压 执行。
本文通过互联网收集整理