vue.config.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. const path = require('path')
  2. const defaultSettings = require('./src/settings.js')
  3. const CompressionPlugin = require('compression-webpack-plugin')
  4. const { RetryChunkLoadPlugin } = require('webpack-retry-chunk-load-plugin')
  5. const Timestamp = new Date().getTime()
  6. function resolve(dir) {
  7. return path.join(__dirname, dir)
  8. }
  9. const name = defaultSettings.title || 'vue Element Admin' // page title
  10. // If your port is set to 80,
  11. // use administrator privileges to execute the command line.
  12. // For example, Mac: sudo npm run
  13. // You can change the port by the following method:
  14. // port = 9527 npm run dev OR npm run dev --port = 9527
  15. const port = process.env.port || process.env.npm_config_port || 9527 // dev port
  16. // All configuration item explanations can be find in https://cli.vuejs.org/config/
  17. module.exports = {
  18. /**
  19. * You will need to set publicPath if you plan to deploy your site under a sub path,
  20. * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
  21. * then publicPath should be set to "/bar/".
  22. * In most cases please use '/' !!!
  23. * Detail: https://cli.vuejs.org/config/#publicpath
  24. */
  25. publicPath: process.env.VUE_APP_PACKAGE_PATH || '/',
  26. outputDir: 'dist',
  27. assetsDir: 'static',
  28. lintOnSave: process.env.VUE_APP_MODE === 'development',
  29. productionSourceMap: false,
  30. devServer: {
  31. port: port,
  32. hot: true,
  33. open: false,
  34. proxy: {
  35. 'api56/open': {
  36. target: 'http://gatewayservicev2.fujica.com.cn/open',
  37. changeOrigin: true,
  38. pathRewrite: {
  39. '^/api56/open': ''
  40. }
  41. },
  42. // change xxx-api/login => mock/login
  43. // detail: https://cli.vuejs.org/config/#devserver-proxy
  44. 'api56': {
  45. target: process.env.VUE_APP_BASE_API,
  46. changeOrigin: true,
  47. pathRewrite: {
  48. '^/api56': ''
  49. }
  50. },
  51. 'api11': {
  52. target: process.env.VUE_APP_BASE_API,
  53. // target: 'http://119.23.214.109:32011',
  54. changeOrigin: true,
  55. pathRewrite: {
  56. '^/api11': ''
  57. }
  58. }
  59. },
  60. // history模式下的url会请求到服务器端,但是服务器端并没有这一个资源文件,就会返回404,所以需要配置这一项
  61. // historyApiFallback: {
  62. // index: '/index.html' // 与output的publicPath
  63. // },
  64. disableHostCheck: true
  65. // after: require('./mock/mock-server.js')
  66. },
  67. configureWebpack: {
  68. // provide the app's title in webpack's name field, so that
  69. // it can be accessed in index.html to inject the correct title.
  70. name: name,
  71. resolve: {
  72. alias: {
  73. '@': resolve('src')
  74. }
  75. },
  76. // 打包时忽略以下文件
  77. externals: {
  78. 'vue': 'Vue',
  79. 'element-ui': 'Element'
  80. },
  81. output: { // 输出重构 打包编译后的 文件名称 【模块名称.时间戳】
  82. 'filename': `[name].${Timestamp}.js`,
  83. 'chunkFilename': `[name].${Timestamp}.js`
  84. },
  85. performance: {
  86. hints: 'warning',
  87. // 入口起点的最大体积
  88. maxEntrypointSize: 50000000,
  89. // 生成文件的最大体积
  90. maxAssetSize: 50000000,
  91. // 只给出 js 文件的性能提示
  92. assetFilter: function(assetFilename) {
  93. return assetFilename.endsWith('.js')
  94. }
  95. },
  96. plugins: [
  97. new RetryChunkLoadPlugin({
  98. cacheBust: `function() {
  99. return Date.now();
  100. }`,
  101. retryDelay: 3000,
  102. maxRetries: 2,
  103. // chunks: ['chunks.**.**.js'],
  104. lastResortScript: 'window.location.reload(true);'
  105. })
  106. ]
  107. },
  108. chainWebpack(config) {
  109. // if (process.env.VUE_APP_MODE === 'production') {
  110. // // 清除css,js版本号
  111. // config.output.filename('static/js/[name].v1.11.js').end()
  112. // config.output.chunkFilename('static/js/[name].v1.11.js').end()
  113. // // 为生产环境修改配置...
  114. // config.plugin('extract-css').tap(args => [{
  115. // filename: `static/css/[name].v1.11.css`,
  116. // chunkFilename: `static/css/[name].v1.11.css`
  117. // }])
  118. // } else {
  119. // config.output.filename(`static/js/[name].${Timestamp}.js`).end()
  120. // config.output.chunkFilename(`static/js/[name].${Timestamp}.js`).end()
  121. // }
  122. config.plugins.delete('prefetch') // TODO: need test
  123. // 可视化显示包的大小
  124. // config
  125. // .plugin('webpack-bundle-analyzer')
  126. // .use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin)
  127. // 开启gzip压缩,加快访问速度
  128. // 开发环境下不使用压缩
  129. config
  130. .when(process.env.VUE_APP_MODE !== 'development', config => {
  131. config.plugin('compression')
  132. .use(CompressionPlugin, [{
  133. algorithm: 'gzip',
  134. test: /\.js$|\.html$|\.css/,
  135. threshold: 10240,
  136. deleteOriginalAssets: false
  137. }])
  138. .end()
  139. })
  140. // config.plugin('compression')
  141. // .use(CompressionPlugin, [{
  142. // algorithm: 'gzip',
  143. // test: /\.js$|\.html$|\.css/,
  144. // threshold: 10240,
  145. // deleteOriginalAssets: false
  146. // }])
  147. // .end()
  148. // set svg-sprite-loader
  149. config.module
  150. .rule('svg')
  151. .exclude.add(resolve('src/icons'))
  152. .end()
  153. config.module
  154. .rule('icons')
  155. .test(/\.svg$/)
  156. .include.add(resolve('src/icons'))
  157. .end()
  158. .use('svg-sprite-loader')
  159. .loader('svg-sprite-loader')
  160. .options({
  161. symbolId: 'icon-[name]'
  162. })
  163. .end()
  164. // set preserveWhitespace
  165. config.module
  166. .rule('vue')
  167. .exclude.add(resolve('src/views/V1'))
  168. .end()
  169. .use('vue-loader')
  170. .loader('vue-loader')
  171. .tap(options => {
  172. options.compilerOptions.preserveWhitespace = true
  173. return options
  174. })
  175. .end()
  176. // config
  177. // // 开发环境下打包生产map文件,其他环境不生成
  178. // .when(process.env.VUE_APP_MODE === 'development',
  179. // config => config.devtool('cheap-source-map')
  180. // )
  181. config
  182. .when(process.env.VUE_APP_MODE !== 'development',
  183. config => {
  184. config
  185. .plugin('ScriptExtHtmlWebpackPlugin')
  186. .after('html')
  187. .use('script-ext-html-webpack-plugin', [{
  188. // `runtime` must same as runtimeChunk name. default is `runtime`
  189. // inline: /runtime\..*\.js$/
  190. }])
  191. .end()
  192. config
  193. .optimization.splitChunks({
  194. chunks: 'all',
  195. cacheGroups: {
  196. libs: {
  197. name: 'chunk-libs',
  198. test: /[\\/]node_modules[\\/]/,
  199. priority: 10,
  200. chunks: 'initial' // only package third parties that are initially dependent
  201. },
  202. elementUI: {
  203. name: 'chunk-elementUI', // split elementUI into a single package
  204. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  205. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  206. },
  207. commons: {
  208. name: 'chunk-commons',
  209. test: resolve('src/components'), // can customize your rules
  210. minChunks: 3, // minimum common number
  211. priority: 5,
  212. reuseExistingChunk: true
  213. }
  214. }
  215. })
  216. config.optimization.runtimeChunk('single')
  217. }
  218. )
  219. }
  220. }