vue.config.js 5.8 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 VueLoaderPlugin = require('vue-loader/lib/plugin');
// module.exports = {
//     entry:path.join(__dirname,'./src/main.js'),
//     output:{
//         path:path.join(__dirname,'./dist'),
//         filename:'bundle.js'
//     },
//     module:{
//         rules: [
//             {
//                 test:/\.vue$/,
//                 loader: 'vue-loader',
//             },
//             {// 添加这个json,解决如上的报错问题
//                 test: /\.scss$/,
//                 use: ['style-loader','css-loader', 'sass-loader']
//             }, 
//         ]
//     },
//     plugins: [
//         new VueLoaderPlugin()
//     ]
// }


const isRrod = process.env.NODE_ENV === 'dev';
const date = new Date();
const Version = date.getTime();
const port = 8081;
// 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: port,
        open: true,
        overlay: {
            warning: false,
            errors: false
        },
        // lintOnSave: false,
        // host: 'local.yiboshi.com',
        https: false,
        proxy: {
            '/v1/science-admin': {
                target: process.env.VUE_APP_BASE_URL,  //真实的后台接口
                changOrigin: true,  //允许跨域
                pathReWrite: {
                    '^/v1/science-admin': '/v1/science-admin'
                }
            },
            '/upload/files': {
                target: process.env.VUE_APP_BASE_URL,  //真实的后台接口
                changOrigin: true,  //允许跨域
                pathReWrite: {
                    '^/upload/files': '/upload/files'
                }
            },
            '/oauth/token': {
                target: process.env.VUE_APP_BASE_URL,
                changeOrigin: true,
                pathRewrite: {
                    '^/oauth/token': '/oauth/token'
                }
            },
            '/v1/user': {
                target: process.env.VUE_APP_BASE_API,
                changeOrigin: true,
                pathRewrite: {
                    '^/v1/user': '/v1/user'
                }
            },
            '/v1/function': {
                target: process.env.VUE_APP_BASE_API,
                changeOrigin: true,
                pathRewrite: {
                    '^/v1/function': '/v1/function'
                }
            }
        }
    },
    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',
            // 解决xlsx-style报错的问题
            './cptable': 'var cptable'
        }
        // 输出重构  打包编译后的 文件名称  【版本号】
        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
                    },
                    // 删除注释
                    // @ts-ignore
                    extractComments: false
                }
            })
        )
    },
    pluginOptions: {
        'style-resources-loader': {
            preProcessor: 'less',
            patterns: [
                resolve('static/antd/variable.less')
            ]
        }
    },
    css: {
        loaderOptions: {
            less: {
                modifyVars: {
                    'primary-color': '#1890ff',
                    'border-primary-color': '#1890ff',
                    'border-radius-base': '2px',
                },
                javascriptEnabled: true
            }
        }
    },
}