From 7dda73fef56402c4558394b8e60c67e5aa58ee77 Mon Sep 17 00:00:00 2001 From: Ayo Date: Sat, 14 Oct 2023 20:58:12 +0200 Subject: [PATCH] feat: clean server script before running --- packages/assets/mcfly-ssr.ts | 42 ++++++++++++++++++++++++++++++++++- public/favicon.ico | Bin 0 -> 1150 bytes src/pages/index.html | 2 ++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 public/favicon.ico diff --git a/packages/assets/mcfly-ssr.ts b/packages/assets/mcfly-ssr.ts index cee8860..0e65368 100644 --- a/packages/assets/mcfly-ssr.ts +++ b/packages/assets/mcfly-ssr.ts @@ -75,7 +75,8 @@ function doSetUp(html: string) { isServerScript ) { const scripts = node.children.map((child) => child.value); - serverScripts.push(scripts.join(" ")); + const script = cleanScript(scripts); + serverScripts.push(script); } }); @@ -117,3 +118,42 @@ function deleteServerScripts(html: string): string { return renderSync(ast); } + +function cleanScript(scripts: string[]): string { + let script = scripts.map((s) => s.trim()).join(" "); + + // remove comments + script = removeComments(script); + + return script.replace(/\n/g, "").replace(/\s+/g, " "); +} + +function isComment(node) { + return ( + node.type === "Line" || + node.type === "Block" || + node.type === "BlockComment" || + node.type === "LineComment" + ); +} + +function removeComments(script: string) { + const entries = []; + parseScript(script, { comment: true }, function (node, meta) { + if (isComment(node)) { + entries.push({ + start: meta.start.offset, + end: meta.end.offset, + }); + } + }); + + entries + .sort((a, b) => { + return b.end - a.end; + }) + .forEach((n) => { + script = script.slice(0, n.start) + script.slice(n.end); + }); + return script; +} diff --git a/public/favicon.ico b/public/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..edb0e4b60d048c92e395d1b447d148ab40376ee0 GIT binary patch literal 1150 zcmZQzU<5(|0R|wcz>vYhz#zuJz@P!dKp~(AL>x#lH~{6afd4=-s^f%&g#V^)0a(oi z$;AROK70Q|0lFGipj$ye+dc@d{;~)QusU@8pI+R+DvquW#K#4mzW;{TZkSo0U*Ey7 z3nak6z)6&Tm>YmG2v-04?r9A3V0Pi6K^jioe*@MJVsAQemuR~|W(J81fwaN(GkV20Dv;001`bWIg}@ literal 0 HcmV?d00001 diff --git a/src/pages/index.html b/src/pages/index.html index a113693..cbbca10 100644 --- a/src/pages/index.html +++ b/src/pages/index.html @@ -9,6 +9,8 @@ let greeting = "hello"; var count = sum(1, 247); + // comment + greeting = "nope"; function sum(x, y) {