vue.config.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. 'use strict'
  2. const path = require('path')
  3. const defaultSettings = require('./src/settings.js')
  4. const { APIURl } = require('./src/utils/apiUrl')
  5. const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin')
  6. function resolve(dir) {
  7. return path.join(__dirname, dir)
  8. }
  9. const name = defaultSettings.title || '客户服务360系统' // 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 = 3000 npm run dev OR npm run dev --port = 3000
  15. const port = process.env.port || process.env.npm_config_port || 3000 // 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: '/',
  26. outputDir: 'dist',
  27. assetsDir: 'static',
  28. // lintOnSave: process.env.NODE_ENV === 'development',
  29. lintOnSave: false,
  30. productionSourceMap: false,
  31. devServer: {
  32. port: port,
  33. open: false,
  34. disableHostCheck: true,
  35. overlay: {
  36. warnings: false,
  37. errors: true
  38. },
  39. // before: require('./mock/mock-server.js'),
  40. // 接口转发
  41. proxy: {
  42. '/dev': {
  43. target: APIURl,
  44. changeOrigin: true,
  45. pathRewrite: {
  46. '^/dev': ''
  47. }
  48. }
  49. }
  50. },
  51. configureWebpack: {
  52. // provide the app's title in webpack's name field, so that
  53. // it can be accessed in index.html to inject the correct title.
  54. name: name,
  55. resolve: {
  56. alias: {
  57. '@': resolve('src'),
  58. 'static': resolve('static') // 增加这一行代码
  59. }
  60. },
  61. plugins: [
  62. new MonacoWebpackPlugin({
  63. // available options are documented at https://github.com/Microsoft/monaco-editor-webpack-plugin#options
  64. languages: ['javascript', 'css', 'html', 'typescript', 'json', 'java', 'sql']
  65. })
  66. ]
  67. },
  68. chainWebpack(config) {
  69. config.externals({
  70. // 'monaco-editor': 'monaco-editor',
  71. 'echarts': 'echarts'
  72. })
  73. // it can improve the speed of the first screen, it is recommended to turn on preload
  74. config.plugin('preload').tap(() => [{
  75. rel: 'preload',
  76. // to ignore runtime.js
  77. // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
  78. fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
  79. include: 'initial'
  80. }])
  81. // when there are many pages, it will cause too many meaningless requests
  82. config.plugins.delete('prefetch')
  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. config
  100. // https://webpack.js.org/configuration/devtool/#development
  101. .when(process.env.NODE_ENV === 'development',
  102. config => config.devtool('cheap-source-map')
  103. )
  104. config
  105. .when(process.env.NODE_ENV !== 'development',
  106. config => {
  107. config
  108. .plugin('ScriptExtHtmlWebpackPlugin')
  109. .after('html')
  110. .use('script-ext-html-webpack-plugin', [{
  111. // `runtime` must same as runtimeChunk name. default is `runtime`
  112. inline: /runtime\..*\.js$/
  113. }])
  114. .end()
  115. config
  116. .optimization.splitChunks({
  117. chunks: 'all',
  118. cacheGroups: {
  119. libs: {
  120. name: 'chunk-libs',
  121. test: /[\\/]node_modules[\\/]/,
  122. priority: 10,
  123. chunks: 'initial' // only package third parties that are initially dependent
  124. },
  125. elementUI: {
  126. name: 'chunk-elementUI', // split elementUI into a single package
  127. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  128. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  129. },
  130. commons: {
  131. name: 'chunk-commons',
  132. test: resolve('src/components'), // can customize your rules
  133. minChunks: 3, // minimum common number
  134. priority: 5,
  135. reuseExistingChunk: true
  136. }
  137. }
  138. })
  139. config.optimization.runtimeChunk('single')
  140. }
  141. )
  142. }
  143. }