parseComponent.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. fs.rm(path.join(dir, "tmp/"), {recursive: true, force: true});
  15. return await createBundle(data);
  16. }
  17. const getNeovanData = async (index)=>{
  18. const neovan = await fs.readFile(index, "utf-8");
  19. const parentPath = path.dirname(index);
  20. const html = neovan.slice(neovan.indexOf("<contents>") + 10, neovan.indexOf("</contents>"));
  21. const css = neovan.slice(neovan.indexOf("<style>") + 7, neovan.indexOf("</style>"));
  22. const js = neovan.slice(neovan.indexOf("<script>") + 8, neovan.indexOf("</script>"));
  23. const cssFile = path.join(parentPath, `tmp/${path.basename(index, ".neovan")}.css`);
  24. const jsFile = path.join(parentPath, `tmp/${path.basename(index, ".neovan")}.js`);
  25. await fs.mkdir(path.join(parentPath, "tmp/"));
  26. await Promise.all([
  27. fs.writeFile(cssFile, css),
  28. fs.writeFile(jsFile, js)
  29. ]);
  30. return {
  31. html: html,
  32. css: cssFile,
  33. js: jsFile,
  34. dir: parentPath
  35. };
  36. }
  37. const parseHtml = async (index)=>{
  38. const parentPath = path.dirname(index);
  39. const basename = path.basename(index, ".html");
  40. const cssPath = path.join(parentPath, `${basename}.css`);
  41. const jsPath = path.join(parentPath, `${basename}.js`);
  42. const proms = [
  43. fs.readFile(index, "utf-8"),
  44. fs.access(cssPath, constants.F_OK),
  45. fs.access(jsPath, constants.F_OK)
  46. ];
  47. let [html, css, js] = await Promise.allSettled(proms);
  48. return {
  49. html: html.value,
  50. css: css.status === "fulfilled" ? cssPath : null,
  51. js: js.status === "fulfilled" ? jsPath : null,
  52. dir: parentPath
  53. };
  54. }
  55. const createBundle = async (data)=>{
  56. const entryPoints = [];
  57. if(data.css) entryPoints.push(data.css);
  58. if(data.js) entryPoints.push(data.js);
  59. data.html = await addComponents(data.html, data.dir);
  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. let cssIndex = comps.html.indexOf("</head>");
  88. cssIndex = cssIndex < 0 ? 0 : cssIndex;
  89. comps.css = comps.css ? `<style>${comps.css}</style>` : "";
  90. const html = `${comps.html.slice(0, cssIndex)}${comps.css}${comps.html.slice(cssIndex)}`;
  91. let jsIndex = html.indexOf("</body>");
  92. jsIndex = jsIndex < 0 ? html.length : jsIndex;
  93. comps.js = comps.js ? `<script>${comps.js}</script>` : "";
  94. return `${html.slice(0, jsIndex)}${comps.js}${html.slice(jsIndex)}`;
  95. }
  96. const addComponents = async (html, dir)=>{
  97. let importStart = 0;
  98. for(let i = 0; i < html.length; i++){
  99. if(html[i] === "@"){
  100. if(html[i-1] === "<"){
  101. importStart = i + 1;
  102. }else if(html[i+1] === ">"){
  103. const importString = html.substring(importStart, i).trim();
  104. const comp = await parseComponent(path.join(dir, importString))
  105. html = html.slice(0, importStart - 2) + comp + html.slice(i+2);
  106. }
  107. }
  108. }
  109. return html;
  110. }
  111. export default parseComponent;