vue.config.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // 读取 manifest.json ,修改后重新写入
  2. const {
  3. log
  4. } = require('console')
  5. const fs = require('fs')
  6. const package_json = require('./package.json')
  7. const manifestPath = `${__dirname}/manifest.json`
  8. const pagePath = `${__dirname}/pages.json`
  9. const scssPath = `${__dirname}/uni.scss`
  10. let Manifest = fs.readFileSync(manifestPath, {
  11. encoding: 'utf-8'
  12. })
  13. let Pagejson = fs.readFileSync(pagePath, {
  14. encoding: 'utf-8'
  15. })
  16. let Scss = fs.readFileSync(scssPath, {
  17. encoding: 'utf-8'
  18. })
  19. function replaceManifest(path, value) { // 修改 manifest.json 的配置
  20. const arr = path.split('.')
  21. const len = arr.length
  22. const lastItem = arr[len - 1]
  23. let i = 0
  24. let ManifestArr = Manifest.split(/\n/)
  25. for (let index = 0; index < ManifestArr.length; index++) {
  26. const item = ManifestArr[index]
  27. if (new RegExp(`"${arr[i]}"`).test(item)) ++i;
  28. if (i === len) {
  29. const hasComma = /,/.test(item)
  30. ManifestArr[index] = item.replace(new RegExp(`"${lastItem}"[\\s\\S]*:[\\s\\S]*`), `"${lastItem}": ${value}${hasComma ? ',' : ''}`)
  31. break;
  32. }
  33. }
  34. Manifest = ManifestArr.join('\n')
  35. }
  36. function replacePagejson(path, value) { // 修改 pages.json 的配置
  37. const arr = path.split('.')
  38. const len = arr.length
  39. const lastItem = arr[len - 1]
  40. let i = 0
  41. let PagejsonArr = Pagejson.split(/\n/)
  42. for (let index = 0; index < PagejsonArr.length; index++) {
  43. const item = PagejsonArr[index]
  44. if (new RegExp(`"${arr[i]}"`).test(item)) ++i;
  45. if (i === len) {
  46. const hasComma = /,/.test(item)
  47. PagejsonArr[index] = item.replace(new RegExp(`"${lastItem}"[\\s\\S]*:[\\s\\S]*`), `"${lastItem}": ${value}${hasComma ? ',' : ''}`)
  48. break;
  49. }
  50. }
  51. Pagejson = PagejsonArr.join('\n')
  52. }
  53. function replaceScssPath(path, value) { // 修改 uni.scss.json 的配置
  54. const arr = path.split('.')
  55. const len = arr.length
  56. const lastItem = arr[len - 1]
  57. let i = 0
  58. let ScssArr = Scss.split(/\n/)
  59. for (let index = 0; index < ScssArr.length; index++) {
  60. const item = ScssArr[index]
  61. const hasComma = /,/.test(item)
  62. const reg = new RegExp(`${lastItem}[\\s\\S]*:[\\s\\S]*`)
  63. ScssArr[index] = item.replace(reg, `${lastItem}: ${value};`)
  64. }
  65. Scss = ScssArr.join('\n')
  66. }
  67. // 使用
  68. if (process.env.UNI_SCRIPT) {
  69. const path = package_json['uni-app']['scripts'][process.env.UNI_SCRIPT]['env']['ENV_PATH'] // 配置文件路径
  70. const config = require(path) // 读取配置信息
  71. replaceManifest('mp-weixin.appid', `"${config.appId}"`) // 更新 appid
  72. replacePagejson('globalStyle.navigationBarTitleText', `"${config.name}"`) // 更新 pages.json
  73. replaceScssPath('base-img-url', `'${config.serverFilePath}'`)
  74. }
  75. // 更新 manifest.json
  76. fs.writeFileSync(manifestPath, Manifest, {
  77. "flag": "w"
  78. })
  79. // 更新 pages.json
  80. fs.writeFileSync(pagePath, Pagejson, {
  81. "flag": "w"
  82. })
  83. // 更新 uni.scss
  84. fs.writeFileSync(scssPath, Scss, {
  85. "flag": "w"
  86. })
  87. module.exports = {
  88. // ...
  89. }