parseComponent.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 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. css === "" ? null : fs.writeFile(cssFile, css),
  28. js === "" ? null : fs.writeFile(jsFile, js)
  29. ]);
  30. return {
  31. html: html,
  32. css: cssFile,
  33. js: jsFile
  34. };
  35. }
  36. const parseHtml = async (index)=>{
  37. const parentPath = path.dirname(index);
  38. const basename = path.basename(index, ".html");
  39. return {
  40. html: await fs.readFile(index, "utf-8"),
  41. css: path.join(parentPath, `${basename}.css`),
  42. js: path.join(parentPath, `${basename}.js`)
  43. };
  44. }
  45. const createBundle = async (data)=>{
  46. const entryPoints = [];
  47. if(data.css) entryPoints.push(data.css);
  48. if(data.js) entryPoints.push(data.js);
  49. const esbuildProm = esbuild.build({
  50. entryPoints: entryPoints,
  51. bundle: true,
  52. minify: true,
  53. write: false,
  54. outdir: "/"
  55. });
  56. const htmlProm = htmlMinifier.minify(data.html, {
  57. collapseBooleanAttributes: true,
  58. collapseInlineTagWhitespace: true,
  59. collapseWhitespace: true,
  60. decodeEntities: true,
  61. html5: true,
  62. includeAutoGeneratedTags: false,
  63. noNewlinesBeforeTagClose: true,
  64. removeComments: true,
  65. useShortDoctype: true
  66. });
  67. const [buildData, html] = await Promise.all([esbuildProm, htmlProm]);
  68. const comps = {html: html};
  69. for(let i = 0; i < buildData.outputFiles.length; i++){
  70. const ext = path.extname(buildData.outputFiles[i].path).replace(".", "");
  71. comps[ext] = buildData.outputFiles[i].text;
  72. }
  73. return mergeFiles(comps);
  74. }
  75. const mergeFiles = (comps)=>{
  76. const cssIndex = comps.html.indexOf("</head>");
  77. const html = `${comps.html.slice(0, cssIndex)}<style>${comps.css}</style>${comps.html.slice(cssIndex)}`;
  78. const jsIndex = html.indexOf("</body>");
  79. return `${html.slice(0, jsIndex)}<script>${comps.js}</script>${html.slice(jsIndex)}`;
  80. }
  81. const writeFile = async (dir, bundle)=>{
  82. const writeDir = dir.replace("routes", ".build");
  83. await fs.mkdir(writeDir, {recursive: true});
  84. await fs.writeFile(`${writeDir}/index.html`, bundle);
  85. }