1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- const fs = require('fs')
- const path = require('path')
- let dir = path.join(__dirname, 'dist')
- let list = walk(dir)
- if (process.env.URL === 'new') {
- global.URL_CDN = '//static.meechat.me/cdn/new.mee.chat/'
- } else if (process.env.URL === 'form') {
- global.URL_CDN = '//static.meechat.me/cdn/mee.chat/'
- } else {
- global.URL_CDN = '/'
- }
- // console.log(global.URL_CDN)
- // console.log(dir)
- // 获取文件
- let baseDir = path.join(__dirname, '/')
- list.forEach((value, index) => {
- // 对.html和.json特殊处理
- if (value.endsWith('.html') || value.endsWith('.json')) {
- value = value.replace(baseDir, '/')
- list[index] = value.replace('/dist/', '/')
- } else {
- value = value.replace(baseDir, '')
- list[index] = global.URL_CDN + value
- }
- })
- let fileName = dir + '/sw.js'
- // 修改文件
- fs.readFile(fileName, 'utf8', function (err, files) {
- if (!err) {
- let result = files.replace('let urlsToCache = []', 'let urlsToCache = ' + JSON.stringify(list) + '')
- fs.writeFile(fileName, result, 'utf8', function (err) {
- if (err) return console.log(err)
- })
- }
- })
- function walk (dir) {
- var children = []
- fs.readdirSync(dir).forEach(function (filename) {
- var path = dir + '/' + filename
- var stat = fs.statSync(path)
- if (stat && stat.isDirectory()) {
- children = children.concat(walk(path))
- } else {
- children.push(path)
- }
- })
- return children
- }
|