vue.config.js 4.38 KB
const path = require('path')
// js压缩插件
const TerserPlugin = require("terser-webpack-plugin")
// 设置moment
const MomentLocalesPlugin = require('moment-locales-webpack-plugin')
const CompressionPlugin = require("compression-webpack-plugin")

const isRrod = process.env.NODE_ENV === 'prod';
const date = new Date();
const Version = '' + date.getFullYear() + (date.getMonth() + 1) + date.getDate() + date.getHours();
function resolve (dir) {
    // path.join()方法用于连接路径
    return path.join(__dirname, dir)
}

module.exports = {
    runtimeCompiler: true,
    indexPath: "index.html",
    publicPath: '/', // 设置打包文件相对路径
    outputDir: 'dist',
/*    configureWebpack: {
        //关闭 webpack 的性能提示
        performance: {
            hints: false
        },
        output: { // 输出重构  打包编译后的 文件名称  【版本号】
            filename: `js/[name].${Version}.js`,
            chunkFilename: `js/[name].${Version}.js`,
        }
    },*/
    // productionSourceMap: isRrod ? false : true,
    productionSourceMap: false,
    // devServer: {
    //     port: 8089,
    //     open: true,
    //     proxy: {  //配置跨域
    //         '/api': {
    //             target: 'http://123.56.183.13:8889',  //真实的后台接口
    //             changOrigin: true,  //允许跨域
    //             pathRewrite: {
    //                 /* 重写路径,当我们在浏览器中看到请求的地址为:http://localhost:8080/api/core/getData/userInfo 时
    //                   实际上访问的地址是:http://121.121.67.254:8185/core/getData/userInfo,因为重写了 /api
    //                  */
    //                 '^/api': ''
    //             }
    //         },
    //         '/checkApi': {
    //             target: 'https://beta-yac.yiboshi.com',  //真实的后台接口
    //             changOrigin: true,  //允许跨域
    //             pathRewrite: {
    //                 '^/checkApi': ''
    //             }
    //         },
    //     }
    // },
    devServer: {
        open: true,
        overlay: {
            warning: false,
            errors: false
        },
        https:true,
    },
    chainWebpack: (config) => {
        config.plugins.delete('preload')
        // 移除 prefetch 插件
        config.plugins.delete('prefetch')
        // ant图标按需引入
        // config.resolve.alias.set('@ant-design/icons/lib/dist$', resolve("src/views/utils/icons.js"))
        if (process.env.NODE_ENV === 'production') {
            config.plugin('extract-css').tap(args => {
                args[0].filename = `css/[name].[contenthash:8].css`
                return args
            })
        }
    },
    configureWebpack: config => {
        // 开启gzip压缩
        /*if (process.env.NODE_ENV === 'production') {*/
            config.plugins.push(
                new CompressionPlugin({
                    filename: '[path][base].gz',
                    test: /\.js$|\.html$|\.css/, // 匹配文件名
                    threshold: 10,
                })
            )
       /* }*/
        config.externals = {
                'html2canvas': 'html2canvas'
        }
        // 输出重构  打包编译后的 文件名称  【版本号】
        config.output.filename = `js/[name].${Version}.js`
        config.output.chunkFilename = `js/[name].${Version}.js`
        // 剥离除 “en” 以外的所有语言环境
        config.plugins.push(new MomentLocalesPlugin())
        // 压缩js
        config.optimization.minimizer.push(
            new TerserPlugin({
                // 启用多线程
                parallel: 4,
                terserOptions: {
                    format: {
                        comments: false
                    },
                    compress: {
                        drop_console: true,
                        drop_debugger: false,
                        pure_funcs: ['console.log'], // 移除console
                    },
                    // 删除注释
                    extractComments: false
                }
            })
        )
    },
    css: {
        loaderOptions: {
            less: {
                modifyVars: {
                    'primary-color': '#FF4D80',
                    'border-primary-color': '#FFB4C0',
                    'border-radius-base': '2px',
                },
                javascriptEnabled: true
            }
        }
    },
}