vue.config.js 5.1 KB

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