parseComponent.js 4.3 KB

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