vue.config.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * @Author: smy
  3. * @Description:
  4. * @Date: 2022-06-20 16:00:18
  5. * @LastEditTime: 2022-12-14 10:41:43
  6. * @FilePath: /AutomationPlatformFront/vue.config.js
  7. */
  8. /* eslint-disable @typescript-eslint/no-var-requires */
  9. const path = require('path')
  10. const webpack = require('webpack')
  11. function resolve(dir) {
  12. return path.join(__dirname, dir)
  13. }
  14. const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
  15. const CompressionPlugin = require('compression-webpack-plugin')
  16. const _mode = process.env.NODE_ENV
  17. const server = _mode === 'development' ? 'http://222.128.14.106:11179/api' : 'test' ? '/test-api' : '/api'
  18. const defaultSettings = require('./src/setting')
  19. const name = defaultSettings.projectTitle || 'testDemo' // page title
  20. module.exports = {
  21. lintOnSave: false,
  22. productionSourceMap: false,
  23. assetsDir: 'static',
  24. // 生产环境,项目部署在服务器子目录cms下,用于查找静态资源
  25. publicPath: '/',
  26. devServer: {
  27. proxy: {
  28. '/dev-api': {
  29. target:server,
  30. changeOrigin: true,
  31. pathRewrite: {
  32. '^/dev-api': '',
  33. },
  34. },
  35. },
  36. open: true, // 配置自动启动浏览器
  37. // disableHostCheck: true, //webpack4.0 开启热更新
  38. },
  39. css: {
  40. // extract: true, // 解决开发模式,打包时未提取CSS的问题 (开发模式先注释)
  41. loaderOptions: {
  42. less: {
  43. lessOptions: {
  44. modifyVars: {
  45. 'primary-color': '#3d62ff', // 修改全局主颜色
  46. },
  47. javascriptEnabled: true,
  48. },
  49. },
  50. },
  51. },
  52. configureWebpack: (config) => {
  53. config.name = name
  54. config.resolve = {
  55. ...config.resolve,
  56. alias: {
  57. '@': resolve('src'),
  58. 'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js', // 解决国际化预警
  59. },
  60. }
  61. config.module.rules.push({
  62. test: path.resolve(__dirname, 'node_modules/leader-line/'),
  63. use: [
  64. {
  65. loader: 'skeleton-loader',
  66. options: {
  67. procedure: (content) => `${content}export default LeaderLine`,
  68. },
  69. },
  70. ],
  71. })
  72. config.plugins.push(
  73. new webpack.ProvidePlugin({
  74. $: 'jquery',
  75. jQuery: 'jquery',
  76. 'windows.jQuery': 'jquery',
  77. })
  78. )
  79. // // 对生产环境的配置
  80. if (process.env.NODE_ENV === 'production') {
  81. const plugns = [
  82. // 去除console.log
  83. new UglifyJsPlugin(),
  84. // 代码压缩
  85. // new CompressionPlugin({
  86. // test: /\.(js|css)$/,
  87. // threshold: 10240, // 小于10KB就不进行压缩
  88. // minRatio: 0.8, // 压缩比率
  89. // deleteOriginalAssets: true, // 删除源文件
  90. // }),
  91. ]
  92. config.plugins.push(...plugns)
  93. }
  94. },
  95. runtimeCompiler: true, // 解决菜单栏 template 模版编译问题
  96. chainWebpack: (config) => {
  97. // 优化首屏加载速度--移除 preload & prefetch 插件
  98. config.plugins.delete('preload')
  99. config.plugins.delete('prefetch')
  100. // 解决 isCustomElement 预警
  101. config.module
  102. .rule('vue')
  103. .use('vue-loader')
  104. .tap((options) => {
  105. options.compilerOptions = {
  106. ...options.compilerOptions,
  107. isCustomElement: (tag) => tag.startsWith('svg-'),
  108. }
  109. return options
  110. })
  111. // 配置识别 svg 规则
  112. const svgRule = config.module.rule('svg')
  113. svgRule.uses.clear()
  114. svgRule
  115. .test(/\.svg$/)
  116. .include.add(resolve('src/icons'))
  117. .end()
  118. .use('svg-sprite-loader')
  119. .loader('svg-sprite-loader')
  120. .options({
  121. symbolId: 'icon-[name]',
  122. })
  123. const fileRule = config.module.rule('file')
  124. fileRule.uses.clear()
  125. fileRule
  126. .test(/\.svg$/)
  127. .exclude.add(resolve('src/icons'))
  128. .end()
  129. .use('file-loader')
  130. .loader('file-loader')
  131. config.when(_mode === 'production', (config) => {
  132. config.optimization.splitChunks({
  133. chunks: 'all',
  134. cacheGroups: {
  135. libs: {
  136. name: 'chunk-libs',
  137. test: /[\\/]node_modules[\\/]/,
  138. priority: 10,
  139. chunks: 'initial', // only package third parties that are initially dependent
  140. },
  141. elementUI: {
  142. name: 'chunk-elementUI', // split elementUI into a single package
  143. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  144. test: /[\\/]node_modules[\\/]_?ant-design-vue(.*)/, // in order to adapt to cnpm
  145. },
  146. commons: {
  147. name: 'chunk-commons',
  148. test: resolve('src/components'),
  149. minChunks: 3,
  150. priority: 5,
  151. reuseExistingChunk: true, // 表示是否使用已有的chunk,如果为true,则表示如果当前的 chunk 包含的模块已经被抽取出去了,那么将不会重新生成新的。
  152. },
  153. },
  154. })
  155. // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
  156. config.optimization.runtimeChunk('single')
  157. })
  158. },
  159. }