forked from pandaoh/js-xxx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_docs.build.js
50 lines (44 loc) · 1.27 KB
/
_docs.build.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
/*
* @Author: HxB
* @Date: 2023-08-25 17:08:31
* @LastEditors: DoubleAm
* @LastEditTime: 2023-08-28 14:56:00
* @Description: 引入 js-xxx 到文档中
* @FilePath: \js-xxx\_docs.build.js
*/
const fs = require('fs');
/**
* 复制文件
* @param {*} src
* @param {*} target
*/
const copyFile = (src, target) => {
if (fs.existsSync(target)) {
console.log('目标文件已存在');
return;
}
try {
fs.writeFileSync(target, fs.readFileSync(src));
} catch (e) {
console.log({ 'copyFile error': e });
try {
fs.createReadStream(src).pipe(fs.createWriteStream(target)); // 大文件复制
} catch (err) {
console.log({ 'copyBigFile error': err });
}
}
};
copyFile('./iife/index.min.js', './html/js-xxx.min.js');
copyFile('./_docs.onload.js', './html/docs.onload.js');
// 读取生成的 HTML 文件
const filePath = './html/index.html';
const html = fs.readFileSync(filePath, 'utf-8');
// 添加自定义的 JavaScript 文件引入
const scriptPath = './js-xxx.min.js';
const onloadPath = './docs.onload.js';
const modifiedHtml = html.replace(
'</body>',
`\n<script src="${scriptPath}"></script>\n<script src="${onloadPath}"></script>\n</body>`,
);
// 将修改后的 HTML 文件保存
fs.writeFileSync(filePath, modifiedHtml, 'utf-8');