parseComponent.js 3.6 KB

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