vue.config.js_学习之路

const UglifyJsPlugin = require("uglifyjs-webpack-plugin"); // 缩小JavaScript
const OptimizeCssnanoPlugin = require("@intervolga/optimize-cssnano-plugin"); // 压缩和去重css样式文件
const path = require("path");

const isProd =
  process.env.VUE_APP_FLAG === "production" ||
  process.env.VUE_APP_FLAG === "prod"; // 是否为生产环境

const name = process.env.VUE_APP_TITLE || "clean-front"; // 网页标题

var plugins = [
  new OptimizeCssnanoPlugin({
    sourceMap: false,
    cssnanoOptions: {
      preset: [
        "default",
        {
          mergeLonghand: false,
          cssDeclarationSorter: false,
        },
      ],
    },
  }),
];
console.log("================================isProd", isProd);

if (isProd) {
  plugins.unshift(
    new UglifyJsPlugin({
      uglifyOptions: { // uglify选项
        compress: { // 其他压缩选项
          drop_debugger: true,
          drop_console: true,
        },
      },
      sourceMap: false, // 使用 source map 将错误信息的位置映射到模块
      parallel: true, // 使用多进程并行运行来提高构建速度
    })
  );
}

const publicPathObj = {
  development: "/",
  test: "***",
  production:
    "***",
};

console.log("process.env.VUE_APP_FLAG]: ", process.env.VUE_APP_FLAG);
module.exports = {
  publicPath: publicPathObj[process.env.VUE_APP_FLAG], // 如果你的应用被部署在 https://www.my-app.com/my-app/,则设置 publicPath 为 /my-app/。
  outputDir: isProd ? "target/prod" : "target/test", // 当运行 vue-cli-service build 时生成的生产环境构建文件的目录
  assetsDir: "static", // 放置生成的静态资源 (js、css、img、fonts) 的 (相对于 outputDir 的) 目录。
  configureWebpack: { // webpack配置
    name: name, // 网页title 在html里进行设置
    devtool: // 此选项控制是否生成,以及如何生成 source map。
      process.env.VUE_APP_FLAG != "development" ? false : "eval-source-map",
    resolve: { // 解析
      alias: { // 创建 import 或 require 的别名,来确保模块引入变得更简单。
        "@": path.join(__dirname, "src"),
        "~": path.join(__dirname),
      },
    },
    plugins: plugins, // 插件
    optimization: { // 优化
      splitChunks: { // 动态导入模块 分离chunk
        cacheGroups: { // 缓存组可以继承和/或覆盖来自 splitChunks.* 的任何选项
          common: {
            //commons 一般是是个人定义的
            name: "chunk-common", // 打包后的文件名
            chunks: "initial",
            minChunks: 1,
            maxInitialRequests: 5,
            minSize: 0,
            priority: 1,
            reuseExistingChunk: true,
          },
          vendors: {
            //vendor 是导入的 npm 包
            name: "chunk-vendors",
            test: /[\\/]node_modules[\\/]/,
            chunks: "initial",
            maxSize: 600000,
            maxInitialRequests: 20,
            priority: 2,
            reuseExistingChunk: true,
            enforce: true,
          },
          antDesignVue: {
            //把antDesignVue从chunk-vendors.js提取出来。当然我们也可以把mixins,vue.min.js等等也按照类似配置提取出来
            name: "chunk-ant-design-vue",
            test: /[\\/]node_modules[\\/]ant-design-vue[\\/]/,
            chunks: "initial",
            priority: 3,
            maxSize: 600000,
            reuseExistingChunk: true,
            enforce: true,
          },
          styles: {
            name: "styles",
            test: /\.css$/,
            chunks: "all",
            enforce: true,
          },
        },
      },
    },
    devServer: {
      headers: {
        "Access-Control-Allow-Origin": "*",
      },
    },
  },
  devServer: {
    open: true,
    port: 8080,
    host: "0.0.0.0",
    https: false,
    hotOnly: true,
    disableHostCheck: true,
    proxy: {
      "/api": {
        target:
          "https://www.fastmock.site/mock/08e002ebed3dde51cdd5e7a73071b386/mockApi",
        changeOrigin: true,
        ws: true,
        pathRewrite: {
          "^/api": "/",
        },
      },
    },
  },
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值