jazz
2022-03-03 adf10a882ee565d6a5c37ba07a9e8ec2289ccf34
提交 | 用户 | age
2a61f6 1 'use strict'
L 2 const path = require('path')
3 const defaultSettings = require('./src/settings.js')
4
5 function resolve(dir) {
6   return path.join(__dirname, dir)
7 }
8
9 const name = defaultSettings.title || 'vue Admin Template' // page title
10
11 // If your port is set to 80,
12 // use administrator privileges to execute the command line.
13 // For example, Mac: sudo npm run
14 // You can change the port by the following methods:
15 // port = 9528 npm run dev OR npm run dev --port = 9528
16 const port = process.env.port || process.env.npm_config_port || 9528 // dev port
17
18 // All configuration item explanations can be find in https://cli.vuejs.org/config/
19 module.exports = {
20   /**
21    * You will need to set publicPath if you plan to deploy your site under a sub path,
22    * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
23    * then publicPath should be set to "/bar/".
24    * In most cases please use '/' !!!
25    * Detail: https://cli.vuejs.org/config/#publicpath
26    */
27   publicPath: './',
28   outputDir: 'dist',
29   assetsDir: 'static',
30   lintOnSave: process.env.NODE_ENV === 'development',
31   productionSourceMap: false,
32   devServer: {
33     proxy: {
34       '/api_test': {
35         target: 'http://192.168.31.22:8080/',
36         // target: 'http://192.168.1.163:8080/',
37         // target: 'http://192.168.31.143:8080/', // 和
38         // target: 'http://192.168.31.143:8888/', // 和
39         changeOrigin: true,
40         ws: true,
41         pathRewrite: {
42           '^/api_test': ''
43         }
44       },
45       '/api_online': {
46         target: 'https://线上请求路径/',
47         changeOrigin: true,
48         ws: true,
49         pathRewrite: {
50           '^/api_online': ''
51         }
52       }
53     }
54   },
55   configureWebpack: {
56     // provide the app's title in webpack's name field, so that
57     // it can be accessed in index.html to inject the correct title.
58     name: name,
59     resolve: {
60       alias: {
61         '@': resolve('src')
62       }
63     }
64   },
65   chainWebpack(config) {
66     // it can improve the speed of the first screen, it is recommended to turn on preload
67     config.plugin('preload').tap(() => [
68       {
69         rel: 'preload',
70         // to ignore runtime.js
71         // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
72         fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
73         include: 'initial'
74       }
75     ])
76
77     // when there are many pages, it will cause too many meaningless requests
78     config.plugins.delete('prefetch')
79
bf9798 80     // 打包后注释console TODO 待测试
L 81     // config.optimization.minimizer('terser').tap((args) => { args[0].terserOptions.compress.drop_console = true; return args })
82
2a61f6 83     // set svg-sprite-loader
L 84     config.module
85       .rule('svg')
86       .exclude.add(resolve('src/icons'))
87       .end()
88     config.module
89       .rule('icons')
90       .test(/\.svg$/)
91       .include.add(resolve('src/icons'))
92       .end()
93       .use('svg-sprite-loader')
94       .loader('svg-sprite-loader')
95       .options({
96         symbolId: 'icon-[name]'
97       })
98       .end()
99
100     config
101       .when(process.env.NODE_ENV !== 'development',
102         config => {
103           config
104             .plugin('ScriptExtHtmlWebpackPlugin')
105             .after('html')
106             .use('script-ext-html-webpack-plugin', [{
107             // `runtime` must same as runtimeChunk name. default is `runtime`
108               inline: /runtime\..*\.js$/
109             }])
110             .end()
111           config
112             .optimization.splitChunks({
113               chunks: 'all',
114               cacheGroups: {
115                 libs: {
116                   name: 'chunk-libs',
117                   test: /[\\/]node_modules[\\/]/,
118                   priority: 10,
119                   chunks: 'initial' // only package third parties that are initially dependent
120                 },
121                 elementUI: {
122                   name: 'chunk-elementUI', // split elementUI into a single package
123                   priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
124                   test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
125                 },
126                 commons: {
127                   name: 'chunk-commons',
128                   test: resolve('src/components'), // can customize your rules
129                   minChunks: 3, //  minimum common number
130                   priority: 5,
131                   reuseExistingChunk: true
132                 }
133               }
134             })
135           // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
136           config.optimization.runtimeChunk('single')
137         }
138       )
139   }
140 }