parseDir2.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import fs from "node:fs/promises";
  2. import {constants} from "node:fs";
  3. import path from "path";
  4. import htmlMinifier from "html-minifier-terser";
  5. import esbuild from "esbuild";
  6. export default async (dir)=>{
  7. const indexFile = await findIndex(dir);
  8. if(!indexFile) return;
  9. let data = {};
  10. if(path.extname(indexFile) === ".neovan"){
  11. data = await getNeovanData(indexFile);
  12. }else{
  13. data = await parseHtml(indexFile);
  14. }
  15. const bundle = await createBundle(data);
  16. fs.rm(path.join(dir, "tmp/"), {recursive: true, force: true});
  17. }
  18. const findIndex = async (dir)=>{
  19. const neovanPath = path.join(dir, "index.neovan");
  20. const htmlPath = path.join(dir, "index.html");
  21. const [neovan, html] = await Promise.allSettled([
  22. fs.access(path.join(dir, "index.neovan"), constants.F_OK),
  23. fs.access(path.join(dir, "index.html"), constants.F_OK)
  24. ]);
  25. if(neovan.status === "fulfilled") return neovanPath;
  26. if(html.status === "fulfilled") return htmlPath;
  27. return null;
  28. }
  29. const getNeovanData = async (index)=>{
  30. const neovan = await fs.readFile(index, "utf-8");
  31. const parentPath = path.dirname(index);
  32. const html = neovan.slice(neovan.indexOf("<contents>") + 10, neovan.indexOf("</contents>"));
  33. const css = neovan.slice(neovan.indexOf("<style>") + 7, neovan.indexOf("</style>"));
  34. const js = neovan.slice(neovan.indexOf("<script>") + 8, neovan.indexOf("</script>"));
  35. const cssFile = path.join(parentPath, `tmp/${path.basename(index, ".neovan")}.css`);
  36. const jsFile = path.join(parentPath, `tmp/${path.basename(index, ".neovan")}.js`);
  37. await fs.mkdir(path.join(parentPath, "tmp/"));
  38. await Promise.all([
  39. css === "" ? null : fs.writeFile(cssFile, css),
  40. js === "" ? null : fs.writeFile(jsFile, js)
  41. ]);
  42. return {
  43. html: html,
  44. css: cssFile,
  45. js: jsFile
  46. };
  47. }
  48. const parseHtml = async (index)=>{
  49. const parentPath = path.dirname(index);
  50. const basename = path.basename(index, ".html");
  51. return {
  52. html: await fs.readFile(index, "utf-8"),
  53. css: path.join(parentPath, `${basename}.css`),
  54. js: path.join(parentPath, `${basename}.js`)
  55. };
  56. }
  57. const createBundle = async (data)=>{
  58. const entryPoints = [];
  59. if(data.css) entryPoints.push(data.css);
  60. if(data.js) entryPoints.push(data.js);
  61. const esbuildProm = esbuild.build({
  62. entryPoints: entryPoints,
  63. bundle: true,
  64. minify: true,
  65. write: false,
  66. outdir: "/"
  67. });
  68. const htmlProm = htmlMinifier.minify(data.html, {
  69. collapseBooleanAttributes: true,
  70. collapseInlineTagWhitespace: true,
  71. collapseWhitespace: true,
  72. decodeEntities: true,
  73. html5: true,
  74. includeAutoGeneratedTags: false,
  75. noNewlinesBeforeTagClose: true,
  76. removeComments: true,
  77. useShortDoctype: true
  78. });
  79. const [buildData, html] = await Promise.all([esbuildProm, htmlProm]);
  80. //potentially replaceable
  81. const comps = {html: html};
  82. for(let i = 0; i < buildData.outputFiles.length; i++){
  83. const ext = path.extname(buildData.outputFiles[i].path).replace(".", "");
  84. comps[ext] = buildData.outputFiles[i].text;
  85. }
  86. return mergeFiles(comps);
  87. }
  88. const mergeFiles = (comps)=>{
  89. const cssIndex = comps.html.indexOf("</head>");
  90. const html = `${comps.html.slice(0, cssIndex)}${comps.css}${comps.html.slice(cssIndex)}`;
  91. const jsIndex = html.indexOf("</body>");
  92. return `${comps.html.slice(0, jsIndex)}${comps.js}${html.slice(jsIndex)}`;
  93. }