UNPKG

901 BJavaScriptView Raw
1const fs = require('fs')
2const path = require('path')
3const targetDir = path.join(process.cwd(), 'dist')
4const files = []
5const offlineCaching = require(path.join(process.cwd(), 'app.config')).offlineCaching
6let fileName = ''
7
8function getFilesPath(filePath) {
9 fs.readdirSync(filePath).forEach(name => {
10 const filedir = path.join(filePath, name)
11 if (fs.statSync(filedir).isFile()) {
12 if (filedir.indexOf('sweet.mobile.manifest') > -1) {
13 fileName = filedir
14 } else if (filedir.indexOf('index.html') === -1) {
15 files.push(filedir.substring(filedir.indexOf('dist') + 5))
16 }
17 } else {
18 getFilesPath(filedir)
19 }
20 })
21}
22
23if (offlineCaching) {
24 getFilesPath(targetDir)
25 const __template = `CACHE MANIFEST
26
27CACHE:
28${files.join('\n')}
29`
30 fs.writeFileSync(path.resolve(targetDir, fileName), __template)
31 console.log('离线缓存manifest生产完成')
32}