blob: 19b8cb86f979baf0948190db8d58760470f2dc4a [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.
57 name: name,
58 resolve: {
59 alias: {
60 '@': resolve('src')
61 }
62 }
63 },
64 chainWebpack(config) {
65 // it can improve the speed of the first screen, it is recommended to turn on preload
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
80 // set svg-sprite-loader
81 config.module
82 .rule('svg')
83 .exclude.add(resolve('src/icons'))
84 .end()
85 config.module
86 .rule('icons')
87 .test(/\.svg$/)
88 .include.add(resolve('src/icons'))
89 .end()
90 .use('svg-sprite-loader')
91 .loader('svg-sprite-loader')
92 .options({
93 symbolId: 'icon-[name]'
94 })
95 .end()
96
97 config
98 .when(process.env.NODE_ENV !== 'development',
99 config => {
100 config
101 .plugin('ScriptExtHtmlWebpackPlugin')
102 .after('html')
103 .use('script-ext-html-webpack-plugin', [{
104 // `runtime` must same as runtimeChunk name. default is `runtime`
105 inline: /runtime\..*\.js$/
106 }])
107 .end()
108 config
109 .optimization.splitChunks({
110 chunks: 'all',
111 cacheGroups: {
112 libs: {
113 name: 'chunk-libs',
114 test: /[\\/]node_modules[\\/]/,
115 priority: 10,
116 chunks: 'initial' // only package third parties that are initially dependent
117 },
118 elementUI: {
119 name: 'chunk-elementUI', // split elementUI into a single package
120 priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
121 test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
122 },
123 commons: {
124 name: 'chunk-commons',
125 test: resolve('src/components'), // can customize your rules
126 minChunks: 3, // minimum common number
127 priority: 5,
128 reuseExistingChunk: true
129 }
130 }
131 })
132 // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
133 config.optimization.runtimeChunk('single')
134 }
135 )
136 }
137}