forked from dianluyuanli-wp/jsDiffWeb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwriteHtml.js
More file actions
24 lines (21 loc) · 1.04 KB
/
writeHtml.js
File metadata and controls
24 lines (21 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
const fs = require('fs');
const path = require('path');
const FILE_TYPE = {
JS: 0,
CSS: 1
};
// 获取版本
var newVersion = require('./package.json').version;
function getTagWithCDN(fileName, type = FILE_TYPE.JS) {
const url = `https://cdn.jsdelivr.net/gh/dianluyuanli-wp/diffDist@${newVersion}/${fileName}`;
return type === FILE_TYPE.JS ? `<script src="${url}" type="text/javascript"></script>` : `<link rel="stylesheet" href="${url}" >`;
}
// 读取原始模板内容
const HTMLTemplate = path.resolve(__dirname, "./view/template.html"); // 大文件存储目录
const oldContent = fs.readFileSync(HTMLTemplate, 'utf8');
// 替换标签
const newContent = oldContent.replace('<app_bundle />', getTagWithCDN('app.bundle.js'))
.replace('<common_bundle />', getTagWithCDN('common.bundle.js')).replace('<app_css />', getTagWithCDN('app.css', FILE_TYPE.CSS))
.replace('<common_css />', getTagWithCDN('common.css', FILE_TYPE.CSS));
// 生成最终的结果:替换cdn源之后的html文件
fs.writeFileSync('dist/index.html', newContent, 'utf8');