You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.5 KiB
47 lines
1.5 KiB
2 years ago
|
//引入路径模块
|
||
|
const path = require("path");
|
||
|
//引入文件模块
|
||
|
const fs = require("fs");
|
||
|
const parse = require("node-html-parser").parse;
|
||
|
const pathName = path.join(__dirname, "../dist/index.html");
|
||
|
fs.readFile(pathName, "utf8", function (err, html) {
|
||
|
if (err) {
|
||
|
return console.log("读取index.html文件失败" + err.message);
|
||
|
}
|
||
|
const root = parse(html);
|
||
|
const elList = root.querySelectorAll("script");
|
||
|
|
||
|
for (let i = 0; i < elList.length; i++) {
|
||
|
// 1、移除 <script type=module> 元素
|
||
|
const data = elList[i].getAttribute("type");
|
||
|
if (data && data === "module") {
|
||
|
elList[i].remove();
|
||
|
}
|
||
|
|
||
|
// 2、移除其他 <script> 的 nomodule 属性
|
||
|
const hasNoModule = elList[i].hasAttribute("nomodule");
|
||
|
if (hasNoModule) {
|
||
|
elList[i].removeAttribute("nomodule");
|
||
|
}
|
||
|
|
||
|
// 3、移除 <script id=vite-legacy-entry> 元素的内容,并把 data-src 属性名改为 src
|
||
|
const hasDataSrc = elList[i].hasAttribute("data-src");
|
||
|
if (hasDataSrc) {
|
||
|
const legacyEle = elList[i];
|
||
|
const srcData = legacyEle.getAttribute("data-src");
|
||
|
legacyEle.setAttribute("src", srcData);
|
||
|
legacyEle.removeAttribute("data-src");
|
||
|
legacyEle.innerText = "";
|
||
|
}
|
||
|
|
||
|
// 4、移除其他 <script> 的 crossorigin 属性
|
||
|
const hasCrossorigin = elList[i].hasAttribute("crossorigin");
|
||
|
if (hasCrossorigin) {
|
||
|
elList[i].removeAttribute("crossorigin");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 将新的组合的内容写入原有index.html
|
||
|
fs.writeFileSync(pathName, root.toString());
|
||
|
});
|