blob: db6370aaac403e2eb5930a06ebff5074c7b075a0 [file] [log] [blame]
sijun.li96462302020-07-24 10:08:05 +08001'use strict'
2const path = require('path')
3const defaultSettings = require('./src/settings.js')
4
5function resolve(dir) {
6 return path.join(__dirname, dir)
7}
8
9const name = defaultSettings.title || 'vue Element Admin' // 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 method:
15// port = 9527 npm run dev OR npm run dev --port = 9527
16const port = process.env.port || process.env.npm_config_port || 9527 // dev port
sijun.li7531fd12020-09-10 14:37:35 +080017var publicPath = 'portal'
18if (process.env.NODE_ENV === 'development') {
19 publicPath = '/portal'
20} else if (process.env.NODE_ENV === 'production') {
21 publicPath = '/portal/pages'
22}
sijun.li96462302020-07-24 10:08:05 +080023// All configuration item explanations can be find in https://cli.vuejs.org/config/
24module.exports = {
25 /**
26 * You will need to set publicPath if you plan to deploy your site under a sub path,
27 * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
28 * then publicPath should be set to "/bar/".
29 * In most cases please use '/' !!!
30 * Detail: https://cli.vuejs.org/config/#publicpath
31 */
sijun.li7531fd12020-09-10 14:37:35 +080032 publicPath: publicPath,
sijun.li96462302020-07-24 10:08:05 +080033 outputDir: 'dist',
34 assetsDir: 'static',
35 lintOnSave: process.env.NODE_ENV === 'development',
36 productionSourceMap: false,
37 devServer: {
38 port: port,
39 open: true,
40 overlay: {
41 warnings: false,
42 errors: true
43 },
sijun.li2c3a34f2020-08-17 15:18:32 +080044 proxy: {
45 [process.env.VUE_APP_BASE_API]: {
46 target: process.env.VUE_APP_BASE_API,
47 changeOrigin: true, // 配置跨域
48 pathRewrite: {
49 ['^' + process.env.VUE_APP_BASE_API]: ''
50 }
51 }
52 }
sijun.li96462302020-07-24 10:08:05 +080053 },
54 configureWebpack: {
55 // provide the app's title in webpack's name field, so that
56 // it can be accessed in index.html to inject the correct title.
sijun.li84ad8b72020-09-29 09:53:03 +080057 externals: {
58 AMap: 'window.AMap'
59 },
sijun.li96462302020-07-24 10:08:05 +080060 name: name,
61 resolve: {
62 alias: {
63 '@': resolve('src')
64 }
65 }
66 },
67 chainWebpack(config) {
68 // it can improve the speed of the first screen, it is recommended to turn on preload
69 // it can improve the speed of the first screen, it is recommended to turn on preload
70 config.plugin('preload').tap(() => [
71 {
72 rel: 'preload',
73 // to ignore runtime.js
74 // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
75 fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
76 include: 'initial'
77 }
78 ])
79
80 // when there are many pages, it will cause too many meaningless requests
81 config.plugins.delete('prefetch')
82
83 // set svg-sprite-loader
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}