parseComponent.js 4.5 KB

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