blob: 33a634836ee90b0ef4c26b005efdc5412dd89b01 [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
17
18// All configuration item explanations can be find in https://cli.vuejs.org/config/
19module.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 port: port,
34 open: true,
35 overlay: {
36 warnings: false,
37 errors: true
38 },
39 before: require('./mock/mock-server.js')
40 },
41 configureWebpack: {
42 // provide the app's title in webpack's name field, so that
43 // it can be accessed in index.html to inject the correct title.
44 name: name,
45 resolve: {
46 alias: {
47 '@': resolve('src')
48 }
49 }
50 },
51 chainWebpack(config) {
52 // it can improve the speed of the first screen, it is recommended to turn on preload
53 // it can improve the speed of the first screen, it is recommended to turn on preload
54 config.plugin('preload').tap(() => [
55 {
56 rel: 'preload',
57 // to ignore runtime.js
58 // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
59 fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
60 include: 'initial'
61 }
62 ])
63
64 // when there are many pages, it will cause too many meaningless requests
65 config.plugins.delete('prefetch')
66
67 // set svg-sprite-loader
68 config.module
69 .rule('svg')
70 .exclude.add(resolve('src/icons'))
71 .end()
72 config.module
73 .rule('icons')
74 .test(/\.svg$/)
75 .include.add(resolve('src/icons'))
76 .end()
77 .use('svg-sprite-loader')
78 .loader('svg-sprite-loader')
79 .options({
80 symbolId: 'icon-[name]'
81 })
82 .end()
83
84 config
85 .when(process.env.NODE_ENV !== 'development',
86 config => {
87 config
88 .plugin('ScriptExtHtmlWebpackPlugin')
89 .after('html')
90 .use('script-ext-html-webpack-plugin', [{
91 // `runtime` must same as runtimeChunk name. default is `runtime`
92 inline: /runtime\..*\.js$/
93 }])
94 .end()
95 config
96 .optimization.splitChunks({
97 chunks: 'all',
98 cacheGroups: {
99 libs: {
100 name: 'chunk-libs',
101 test: /[\\/]node_modules[\\/]/,
102 priority: 10,
103 chunks: 'initial' // only package third parties that are initially dependent
104 },
105 elementUI: {
106 name: 'chunk-elementUI', // split elementUI into a single package
107 priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
108 test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
109 },
110 commons: {
111 name: 'chunk-commons',
112 test: resolve('src/components'), // can customize your rules
113 minChunks: 3, // minimum common number
114 priority: 5,
115 reuseExistingChunk: true
116 }
117 }
118 })
119 // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
120 config.optimization.runtimeChunk('single')
121 }
122 )
123 }
124}