vue-cli打包配置

const CompressionPlugin = require('compression-webpack-plugin')

module.exports = {
  // 项目部署的基础路径
  // 我们默认假设你的应用将会部署在域名的根部,
  // 比如 https://www.my-app.com/
  // 如果你的应用时部署在一个子路径下,那么你需要在这里
  // 指定子路径。比如,如果你的应用部署在
  // https://www.foobar.com/my-app/
  // 那么将这个值改为 `/my-app/`
  publicPath: '/',

  // 将构建好的文件输出到哪里(或者说将编译的文件)
  outputDir: 'dist',

  // 放置静态资源的地方 (js/css/img/font/...)
  assetsDir: '',

  // 用于多页配置,默认是 undefined
  // pages: {
  //   index: {
  //     // 入口文件
  //     entry: 'src/main.js',  /*这个是根入口文件*/
  //     // 模板文件
  //     template: 'public/index.html',
  //     // 输出文件
  //     filename: 'index.html',
  //     // 页面title
  //     title: '音视频'
  //   },
  //   // 简写格式
  //   // 模板文件默认是 `public/subpage.html`
  //   // 如果不存在,就是 `public/index.html`.
  //   // 输出文件默认是 `subpage.html`.
  //   subpage: 'src/main.js'    /*注意这个是*/
  // },

  // 是否在保存的时候使用 `eslint-loader` 进行检查。
  // 有效的值:`ture` | `false` | `"error"`
  // 当设置为 `"error"` 时,检查出的错误会触发编译失败。
  lintOnSave: true,

  // 使用带有浏览器内编译器的完整构建版本
  // 查阅 https://cn.vuejs.org/v2/guide/installation.html#运行时-编译器-vs-只包含运行时
  runtimeCompiler: false,

  // babel-loader 默认会跳过 node_modules 依赖。
  // 通过这个选项可以显式转译一个依赖。
  transpileDependencies: [/* string or regex */],

  // 是否为生产环境构建生成 source map?
  productionSourceMap: false,

  // 调整内部的 webpack 配置。
  // 查阅 https://github.com/vuejs/vue-docs-zh-cn/blob/master/vue-cli/webpack.md
  chainWebpack: (config) => {
    // 开启gzip压缩
    config.plugin('compression')
      .use(CompressionPlugin, [{
        algorithm: 'gzip',
        test: /\.js$|\.html$|\.css/,
        threshold: 10240,
        deleteOriginalAssets: false
      }])
      .end()
    // 抽离公共模块
    // 可参考 1) https://webpack.docschina.org/plugins/split-chunks-plugin/#defaults     2) https://blog.csdn.net/zm9998/article/details/111506878
    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-plus(.*)/ // in order to adapt to cnpm
        }
      },
      // commons: {
      //   name: 'chunk-commons',
      //   test: resolve('src/components'), // can customize your rules
      //   minChunks: 3, //  minimum common number
      //   priority: 5,
      //   reuseExistingChunk: true
      // }
    })
    // 路由使用懒加载时使用,打包时未发生改变的文件不会改变hash值
    config.optimization.runtimeChunk('single')
  },
  configureWebpack: () => {
    // return {
    //   plugins: [
    //     new CompressionPlugin({
    //       test: /\.js$|\.html$|\.css$|\.jpg$|\.jpeg$|\.png/, // 需要压缩的文件类型
    //       threshold: 10240, // 归档需要进行压缩的文件大小最小值,我这个是10K以上的进行压缩
    //       deleteOriginalAssets: false // 是否删除原文件
    //     })
    //   ]
    // }
  },

  // CSS 相关选项
  css: {
    // 将组件内的 CSS 提取到一个单独的 CSS 文件 (只用在生产环境中)
    // 也可以是一个传递给 `extract-text-webpack-plugin` 的选项对象
    extract: false,

    // 是否开启 CSS source map?
    sourceMap: true,

    // 为预处理器的 loader 传递自定义选项。比如传递给
    // sass-loader 时,使用 `{ sass: { ... } }`。
    loaderOptions: {},

    // 为所有的 CSS 及其预处理文件开启 CSS Modules。
    // 这个选项不会影响 `*.vue` 文件。
    requireModuleExtension: true // 这个值为false时,element自带的样式不会生效
  },

  // 在生产环境下为 Babel 和 TypeScript 使用 `thread-loader`
  // 在多核机器下会默认开启。
  parallel: require('os').cpus().length > 1,

  // PWA 插件的选项。
  // 查阅 https://github.com/vuejs/vue-cli/tree/dev/packages/@vue/cli-plugin-pwa
  pwa: {},

  // 配置 webpack-dev-server 行为。(跨域)
  devServer: {
    open: true,
    host: '',
    port: 8080,
    https: false,
    hotOnly: false,
    // 查阅 https://github.com/vuejs/vue-docs-zh-cn/blob/master/vue-cli/cli-service.md#配置代理
    proxy: {
      '/api': {
        target: 'http://localhost:8880',
        changeOrigin: true,
        secure: false,
        // ws: true,
        pathRewrite: {
            '^/api': '/static/mock'   // 请求数据路径别名,这里是注意将static/mock放入public文件夹
        }
      }
   }
  },

  // 三方插件的选项
  pluginOptions: {
    // ...
  }
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值