惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 聂微东
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
L
LangChain Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 司徒正美
WordPress大学
WordPress大学
T
The Blog of Author Tim Ferriss
Blog — PlanetScale
Blog — PlanetScale
J
Java Code Geeks
Y
Y Combinator Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
GbyAI
GbyAI
Vercel News
Vercel News
大猫的无限游戏
大猫的无限游戏
T
Tailwind CSS Blog
Jina AI
Jina AI
B
Blog
Recorded Future
Recorded Future
MyScale Blog
MyScale Blog
I
InfoQ
aimingoo的专栏
aimingoo的专栏
博客园_首页
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
雷峰网
雷峰网
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
腾讯CDC
爱范儿
爱范儿
Last Week in AI
Last Week in AI
博客园 - 三生石上(FineUI控件)
博客园 - Franky
Schneier on Security
Schneier on Security
V
V2EX
TaoSecurity Blog
TaoSecurity Blog
H
Hacker News: Front Page
Cloudbric
Cloudbric
D
DataBreaches.Net
B
Blog RSS Feed
P
Palo Alto Networks Blog
云风的 BLOG
云风的 BLOG
NISL@THU
NISL@THU
I
Intezer
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Cyberwarzone
Cyberwarzone
F
Fortinet All Blogs
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cybersecurity and Infrastructure Security Agency CISA
C
Cisco Blogs
K
Kaspersky official blog
Forbes - Security
Forbes - Security

博客园 - 我就是那个王小明

git修改历史提交记录名字 gitbase配置两个git仓库源头地址 yarn dev 或者 npm run dev 或node -v 等报错:'node' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 htmlToPdf ES6 Generator使用 - 我就是那个王小明 - 博客园 数组中的每一项按照某个属性分组 - 我就是那个王小明 - 博客园 前端开发快速定位bug的一些小技巧 基本css拼图形 forEach时候删除数组某一属性项,使用splice容易出现问题 对于vue的一些理解 配置vuex并使用 vue搭建开发环境 一些意想不到的小bug。 小程序开发中遇到的问题 rem原理 分别使用ES5和ES6进行数组去重以及注意事项 nodeJS理解 package.json和bower的参数解释 Angular.js基础
vue使用webpack压缩后体积过大要怎么优化
我就是那个王小明 · 2018-05-18 · via 博客园 - 我就是那个王小明

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. 最终打包成的文件