add
yirongjin
2025-07-23 d518ecbcbc853e891c389c422d1470b028ed1dc6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const webpack = require('webpack');
const path = require('path');
 
const CompressionWebpackPlugin = require('compression-webpack-plugin');
const defaultSettings = require('./src/settings.js');
const port = process.env.port || process.env.npm_config_port || 9527; // dev port
function resolve (dir) {
  return path.join(__dirname, dir);
}
const isProd = process.env.NODE_ENV === 'production';
const name = defaultSettings.title || '智能化立体库管理系统'; // page title
const {
  VueCDN,
  AxiosCDN,
  VueRouterCDN,
  VuexCDN
} = require('./src/plugins/cdn');
 
const cdn = {
  css: [],
  js: [VueCDN, AxiosCDN, VueRouterCDN, VuexCDN],
  externals: {
    vue: 'Vue',
    'vue-router': 'VueRouter',
    vuex: 'Vuex',
    axios: 'axios'
  }
};
module.exports = {
  /**
   *查看配置 https://cli.vuejs.org/config/#publicpath
   */
  publicPath: '/', //根路径 cli3.0以上使用publicPath
  //打包后输出路径
  outputDir: 'dist',
  assetsDir: 'static',
  lintOnSave: false,
  //去除生产环境的productionSourceMap
  productionSourceMap: false,
  devServer: {
    port: port,
    open: true,
    overlay: {
      warnings: false,
      errors: true
    },
    proxy: {
      [process.env.VUE_APP_BASE_API]: {
        //target: 'http://10.103.11.236:8085', // 内网服务器部署 11网段
        //target: 'http://10.103.9.200:8085/', // 内网服务器部署 9网段
        //target: 'http://120.53.244.200:8085', // 云服务器测试
        //target: 'http://192.168.43.252:8085', // 本机发布测试
        //target: 'http://localhost:12319/', // 本机调试测试
        //target: 'http://10.103.11.130:8085/', //11网段
        target: 'http://localhost:6099/', //9网段
        changeOrigin: true,
        pathRewrite:{
          '[process.env.VUE_APP_BASE_API]': ''
        }
      }
    }
  },
  css: {
    loaderOptions: {
      sass: {}
    }
  },
  // configureWebpack 值为对象,会通过 webpack-merge 合并到最终的配置
  configureWebpack: {
    //在webpack的name字段中提供应用程序的标题,以便可以在index.html中访问它来注入正确的标题
    name: name,
    resolve: {
      alias: {
        '@': resolve('src')
      }
    },
 
    externals: {}
  },
 
  chainWebpack (config) {
    config.plugin('define').tap(args => {
      //args[0]['process.env'].IMAGE_URL = "'http://120.53.244.200:8085/'"; //测试环境
      // args[0]['process.env'].IMAGE_URL = "'http://localhost:8085/'"; //正式环境 11网段
      args[0]['process.env'].IMAGE_URL = "'http://10.103.9.200:8085/'"; //正式环境 9网段
      //args[0]['process.env'].IMAGE_URL = "'http://192.168.137.200:8085/'"; //本机发布测试
      return args;
    });
    config.plugin('provide').use(webpack.ProvidePlugin, [{
      $: 'jquery',
      jquery: 'jquery',
      jQuery: 'jquery',
      'window.jQuery': 'jquery'
    }]);
    // 设置svg
    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]'
      })
      .end();
    //设置开发环境sourceMap
    config.when(!isProd, config => config.devtool('cheap-source-map'));
    //开发环境
 
    config.when(isProd, config => {
      config.optimization.splitChunks({
        chunks: 'all',
        cacheGroups: {
          libs: {
            name: 'chunk-libs',
            test: /[\\/]node_modules[\\/]/,
            priority: 10,
            chunks: 'initial'
          },
          elementUI: {
            name: 'chunk-elementUI',
            priority: 20,
            test: /[\\/]node_modules[\\/]_?element-ui(.*)/
          },
          commons: {
            name: 'chunk-commons',
            test: resolve('src/components'),
            minChunks: 3,
            priority: 5,
            reuseExistingChunk: true
          }
        }
      });
      // config.plugin('html').tap(args => {
      //     args[0].cdn = cdn;
      //     return args;
      // });
      config.optimization.runtimeChunk('single');
 
      //去除生产环境debugger 和console
      config.optimization.minimizer('terser').tap(args => {
        args[0].terserOptions.compress.warnings = false;
        args[0].terserOptions.compress.drop_console = true;
        args[0].terserOptions.compress.drop_debugger = true;
        args[0].terserOptions.compress.pure_funcs = ['console.*'];
        return args;
      });
      //g-zip开启
      config.plugin('CompressionWebpackPlugin').use(CompressionWebpackPlugin, [{
        filename: '[path].gz[query]',
        algorithm: 'gzip',
        test: /\.js$|\.css/, //匹配文件名
        threshold: 10240, //对超过10k的数据压缩
        minRatio: 0.8
      }]);
    });
  }
};