import { defineConfig, loadEnv } from 'vite' import vue from '@vitejs/plugin-vue' import AutoImport from 'unplugin-auto-import/vite' import Components from 'unplugin-vue-components/vite' import { ElementPlusResolver } from 'unplugin-vue-components/resolvers' import path from 'path'; import { VxeTableResolver } from '@vxecli/import-unplugin-vue-components'; import CssMinimizerPlugin from "css-minimizer-webpack-plugin"; import TerserPlugin from "terser-webpack-plugin"; const name = "闲鱼特卖开放平台"; // page title const port = process.env.port || process.env.npm_config_port || 80; // dev port // https://vitejs.dev/config/ export default defineConfig((command, mode) => { const env = loadEnv(mode, __dirname); return { publicPath: "./", outputDir: "dist", assetsDir: "static", lintOnSave: env.VITE_MODE === "development", //lintOnSave: false, productionSourceMap: false, plugins: [ vue(), AutoImport({ resolvers: [ElementPlusResolver(), VxeTableResolver()], }), Components({ resolvers: [ElementPlusResolver(), VxeTableResolver()], }), ], define: { 'process.env': { VITE_APP_BASE_API: 'JSON.stringify(env.VUE_APP_BASE_API)', VITE_APP_BASE_SITE: 'JSON.stringify(env.VITE_APP_BASE_SITE)', VITE_APP_BASE_URL:'JSON.stringify(env.VITE_APP_BASE_URL)', }, }, server: { port: port, open: true, hot: true, overlay: { warnings: false, errors: true, }, host: '0.0.0.0', proxy: { [process.env.VUE_APP_BASE_API]: { //这里是公共部分,在调用接口时后面接不相同的部分 target: process.env.VUE_APP_BASE_API, //修改后台接口地址 changeOrigin: true, //开启跨域 pathRewrite: { //重命名 ["^" + process.env.VUE_APP_BASE_API]: "", }, }, '^/api': { target: 'http://dev-erp-api.xtkj99.com:7890', changeOrigin: true } }, disableHostCheck: true, }, resolve: { // 设置路径别名 alias: { '@': path.join(__dirname, '/src'), '@services': path.join(__dirname, '/src/services'), '@axioshooks': path.join(__dirname, '/src/services/axioshooks'), '@assets': path.join(__dirname, '/src/assets'), '@components': path.join(__dirname, '/src/components'), '@api': path.join(__dirname, '/src/services/api'), '@untils': path.join(__dirname, '/src/untils') } }, css: { // 是否使用css分离插件 ExtractTextPlugin extract: { filename: `static/js/chunk-[hash].css` }, }, configureWebpack: { output: { filename: `static/js/chunk-[hash].js` }, module: { rules: [ { test: /\.js$/, include: path.resolve(__dirname, '../src'), exclude: /node_modules/, use: [ 'cache-loader', { loader: 'thread-loader', options: { workers: 2 } }, 'bable-loader' ] } ] }, }, // 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, optimization: { minimizer: [ // For webpack@5 you can use the `...` syntax to extend existing minimizers (i.e. `terser-webpack-plugin`), uncomment the next line // `...`, new CssMinimizerPlugin(), new TerserPlugin({ terserOptions: { compress: { drop_console: true, // 生产环境自动删除console }, } }) ], }, chainWebpack(config) { // it can improve the speed of the first screen, it is recommended to turn on preload config.plugin("preload").tap(() => [ { rel: "preload", // to ignore runtime.js // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171 fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/], include: "initial", }, ]); // when there are many pages, it will cause too many meaningless requests 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].${Version}.[ext]`, }) .end(); 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 }, commons: { name: "chunk-commons", test: resolve("src/components"), // can customize your rules minChunks: 3, // minimum common number priority: 5, reuseExistingChunk: true, }, }, }); // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk config.optimization.runtimeChunk("single"); config.optimization.minimizer("terser").tap((args) => { args[0].parallel = 4; args[0].terserOptions.compress.warnings = true; args[0].terserOptions.compress.drop_debugger = true; args[0].terserOptions.compress.drop_console = true; return args; }); }); }, } })