vue使用webPack压缩后存储过大,怎么优化

  • 在生产环境去除developtool选项

    在webpack.config.js中设置的developtool选项,仅适用于开发环境,这样会造成打包成的文件有几M,所以在生产环境需要去除此项配置

  • 分离CSS

    安装npm插件

    npm install extract-text-webpack-plugin --save

    var ExtractTextPlugin = require("extract-text-webpack-plugin");

    loaders:[
{
test: /\.css$/,
loader: ExtractTextPlugin.extract("style-loader", "css-loader")
},
{
test: /\.less$/,
loader: ExtractTextPlugin.extract("style-loader", "css-loader!less-loader")
}, plugins: [ new ExtractTextPlugin("bundle.css")
]
  • 分离第三方库

    使用CommonChunkPlugin插件
entry: {
app: './src/main.js'
vendors: ['vue','vue-router','moment']
} plungins[
new Webpack.optimize.CommonChunkPlugin({
name: ['vendor', 'manifest'], // 如果有manifest 每次打包压缩后的文件不会改变hash
minChunks: function (module, count) {
// any required modules inside node_modules are extracted to vendor
return (
module.resource &&
/\.js$/.test(module.resource) &&
module.resource.indexOf(
path.join(__dirname, '../node_modules')
) === 0
)
}
})
]

每次使用commonChunkPlugin插件进行build之后,都会重新设置hash,导致Etag不同,每次上线都会更新Etag,就无法利用浏览器缓存了

  • 还有按需打包Loadash,也就是结合vue-router实现的懒加载

先看效果:

0.a5a1bae6addad442ac82.js文件是包含componentA,componentB,componentC三个vue组件的打包文件,当页面加载的时候并没有加载此文件,点击pageA的时候加载了一次,点击pageB,pageC的时候没有再次加载此文件。

实现步骤:

  1. 首先使用vue-cli创建一个包含vue-router的项目,如下:

  2. 在CommonComponts下面创建index.js:

    exports.ComponentA = require('./ComponentA')

    exports.ComponentB = require('./ComponentB')

    exports.ComponentC = require('./ComponentC')

  3. 使用AMD风格callback-require语法实现异步加载

    这时候在使用webpack打包是就可以实现将ABC这三个vue组件组合打包

    require(['components/CommonComponents'],function(CommonComponents){

    //do whatEver you want with CommonComponents

    })

    这时候模块加载成功是,回调函数中的CommonComponents参数就是一个包含ComponentA、ComponentB、 ComponentC 这三个组件选项的对象。

  4. 在路由配置文件中添加组件的配置文件依赖

    平时,我们使用工厂函数来加入vue组件文件的路由选项

    工厂函数写法 resolve => {require(['./my-vue-component'], resolve)}

    这时候,如果我们添加componentA的路由项,只需要加载刚刚使用callback-require处理好的CommonComponets对象

    let getCommonComponent = componentName => resolve => require(['components/CommonComponents'], components => resolve(components[componentName]))

  5. 然后再组件中或者路由中可以使用getCommonComponent('ComponentA')来调用其中的ComponentA组件

    在路由中调用为例子:

    routes: [
    {
    path: '/a',
    name: 'a',
    component: getCommonComponent('ComponentA')
    },
    {
    path: '/b',
    name: 'B',
    component: getCommonComponent('ComponentB')
    },
    {
    path: '/c',
    name: 'C',
    component: getCommonComponent('ComponentC')
    }
    ]
    1. 最终打包成的文件

vue使用webpack压缩后体积过大要怎么优化的更多相关文章

  1. vue 使用webpack打包后路径报错以及 alias 的使用

    一.vue 使用webpack打包后路径报错(两步解决) 1. config文件夹 ==> index.js ==> 把assetsPublicPath的 '/ '改为 './' 2. b ...

  2. vue通过webpack打包后怎么运行

    1. 成功使用webpack打包完成后会默认得到dist的文件夹 2. dist文件夹中有html与其他的静态文件 3. 在dist文件夹中打开命令窗口或者git,开一个服务器(像anywhere) ...

  3. vue项目 webpack打包后,图片路径是绝对路径

    vue项目,使用webpack打包,虽然在全局把路径改成了相对的路径,但是图片引用的路径还是异常的,解决办法如下: 1.config文件夹下index.js中: assetsPublicPath:&q ...

  4. vue项目webpack打包后修改配置文件

    从webpack打包结构中我们知道,vue中有一个存放外部资源的文件夹static,它里面的文件是不会被打包编译的,所以我们就可以利用外部引入js的方式将我们的想要的数据在index.html中以js ...

  5. vue项目webpack打包后图片路径错误

    首先项目是vue-cli搭建的,项目结构如下: 然后发现在css里写的图片引用地址在开发时正常显示,但在打包扔上服务器之后报错 报的是404,路径前面多了/static/css,不知道为啥. 在自己慢 ...

  6. vue webpack打包后 iconfont引入路径不对

    vue webpack打包后 iconfont引入路径不对 { test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/, loader: 'url-loader', option ...

  7. Vue项目用webpack打包后,预览时资源路径出错(文末有vue项目链接分享)

    最近用vue写了一些项目,项目写完之后需要打包之后才能放到网上展示,所以在这里记录一下项目打包的过程以及遇到的一些问题. --------------------------------------- ...

  8. webpack打包后不能调用,改用uglifyjs打包压缩

    背景: 项目基于原生js,没用到任何脚手架和框架,但也需要打包压缩. 项目的js中声明了一些全局变量 供其他js调用. 这时候如果用webpack打包,基于webpack特性,会嵌套一层大函数,会将j ...

  9. Vue-loader 开启压缩后的一些坑

    在使用vue-loader 配合webpack 对.vue文件进行加载的时候,如果开启了代码压缩会出来下面几种问题,做个记录. 丢失td结束标记,导致页面的布局错乱 input的属性type为text ...

随机推荐

  1. Jenkins部署及邮箱配置

    第一步:下载jenkins安装包,下载地址是https://jenkins.io/download/,下载通用的war文件即可,这个格式文件不论哪个操作系统都可运行: 第二步:jdk安装,最新的jen ...

  2. MySQL--Basic(二)

    USE db_name; CREATE DATABASE school; Use school; CREATE TABLE `StuInfo` ( `STU_ID` ) NOT NULL , `STU ...

  3. I.MX6 FFmpeg 录制视频

    /************************************************************************* * I.MX6 FFmpeg 录制视频 * 说明: ...

  4. 影响Cache的几个HTTP头信息【转载http://hi.baidu.com/feilala_fly/item/f79eca08fbf389026c9048a7】

    Http的Cache机制总共有4个组成部分: Cache-Control.Last-Modified(If-Modified-Since).Etag(If-None-Match) .Expires 服 ...

  5. POJ1904 King's Quest

    King's Quest Language:Default King's Quest Time Limit: 15000MS Memory Limit: 65536K Total Submission ...

  6. ACM学习历程—HDU1030 Delta-wave(数学)

    Description A triangle field is numbered with successive integers in the way shown on the picture be ...

  7. [JSOI 2015] 最大公约数

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4488 [算法] 不妨首先枚举左端点 注意到对于任意一个正整数n , 其质因子个数是l ...

  8. ie11

    可用:document.documentMode来检测. var isIE11 = function() { var result = false; if (document.documentMode ...

  9. POJ1703(2集合并查集)

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39402   Accepted: ...

  10. JavaScript设模式---单例模式

    单例模式也称为单体模式,其中: 1,单体模式用于创建命名空间,将系列关联的属性和方法组织成一个逻辑单元,减少全局变量. 逻辑单元中的代码通过单一的变量进行访问. 2,三个特点: ① 该类只有一个实例: ...