From de4f1440b5135737429ffa38e57fdc51b09df658 Mon Sep 17 00:00:00 2001 From: halfnelson Date: Thu, 5 Nov 2020 22:34:19 +1000 Subject: [PATCH] Add comments to blanked script/style tags for perf --- packages/svelte2tsx/src/utils/htmlxparser.ts | 11 +- packages/svelte2tsx/test/htmlxparser/index.js | 15 + .../svelte2tsx/test/htmlxparser/large.svelte | 2273 +++++++++++++++++ 3 files changed, 2295 insertions(+), 4 deletions(-) create mode 100644 packages/svelte2tsx/test/htmlxparser/index.js create mode 100644 packages/svelte2tsx/test/htmlxparser/large.svelte diff --git a/packages/svelte2tsx/src/utils/htmlxparser.ts b/packages/svelte2tsx/src/utils/htmlxparser.ts index bca7ec3e2..844593744 100644 --- a/packages/svelte2tsx/src/utils/htmlxparser.ts +++ b/packages/svelte2tsx/src/utils/htmlxparser.ts @@ -82,10 +82,13 @@ function blankVerbatimContent(htmlx: string, verbatimElements: Node[]) { for (const node of verbatimElements) { const content = node.content; if (content) { - output = - output.substring(0, content.start) + - output.substring(content.start, content.end).replace(/[^\n]/g, ' ') + - output.substring(content.end); + output = htmlx.substring(0, content.start) + + htmlx.substring(content.start, content.end) + // blank out the content + .replace(/[^\n]/g, ' ') + // excess blank space can make the svelte parser very slow (sec->min). break it up with comments (works in style/script) + .replace(/[^\n][^\n][^\n][^\n]\n/g, '/**/\n') + + htmlx.substring(content.end); } } return output; diff --git a/packages/svelte2tsx/test/htmlxparser/index.js b/packages/svelte2tsx/test/htmlxparser/index.js new file mode 100644 index 000000000..a0d296ec3 --- /dev/null +++ b/packages/svelte2tsx/test/htmlxparser/index.js @@ -0,0 +1,15 @@ +let converter = require('../build/htmlxtojsx') +let fs = require('fs') +let assert = require('assert') + +describe('htmlxparser', () => { + let content = fs.readFileSync(`${__dirname}/large.svelte`, {encoding: 'utf8'}); + + it('parses in a reasonable time', () => { + const start = new Date(); + converter.htmlx2jsx(content); + const elapsed = new Date() - start; + assert(elapsed <= 1000, `Parsing took ${elapsed} ms, which was longer than 1000ms`); + }) + +}); \ No newline at end of file diff --git a/packages/svelte2tsx/test/htmlxparser/large.svelte b/packages/svelte2tsx/test/htmlxparser/large.svelte new file mode 100644 index 000000000..b00d72b7c --- /dev/null +++ b/packages/svelte2tsx/test/htmlxparser/large.svelte @@ -0,0 +1,2273 @@ + + + \ No newline at end of file