parseComponent.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. const parseComponent = async (file)=>{
  7. const dir = path.dirname(file);
  8. let data = {};
  9. if(path.extname(file) === ".neovan"){
  10. data = await getNeovanData(file);
  11. }else{
  12. data = await parseHtml(file);
  13. }
  14. return await createBundle(data);
  15. }
  16. const getNeovanData = async (index)=>{
  17. const neovan = await fs.readFile(index, "utf-8");
  18. const parentPath = path.dirname(index);
  19. const html = neovan.slice(neovan.indexOf("<contents>") + 10, neovan.indexOf("</contents>"));
  20. let css = "";
  21. let js = "";
  22. const cssIndex = neovan.indexOf("<style>");
  23. if(cssIndex >= 0){
  24. css = neovan.slice(cssIndex + 7, neovan.indexOf("</style>"));
  25. }
  26. const jsIndex = neovan.indexOf("<script>")
  27. if(jsIndex >= 0){
  28. js = neovan.slice(jsIndex + 8, neovan.indexOf("</script>"));
  29. }
  30. const cssFile = path.join(parentPath, `tmp/${path.basename(index, ".neovan")}.css`);
  31. const jsFile = path.join(parentPath, `tmp/${path.basename(index, ".neovan")}.js`);
  32. await fs.mkdir(path.join(parentPath, "tmp/"));
  33. await Promise.all([
  34. fs.writeFile(cssFile, css),
  35. fs.writeFile(jsFile, js)
  36. ]);
  37. return {
  38. html: html,
  39. css: cssFile,
  40. js: jsFile,
  41. dir: parentPath
  42. };
  43. }
  44. const parseHtml = async (index)=>{
  45. const parentPath = path.dirname(index);
  46. const basename = path.basename(index, ".html");
  47. const cssPath = path.join(parentPath, `${basename}.css`);
  48. const jsPath = path.join(parentPath, `${basename}.js`);
  49. const proms = [
  50. fs.readFile(index, "utf-8"),
  51. fs.access(cssPath, constants.F_OK),
  52. fs.access(jsPath, constants.F_OK)
  53. ];
  54. let [html, css, js] = await Promise.allSettled(proms);
  55. return {
  56. html: html.value,
  57. css: css.status === "fulfilled" ? cssPath : null,
  58. js: js.status === "fulfilled" ? jsPath : null,
  59. dir: parentPath
  60. };
  61. }
  62. const createBundle = async (data)=>{
  63. const entryPoints = [];
  64. if(data.css) entryPoints.push(data.css);
  65. if(data.js) entryPoints.push(data.js);
  66. data.html = await addComponents(data.html, data.dir);
  67. const esbuildProm = esbuild.build({
  68. entryPoints: entryPoints,
  69. bundle: true,
  70. minify: true,
  71. write: false,
  72. outdir: "/"
  73. });
  74. const htmlProm = htmlMinifier.minify(data.html, {
  75. collapseBooleanAttributes: true,
  76. collapseInlineTagWhitespace: true,
  77. collapseWhitespace: true,
  78. decodeEntities: true,
  79. html5: true,
  80. includeAutoGeneratedTags: false,
  81. noNewlinesBeforeTagClose: true,
  82. removeComments: true,
  83. useShortDoctype: true
  84. });
  85. const [buildData, html] = await Promise.all([esbuildProm, htmlProm]);
  86. const comps = {html: html};
  87. for(let i = 0; i < buildData.outputFiles.length; i++){
  88. const ext = path.extname(buildData.outputFiles[i].path).replace(".", "");
  89. comps[ext] = buildData.outputFiles[i].text;
  90. }
  91. return mergeFiles(comps);
  92. }
  93. const mergeFiles = (comps)=>{
  94. console.log(comps);
  95. console.log();
  96. let cssIndex = comps.html.indexOf("</head>");
  97. cssIndex = cssIndex < 0 ? 0 : cssIndex;
  98. comps.css = comps.css ? `<style>${comps.css}</style>` : "";
  99. const html = `${comps.html.slice(0, cssIndex)}${comps.css}${comps.html.slice(cssIndex)}`;
  100. let jsIndex = html.indexOf("</body>");
  101. jsIndex = jsIndex < 0 ? html.length : jsIndex;
  102. comps.js = comps.js ? `<script>${comps.js}</script>` : "";
  103. return `${html.slice(0, jsIndex)}${comps.js}${html.slice(jsIndex)}`;
  104. }
  105. const addComponents = async (html, dir)=>{
  106. let importStart = 0;
  107. for(let i = 0; i < html.length; i++){
  108. if(html[i] === "@"){
  109. if(html[i-1] === "<"){
  110. importStart = i + 1;
  111. }else if(html[i+1] === ">"){
  112. const importString = html.substring(importStart, i).trim();
  113. const comp = await parseComponent(path.join(dir, importString))
  114. html = html.slice(0, importStart - 2) + comp + html.slice(i+2);
  115. }
  116. }
  117. }
  118. return html;
  119. }
  120. export default parseComponent;