-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfe-xiaozhuye.js
87 lines (80 loc) · 3.07 KB
/
fe-xiaozhuye.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const fs = require('fs');
const path = require('path');
const _ = require('lodash');
const webpack = require('webpack');
const webpackConfig = require('./www/xiaozhuye/webpack.config.js');
let compiler = webpack(webpackConfig);
const jsPath = './www/static/js/xiaozhuye';
const chunkPath = './www/static/js/xiaozhuye/chunk';
const indexHtmlPath = './view/home/index_index.html';
const manifestPath = './www/tiny.appcache';
// 清空js文件夹内所有文件
// 删除chunk文件夹
console.log('\n\033[91m 清理js文件夹... \033[0m' + '\n');
if (fs.existsSync(chunkPath)) {
let chunkFolder = fs.readdirSync(chunkPath);
chunkFolder.forEach(fileName => {
fs.unlinkSync(chunkPath + '/' + fileName);
});
fs.rmdirSync(chunkPath);
}
// 删除js文件夹
if (fs.existsSync(jsPath)) {
let jsFolder = fs.readdirSync(jsPath);
jsFolder.forEach(fileName => {
fs.unlinkSync(jsPath + '/' + fileName);
});
}
// webpack打包
console.log('\033[91m 开始打包js... \033[0m');
compiler.run((err, stats) => {
// if (stats.hasErrors()) {
// return console.log(stats.toJson().errorDetails);
// }
// if (stats.hasWarnings()) {
// return console.log(stats.hasWarnings());
// }
let chunksArr = stats.toJson('minimal').chunks;
// console.log(chunksArr);
// 获取index.js
let indexJs = _.find(chunksArr, ['names', ['index']]);
// 更新xiaozhuye index_html script标签
fs.readFile(indexHtmlPath, 'utf8', (err, data) => {
if (err) throw err;
// script标签
let scriptExp = /\/static\/js\/\w*.*[\d\w]*\.js/;
let newScript = data.replace(scriptExp, '/static/js/' + indexJs.files[0]);
// 更新文件
fs.writeFile(indexHtmlPath, newScript, 'utf8', (err) => {
if (err) throw err;
console.log('\033[92m ' + indexHtmlPath + ' 更新成功 \033[0m' + '\n');
});
});
// 获取当前文件夹下所有js文件
let newJsCache = '';
_.each(chunksArr, (value) => {
newJsCache += '\n' + '/static/js/' + value.files[0];
console.log('\033[92m 打包js => \033[0m' + value.files[0] + '\033[92m 大小: ' + value.size / 1000 + 'KB\033[0m');
});
// 更新manifest配置
if (fs.existsSync(manifestPath)) {
console.log('\n\033[91m 检测到: ' + manifestPath + ' 开始清理旧文件... \033[0m');
fs.unlinkSync(manifestPath);
console.log('\033[91m 已删除: ' + manifestPath + ' \033[0m');
}
if (!fs.existsSync(manifestPath)) {
let verNum = '# ' + Date();
let verText = 'CACHE MANIFEST\n' + verNum;
let cssStatic = '\n/static/css/bootstrap.min.css\n' +
'/static/css/style.css\n' +
'/static/img/default.png\n' +
'/static/img/refresh.png\n' +
'/favicon.ico\n\n' +
'NETWORK:\n*';
let newFile = verText + newJsCache + cssStatic;
fs.writeFile(manifestPath, newFile, 'utf8', (err) => {
if (err) throw err;
console.log('\033[92m 新建 ' + manifestPath + ' 成功 \033[0m' + '\n');
});
}
});