vue.config.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. 'use strict'
  2. const path = require('path')
  3. const ip = '192.168.1.43'
  4. const os = require('os')
  5. const fs = require("fs")
  6. //判断环境
  7. let ifaces = os.networkInterfaces()
  8. let localityIp = '', result = []
  9. for(let dev in ifaces) {
  10. ifaces[dev].forEach(function(details) {
  11. if(localityIp === '' && details.family === 'IPv4' && !details.internal) {
  12. localityIp = details.address
  13. return;
  14. }
  15. })
  16. }
  17. let getFile = localityIp == ip?false:true
  18. /******** 获取git版本信息 ********/
  19. const gitHEAD = getFile ? fs.readFileSync('.git/HEAD', 'utf-8').trim() : false
  20. const ref = getFile ? gitHEAD.split(': ')[1] : false
  21. const develop = getFile ? gitHEAD.split('/')[2] : false
  22. const gitVersion = getFile ? fs.readFileSync('.git/' + ref, 'utf-8').trim() : false
  23. const buildEdition = develop && gitVersion ? develop + ': ' + gitVersion : '未配置'
  24. const myDate = new Date()
  25. const buildTime = myDate.getFullYear() +'-'+ (myDate.getMonth()+1) +'-'+ myDate.getDate() +' '+ myDate.getHours() +':'+ myDate.getMinutes();
  26. const logsList = getFile ? fs.readFileSync('.git/logs/HEAD', 'utf-8').replace(/\r\n|\r/g, "\n").split("\n") : false
  27. const buildUser = getFile ? logsList[logsList.length-2].split(" ")[2] : '未配置'
  28. //生成随机版本编码
  29. process.env.VUE_APP_RENEWAL_ENCODING = generateRandomString()
  30. function generateRandomString() {
  31. let chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  32. let randomString = "";
  33. for (let i = 0; i < 24; i++) {
  34. let randomIndex = Math.floor(Math.random() * chars.length);
  35. randomString += chars.charAt(randomIndex);
  36. }
  37. return randomString;
  38. }
  39. const { defineConfig } = require('@vue/cli-service')
  40. module.exports = defineConfig({
  41. transpileDependencies: true,
  42. lintOnSave:false,
  43. publicPath: "./", // 公共路径(必须有的)
  44. outputDir: "bigData", // 输出文件目录
  45. assetsDir: "static", //静态资源文件名称
  46. productionSourceMap: false, //去除打包后js的map文件
  47. devServer: {
  48. host: "localhost", // 默认是localhost
  49. port: 8081, // 前端项目编译后使用的端口号,跟webpack配置的port同理
  50. open:true,
  51. proxy: {
  52. '/api': {
  53. target: `http://localhost:8081`, // 实际跨域请求的API地址
  54. changeOrigin: true, // 跨域
  55. // 请求地址重写 http://front-end/api/login ⇒ http://api-url/login
  56. pathRewrite: {
  57. ['^' + process.env.VUE_APP_BASE_API]: ''
  58. }
  59. }
  60. },
  61. },
  62. chainWebpack(config) {
  63. //标记打包时间与当前git版本信息
  64. config.plugin('html').tap(args => {
  65. args[0].buildUser = buildUser;
  66. args[0].buildTime = buildTime;
  67. args[0].buildEdition = buildEdition;
  68. args[0].renewalEncoding = process.env.VUE_APP_RENEWAL_ENCODING;
  69. // args[0].minify.removeComments = false
  70. return args
  71. })
  72. config
  73. .when(process.env.VUE_APP_ENV !== 'www.dlc.com',
  74. config => {
  75. config
  76. .plugin('ScriptExtHtmlWebpackPlugin')
  77. .after('html')
  78. .use('script-ext-html-webpack-plugin', [{
  79. // `runtime` must same as runtimeChunk name. default is `runtime`
  80. inline: /runtime\..*\.js$/
  81. }])
  82. .end()
  83. config
  84. .optimization.splitChunks({
  85. chunks: 'all',
  86. minSize: 50000, //构建出来的chunk大于30000才会被分割
  87. maxSize: 100000, //会尝试根据这个大小进行代码分割
  88. })
  89. }
  90. )
  91. },
  92. })