Vue 打包文件过大
发布于 5 天前 作者 chaoren 55 次浏览 来自 问答
粉丝福利 : 关注VUE中文社区公众号,回复视频领取粉丝福利

最近项目需求,上手vue,有个代码编辑器的插件 vue2-ace-editor 打包后发现很大,发现是依赖的brace太多的关系

drwxrwxr-x 2 root root 4.0K 4月   1 17:39 ./
drwxrwxr-x 6 root root 4.0K 4月   1 17:39 ../
-rw-rw-r-- 1 root root 1.3M 4月   1 17:39 app.734c31c8.js

// 一共大约 5m 作为一个全局插件,太大了
-rw-rw-r-- 1 root root 1.1M 4月   1 17:39 chunk-brace~254db813.4b29b663.js
-rw-rw-r-- 1 root root 362K 4月   1 17:39 chunk-brace~47ae76c5.1b347347.js
-rw-rw-r-- 1 root root 1.9M 4月   1 17:39 chunk-brace~4edd98d2.0ffee1f3.js
-rw-rw-r-- 1 root root 1.7M 4月   1 17:39 chunk-brace~603618cf.db08e5a1.js
-rw-rw-r-- 1 root root 194K 4月   1 17:39 chunk-brace~6239ec68.54d9001c.js
-rw-rw-r-- 1 root root  55K 4月   1 17:39 chunk-brace~c2609f75.fc2dd21c.js

// 其他文件略过

想知道有没有用过这个插件的大佬,怎么按需打包呢? 因为我只使用几种语言的编辑器而已

附上 vue.config.js

'use strict'
const path = require('path')

function resolve (dir) {
  return path.join(__dirname, dir) // __dirname 是nodejs的全局变量
}

// 端口,默认是8080
const port = 8080
const name = 'Demo Service'

// All configuration item explanations can be find in https://cli.vuejs.org/config/
module.exports = {
  /**
   * You will need to set publicPath if you plan to deploy your site under a sub path,
   * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
   * then publicPath should be set to "/bar/".
   * In most cases please use '/' !!!
   * Detail: https://cli.vuejs.org/config/#publicpath
   */
  publicPath: '/',
  outputDir: 'dist',
  assetsDir: 'static',
  lintOnSave: process.env.NODE_ENV === 'development',
  productionSourceMap: false,
  // 文件名按照hash生成,如果你的html不是通过vue-cli生成的,要改为false
  filenameHashing: true,
  // 定义开发环境下的服务器
  devServer: {
    port: port,
    open: true,
    overlay: {
      warnings: false,
      errors: true
    }
  },
  configureWebpack: {
    // provide the app's title in webpack's name field, so that
    // it can be accessed in index.html to inject the correct title.
    name: name,
    resolve: {
      alias: {
        '@': resolve('src')
      }
    }
  },
  // 以下是webpack链的一些优化策略
  chainWebpack (config) {
    config.plugins.delete('preload') 
    config.plugins.delete('prefetch') 

    // set svg-sprite-loader
    config.module
      .rule('svg')
      .exclude.add(resolve('src/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()

    // set preserveWhitespace
    config.module
      .rule('vue')
      .use('vue-loader')
      .loader('vue-loader')
      .tap(options => {
        options.compilerOptions.preserveWhitespace = true
        return options
      })
      .end()

    config
    // https://webpack.js.org/configuration/devtool/#development
      .when(process.env.NODE_ENV === 'development',
        config => config.devtool('cheap-source-map')
      )

    config
      .when(process.env.NODE_ENV !== 'development',
        config => {
          config
            .plugin('ScriptExtHtmlWebpackPlugin')
            .after('html')
            .use('script-ext-html-webpack-plugin', [{
              // `runtime` must same as runtimeChunk name. default is `runtime`
              inline: /runtime\..*\.js$/
            }])
            .end()
          config
            .optimization.splitChunks({
              chunks: 'all',
              cacheGroups: {
                libs: {
                  name: 'chunk-libs',
                  test: /[\\/]node_modules[\\/]/,
                  priority: 10,
                  chunks: 'initial' // only package third parties that are initially dependent
                },
                elementUI: {
                  name: 'chunk-elementUI', // split elementUI into a single package
                  priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
                  test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
                },
                // 主要是ace编辑器的,文件大,独立出来
                brace: {
                  name: 'chunk-brace', // split elementUI into a single package
                  priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
                  test: /[\\/]node_modules[\\/]_?brace(.*)/,
                  minSize: 100000,
                  maxSize: 5000000
                },
                commons: {
                  name: 'chunk-commons',
                  test: resolve('src/components'), // can customize your rules
                  minChunks: 3, //  minimum common number
                  priority: 5,
                  reuseExistingChunk: true
                }
              }
            })
          config.optimization.runtimeChunk('single')
        }
      )
  }
}
回到顶部