平时写完项目打包,我们会使用npm run build

实际上,我们可以自己模拟一下这个过程,以便深入了解打包的全过程。

咱们的案例是,第一步读取数据./public/index.html和./src/index.js第二步将读入的数据处理后分别存入./disc/index.js和./disc/index.html这里注意,我需要将js引入到html中

```
fs.readFile("./src/index.js", { encoding: "utf-8" }, (err, js) => {
fs.writeFile("./disc/index.js", js, (err) => {
if (err) {
console.error(err);
} else {
console.log("JS写入成功!");
fs.readFile("./public/index.html", { encoding: "utf-8" }, (err, html) => {
let newHtml = html.replace(
"</body>",
"</body><script src='./index.js'></script>"
);
fs.writeFile("./disc/index.html", newHtml, (err) => {
if (err) {
console.error(err);
} else {
console.log("html写入成功!");
}
});
});
}
});
});


```
展开
2