vue.config.js 5.2 KB

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