build_cache.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. const fs = require('fs')
  2. const path = require('path')
  3. let dir = path.join(__dirname, 'dist')
  4. let list = walk(dir)
  5. if (process.env.URL === 'new') {
  6. global.URL_CDN = '//static.meechat.me/cdn/new.mee.chat/'
  7. } else if (process.env.URL === 'form') {
  8. global.URL_CDN = '//static.meechat.me/cdn/mee.chat/'
  9. } else {
  10. global.URL_CDN = '/'
  11. }
  12. // console.log(global.URL_CDN)
  13. // console.log(dir)
  14. // 获取文件
  15. let baseDir = path.join(__dirname, '/')
  16. list.forEach((value, index) => {
  17. list[index] = global.URL_CDN + value.replace(baseDir, '')
  18. })
  19. let fileName = dir + '/sw.js'
  20. console.log(list)
  21. // 修改文件
  22. fs.readFile(fileName, 'utf8', function (err, files) {
  23. if (!err) {
  24. let result = files.replace('let urlsToCache = []', 'let urlsToCache = ' + JSON.stringify(list) + '')
  25. fs.writeFile(fileName, result, 'utf8', function (err) {
  26. if (err) return console.log(err)
  27. })
  28. }
  29. })
  30. function walk (dir) {
  31. var children = []
  32. fs.readdirSync(dir).forEach(function (filename) {
  33. var path = dir + '/' + filename
  34. var stat = fs.statSync(path)
  35. if (stat && stat.isDirectory()) {
  36. children = children.concat(walk(path))
  37. } else {
  38. children.push(path)
  39. }
  40. })
  41. return children
  42. }