vue.config.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. 'use strict'
  2. const path = require('path')
  3. function resolve(dir) {
  4. return path.join(__dirname, dir)
  5. }
  6. function getNowDate() {
  7. const date = new Date()
  8. const year = String(date.getFullYear())
  9. const month = String(date.getMonth() + 1)
  10. const day = String(date.getDate())
  11. return year + month + day
  12. }
  13. const nowDate = getNowDate()
  14. const Timestamp = new Date().getTime()
  15. const params = nowDate + '-' + Timestamp
  16. module.exports = {
  17. publicPath: '/vuepay', //部署路径后缀
  18. assetsDir: 'static', // 放置生成的静态资源 (js、css、img、fonts) 的 (相对于 outputDir 的) 目录。
  19. indexPath: 'index.html', // 指定生成的 index.html 的输出路径 (相对于 outputDir)。也可以是一个绝对路径。
  20. filenameHashing: true, // 文件名hash,默认
  21. lintOnSave: false, // 开发环境下通过 eslint-loader 在每次保存时 lint 代码
  22. productionSourceMap: false, // 关闭生产环境的 source map
  23. devServer: {
  24. overlay: {
  25. // 让浏览器 overlay 同时显示警告和错误
  26. warnings: true,
  27. errors: true
  28. },
  29. //host: '172.16.120.200',
  30. host: '0.0.0.0',
  31. port: 8080, // 端口号
  32. // https: false, // https:{type:Boolean}
  33. open: true, // 配置自动启动浏览器
  34. hotOnly: true, // 热更新
  35. proxy: {
  36. // 配置多个跨域
  37. '/': {
  38. target: 'https://smartpark.caih.com/dxapi/', //东信
  39. //target: 'https://smartpark.caih.com/dxapi/', //本地
  40. changeOrigin: false,
  41. pathRewrite: {
  42. '': '/'
  43. }
  44. }
  45. },
  46. headers: {
  47. 'Access-Control-Allow-Origin': '*',
  48. }
  49. },
  50. configureWebpack: {
  51. performance: {
  52. hints: 'warning',
  53. // 入口起点的最大体积
  54. maxEntrypointSize: 50000000,
  55. // 生成文件的最大体积
  56. maxAssetSize: 30000000,
  57. // 只给出 js 文件的性能提示
  58. assetFilter: function(assetFilename) {
  59. return assetFilename.endsWith('.js')
  60. }
  61. },
  62. output: { // 输出重构 打包编译后的 文件名称 【模块名称.版本号.时间戳】
  63. filename: `js/[name].${params}.js`,
  64. chunkFilename: `js/[name].${params}.js`
  65. },
  66. },
  67. css: {
  68. loaderOptions: {
  69. postcss: {
  70. plugins: [
  71. require('postcss-px-to-viewport')({
  72. unitToConvert: 'px', // 需要转换的单位,默认为"px"
  73. viewportWidth: 375, // 设计稿的视口宽度
  74. unitPrecision: 5, // 单位转换后保留的精度
  75. propList: ['*'], // 能转化为vw的属性列表
  76. viewportUnit: 'vw', // 希望使用的视口单位
  77. fontViewportUnit: 'vw', // 字体使用的视口单位
  78. selectorBlackList: [], // 需要忽略的CSS选择器
  79. minPixelValue: 1, // 最小的转换数值,如果为1的话,只有大于1的值会被转换
  80. mediaQuery: false, // 媒体查询里的单位是否需要转换单位
  81. replace: true, // 是否直接更换属性值,而不添加备用属性
  82. exclude: /node_modules/, // 忽略某些文件夹下的文件或特定文件
  83. include: undefined, // 如果设置了include,那将只有匹配到的文件才会被转换,例如只转换 'src/mobile' 下的文件 (include: /\/src\/mobile\//)
  84. landscape: false, // 是否添加根据 landscapeWidth 生成的媒体查询条件 @media (orientation: landscape)
  85. landscapeUnit: 'vw', // 横屏时使用的单位
  86. landscapeWidth: 667 // 横屏时使用的视口宽度
  87. })
  88. ]
  89. }
  90. },
  91. extract: {
  92. filename: `static/css/[name].${params}.css`,
  93. chunkFilename: `static/css/[name].${params}.css`
  94. }
  95. },
  96. chainWebpack(config) {
  97. // it can improve the speed of the first screen, it is recommended to turn on preload
  98. config.plugin('preload').tap(() => [{
  99. rel: 'preload',
  100. // to ignore runtime.js
  101. // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
  102. fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
  103. include: 'initial'
  104. }])
  105. // when there are many pages, it will cause too many meaningless requests
  106. config.plugins.delete('prefetch')
  107. // set svg-sprite-loader
  108. config.module
  109. .rule('svg')
  110. .exclude.add(resolve('src/assets/icons'))
  111. .end()
  112. config.module
  113. .rule('icons')
  114. .test(/\.svg$/)
  115. .include.add(resolve('src/assets/icons'))
  116. .end()
  117. .use('svg-sprite-loader')
  118. .loader('svg-sprite-loader')
  119. .options({
  120. symbolId: 'icon-[name]'
  121. })
  122. .end()
  123. config
  124. .when(process.env.NODE_ENV !== 'development',
  125. config => {
  126. config
  127. .plugin('ScriptExtHtmlWebpackPlugin')
  128. .after('html')
  129. .use('script-ext-html-webpack-plugin', [{
  130. // `runtime` must same as runtimeChunk name. default is `runtime`
  131. inline: /runtime\..*\.js$/
  132. }])
  133. .end()
  134. config
  135. .optimization.splitChunks({
  136. chunks: 'all',
  137. cacheGroups: {
  138. libs: {
  139. name: 'chunk-libs',
  140. test: /[\\/]node_modules[\\/]/,
  141. priority: 10,
  142. chunks: 'initial' // only package third parties that are initially dependent
  143. },
  144. elementUI: {
  145. name: 'chunk-elementUI', // split elementUI into a single package
  146. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  147. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  148. },
  149. commons: {
  150. name: 'chunk-commons',
  151. test: resolve('src/components'), // can customize your rules
  152. minChunks: 3, // minimum common number
  153. priority: 5,
  154. reuseExistingChunk: true
  155. }
  156. }
  157. })
  158. // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
  159. config.optimization.runtimeChunk('single')
  160. }
  161. )
  162. }
  163. };