1 | const fs = require('fs')
|
2 | const path = require('path')
|
3 | const targetDir = path.join(process.cwd(), 'dist')
|
4 | const files = []
|
5 | const offlineCaching = require(path.join(process.cwd(), 'app.config')).offlineCaching
|
6 | let fileName = ''
|
7 |
|
8 | function 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 |
|
23 | if (offlineCaching) {
|
24 | getFilesPath(targetDir)
|
25 | const __template = `CACHE MANIFEST
|
26 |
|
27 | CACHE:
|
28 | ${files.join('\n')}
|
29 | `
|
30 | fs.writeFileSync(path.resolve(targetDir, fileName), __template)
|
31 | console.log('离线缓存manifest生产完成')
|
32 | }
|