• vue cli4 配置示例


    vue.config.js

    const Webpack = require('webpack')
    const CompressionWebpackPlugin = require('compression-webpack-plugin')
    const path = require('path')
    const TerserPlugin = require('terser-webpack-plugin')
    const productionGzipExtensions = /\.(js|css|json|ttf)(\?.*)?$/i
    const resolve = dir => path.join(__dirname, dir)
    const port = process.env.port || process.env.npm_config_port || 8088// dev port
    const isPro=process.env.NODE_ENV === 'production'
    
    module.exports = {
      lintOnSave:false,
      // 基础路径
      publicPath: isPro ? "./" : "./",
      // 输出文件目录
      outputDir: 'dist',
      assetsDir: 'static',
      productionSourceMap:false,  // 设置上线后是否加载webpack文件
      parallel: require("os").cpus().length > 1, //该选项在系统的 CPU 有多于一个内核时自动启用,仅作用于生产构建。
      devServer: {
        port: port,
        open: true,
        overlay: {
          warnings: false,
          errors: true
        }
      },
      chainWebpack: config => {
        config.plugin('html').tap(args => {
          const date = new Date()
          args[0].createDate = date
          return args
        })
        config.resolve.symlinks(true); // 修复热更新失效
        // 添加别名
        config.resolve.alias
          .set('@', resolve('src'))
          .set('assets', resolve('src/assets'))
          .set('api', resolve('src/api'))
          .set('views', resolve('src/views'))
          .set('components', resolve('src/components'))
    
          config.module
          .rule('min-image')
          .test(/\.(png|jpe?g|gif)(\?.*)?$/)
          .use('image-webpack-loader')
          .loader('image-webpack-loader')
          .options({ disable: process.env.NODE_ENV == 'development' ? true : false })//此处为ture的时候不会启用压缩处理,目的是为了开发模式下调试速度更快
          .end()
      },
    
      configureWebpack: {
        performance: {
          hints:false
        },
        plugins: [
          new CompressionWebpackPlugin({
            filename: '[path][name].gz[query]',
            algorithm: 'gzip',
            test: productionGzipExtensions,
            threshold: 10240, // 只有大小大于该值的资源会被处理
            minRatio: 0.8, // 只有压缩率小于这个值的资源才会被处理
            // deleteOriginalAssets: true // 删除原文件
          }),
          new Webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
        ],
        optimization: {
          minimizer: [
            new TerserPlugin({
              terserOptions: {
                ecma: undefined,
                warnings: false,
                parse: {},
                compress: {
                  drop_console: true,
                  drop_debugger: false,
                  pure_funcs: ['console.log'], // 移除console
                },
              },
            }),
          ],
          splitChunks: { // 分割代码块
            cacheGroups: {
                vendor: {//第三方库抽离
                    chunks: 'all',
                    test: /node_modules/,
                    name: 'vendor',
                    minChunks: 1,//在分割之前,这个代码块最小应该被引用的次数
                    maxInitialRequests: 5,
                    minSize: 0,//大于0个字节
                    priority: 100//权重
                },
                common: {  //公用模块抽离
                    chunks: 'all',
                    test: /[\\/]src[\\/]js[\\/]/,
                    name: 'common',
                    minChunks: 2,//在分割之前,这个代码块最小应该被引用的次数
                    maxInitialRequests: 5,
                    minSize: 0,//大于0个字节
                    priority: 60
                },
                styles: { //样式抽离
                    name: 'styles',
                    test: /\.(sa|sc|c)ss$/,
                    chunks: 'all',
                    enforce: true
                },
                runtimeChunk: {
                    name: 'manifest'
                }
            }
        }
        },
      },
      css: {
        extract: isPro ? {
          ignoreOrder: true,
       } : false,
          loaderOptions: {
            less: {
              globalVars: {
                hack: `true; @import '~@/assets/css/variable.less';`
              }
            }
          }
      }
    
    };
    

      

  • 相关阅读:
    js全局变量
    $.getJSON异步请求和同步请求
    让js中的函数只有一次有效调用
    两个div并排显示,当浏览器界面缩小时会出现换行
    jquery获取窗口和文档的高度和宽度
    后台传带引号(")的数据需要注意
    C# dynamic
    (转)数据库函数解析JSON字符串
    Unicode和UTF-8
    用户通过浏览器修改表单隐藏域
  • 原文地址:https://www.cnblogs.com/7c89/p/15714887.html
Copyright © 2020-2023  润新知