vue.config.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. 'use strict'
  2. const path = require('path')
  3. const defaultSettings = require('./src/settings.js')
  4. const CompressionWebpackPlugin = require('compression-webpack-plugin')
  5. const setMemeoy = require('./memoryConfig')
  6. const webpack = require('webpack')
  7. function resolve(dir) {
  8. console.log(__dirname)
  9. return path.join(__dirname, dir)
  10. }
  11. const name = defaultSettings.title || '登录系统' // page title
  12. const BASE_URL = process.env.NODE_ENV === 'production' ? '' : 'http://192.168.2.238:1111'
  13. const port = 8031
  14. // const port = process.env.port || process.env.npm_config_port || 9528 // dev port
  15. module.exports = {
  16. /**
  17. * 如果计划在子路径下部署站点,则需要设置publicPath
  18. */
  19. publicPath: './', // 发布路径
  20. // publicPath: '/',//本地路径
  21. outputDir: 'jgpt',
  22. assetsDir: 'static',
  23. // lintOnSave: process.env.NODE_ENV === 'development',
  24. lintOnSave: false,
  25. productionSourceMap: false, // 生产环境是否生成 sourceMap 文件
  26. devServer: {
  27. port: port, // 端口
  28. open: false, // 自动开启浏览器
  29. compress: false, // 开启压缩
  30. overlay: {
  31. warnings: false,
  32. errors: false
  33. },
  34. proxy: {
  35. '/api': {
  36. target: 'http://192.168.2.238:2111', // 后台接口地址
  37. changeOrigin: true,
  38. pathRewrite: {
  39. '^/api': ''
  40. }
  41. },
  42. }
  43. // before: require('./mock/mock-server.js')
  44. },
  45. css: {
  46. // 是否使用css分离插件 ExtractTextPlugin
  47. extract: true,
  48. // 开启 CSS source maps?
  49. // sourceMap: false,
  50. // css预设器配置项
  51. loaderOptions: {
  52. // pass options to sass-loader
  53. sass: {
  54. // 引入全局变量样式,@使我们设置的别名,执行src目录
  55. data: `@import "@/styles/variables.scss";`
  56. }
  57. },
  58. // 启用 CSS modules for all css / pre-processor files.
  59. modules: false
  60. },
  61. configureWebpack: {
  62. name: name,
  63. resolve: {
  64. alias: {
  65. '@': resolve('src'),
  66. staticPub: resolve('public')
  67. }
  68. },
  69. plugins: [
  70. // 压缩代码
  71. new CompressionWebpackPlugin({
  72. filename: '[path].gz[query]',
  73. algorithm: 'gzip',
  74. test: /\.js$|\.html$|\.json$|\.css/,
  75. threshold: 0, // 只有大小大于该值的资源会被处理
  76. minRatio: 0.8, // 只有压缩率小于这个值的资源才会被处理
  77. deleteOriginalAssets: false // 删除原文件
  78. }),
  79. new webpack.ProvidePlugin({
  80. $: 'jquery',
  81. jQuery: 'jquery',
  82. 'windows.jQuery': 'jquery',
  83. 'vue-moment': 'vue-moment',
  84. 'vue-echarts': 'vue-echarts',
  85. xlsx: 'xlsx',
  86. 'xlsx-style': 'xlsx-style',
  87. 'benz-amr-recorder': 'benz-amr-recorder',
  88. html2canvas: 'html2canvas',
  89. html2pdf: 'html2pdf'
  90. })
  91. ]
  92. },
  93. chainWebpack(config) {
  94. // 删除预加载
  95. config.plugins.delete('preload')
  96. config.plugins.delete('prefetch')
  97. // 设置 svg-sprite-loader
  98. config.module
  99. .rule('svg')
  100. .exclude.add(resolve('src/icons'))
  101. .end()
  102. config.module
  103. .rule('icons')
  104. .test(/\.svg$/)
  105. .include.add(resolve('src/icons'))
  106. .end()
  107. .use('svg-sprite-loader')
  108. .loader('svg-sprite-loader')
  109. .options({
  110. symbolId: 'icon-[name]'
  111. })
  112. .end()
  113. // 设置保留空白
  114. config.module
  115. .rule('vue')
  116. .use('vue-loader')
  117. .loader('vue-loader')
  118. .tap((options) => {
  119. options.compilerOptions.preserveWhitespace = true
  120. return options
  121. })
  122. .end()
  123. config.when(process.env.NODE_ENV === 'development', (config) => config.devtool('cheap-source-map'))
  124. config.when(process.env.NODE_ENV !== 'development', (config) => {
  125. config
  126. .plugin('ScriptExtHtmlWebpackPlugin')
  127. .after('html')
  128. .use('script-ext-html-webpack-plugin', [
  129. {
  130. // `runtime`必须与runtimeChunk名称相同。默认值为“runtime”`
  131. inline: /runtime\..*\.js$/
  132. }
  133. ])
  134. .end()
  135. // 分割代码
  136. config.optimization.splitChunks({
  137. chunks: 'all',
  138. cacheGroups: {
  139. libs: {
  140. name: 'chunk-libs',
  141. test: /[\\/]node_modules[\\/]/,
  142. priority: 10,
  143. chunks: 'initial' // 仅打包最初依赖的第三方
  144. },
  145. commons: {
  146. name: 'chunk-commons',
  147. test: resolve('src/components'), // 可以自定义规则
  148. minChunks: 3, // 最小公共数
  149. priority: 5,
  150. reuseExistingChunk: true
  151. },
  152. elementUI: {
  153. name: 'chunk-elementUI', // 将elementUI拆分为一个包
  154. priority: 20, // 重量必须大于libs和app,否则将打包成libs或app
  155. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // 为了适应cnpm
  156. },
  157. monent: {
  158. name: 'chunk-moment',
  159. priority: 20,
  160. test: /[\\/]node_modules[\\/]_?vue-moment(.*)/
  161. },
  162. echarts: {
  163. name: 'chunk-echarts',
  164. priority: 20,
  165. test: /[\\/]node_modules[\\/]_?vue-echarts(.*)/
  166. },
  167. xlsx: {
  168. name: 'chunk-xlsx',
  169. priority: 20,
  170. test: /[\\/]node_modules[\\/]_?xlsx(.*)/
  171. },
  172. 'xlsx-style': {
  173. name: 'chunk-xlsx-style',
  174. priority: 20,
  175. test: /[\\/]node_modules[\\/]_?xlsx-style(.*)/
  176. },
  177. 'benz-amr-recorder': {
  178. name: 'chunk-amr',
  179. priority: 20,
  180. test: /[\\/]node_modules[\\/]_?benz-amr-recorder(.*)/
  181. },
  182. html2canvas: {
  183. name: 'chunk-html2canvas',
  184. priority: 20,
  185. test: /[\\/]node_modules[\\/]_?html2canvas(.*)/
  186. },
  187. html2pdf: {
  188. name: 'chunk-html2pdf',
  189. priority: 20,
  190. test: /[\\/]node_modules[\\/]_?html2pdf(.*)/
  191. }
  192. }
  193. })
  194. config.optimization.runtimeChunk('single')
  195. })
  196. config.externals({ './cptable': 'var cptable' })
  197. setMemeoy(config)
  198. }
  199. }