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 0000000..edb0e4b Binary files /dev/null and b/public/favicon.ico differ 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) {