From 7afbafcc1cbb36856219b42725d9fe1ee91ee61a Mon Sep 17 00:00:00 2001 From: daiwei Date: Wed, 11 Sep 2024 10:38:08 +0800 Subject: [PATCH 1/2] fix(compile-dom): properly stringify static w/ non-eligible nodes --- .../stringifyStatic.spec.ts.snap | 22 +++++++++++++++++++ .../transforms/stringifyStatic.spec.ts | 11 ++++++++++ .../src/transforms/stringifyStatic.ts | 2 +- 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/packages/compiler-dom/__tests__/transforms/__snapshots__/stringifyStatic.spec.ts.snap b/packages/compiler-dom/__tests__/transforms/__snapshots__/stringifyStatic.spec.ts.snap index f55c27a9c6e..4a743342451 100644 --- a/packages/compiler-dom/__tests__/transforms/__snapshots__/stringifyStatic.spec.ts.snap +++ b/packages/compiler-dom/__tests__/transforms/__snapshots__/stringifyStatic.spec.ts.snap @@ -1,5 +1,16 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html +exports[`stringify static html > eligible content (elements > 20) + non-eligible content 1`] = ` +"const { createElementVNode: _createElementVNode, createStaticVNode: _createStaticVNode, openBlock: _openBlock, createElementBlock: _createElementBlock } = Vue + +return function render(_ctx, _cache) { + return (_openBlock(), _createElementBlock("div", null, _cache[0] || (_cache[0] = [ + _createStaticVNode("", 20), + _createElementVNode("div", { key: "1" }, "1", -1 /* HOISTED */) + ]))) +}" +`; + exports[`stringify static html > escape 1`] = ` "const { toDisplayString: _toDisplayString, normalizeClass: _normalizeClass, createElementVNode: _createElementVNode, createStaticVNode: _createStaticVNode, openBlock: _openBlock, createElementBlock: _createElementBlock } = Vue @@ -73,6 +84,17 @@ return function render(_ctx, _cache) { }" `; +exports[`stringify static html > should work on eligible content (elements > 20) + non-eligible content 1`] = ` +"const { createElementVNode: _createElementVNode, createStaticVNode: _createStaticVNode, openBlock: _openBlock, createElementBlock: _createElementBlock } = Vue + +return function render(_ctx, _cache) { + return (_openBlock(), _createElementBlock("div", null, _cache[0] || (_cache[0] = [ + _createStaticVNode("", 20), + _createElementVNode("div", { key: "1" }, "1", -1 /* HOISTED */) + ]))) +}" +`; + exports[`stringify static html > should work on eligible content (elements > 20) 1`] = ` "const { createElementVNode: _createElementVNode, createStaticVNode: _createStaticVNode, openBlock: _openBlock, createElementBlock: _createElementBlock } = Vue diff --git a/packages/compiler-dom/__tests__/transforms/stringifyStatic.spec.ts b/packages/compiler-dom/__tests__/transforms/stringifyStatic.spec.ts index fbf5718e65e..eeb13b993d4 100644 --- a/packages/compiler-dom/__tests__/transforms/stringifyStatic.spec.ts +++ b/packages/compiler-dom/__tests__/transforms/stringifyStatic.spec.ts @@ -122,6 +122,17 @@ describe('stringify static html', () => { expect(code).toMatchSnapshot() }) + test('eligible content (elements > 20) + non-eligible content', () => { + const { code } = compileWithStringify( + `
${repeat( + ``, + StringifyThresholds.NODE_COUNT, + )}
1
`, + ) + + expect(code).toMatchSnapshot() + }) + test('should work for multiple adjacent nodes', () => { const { ast, code } = compileWithStringify( `
${repeat( diff --git a/packages/compiler-dom/src/transforms/stringifyStatic.ts b/packages/compiler-dom/src/transforms/stringifyStatic.ts index 05535a2c312..8744497d090 100644 --- a/packages/compiler-dom/src/transforms/stringifyStatic.ts +++ b/packages/compiler-dom/src/transforms/stringifyStatic.ts @@ -108,7 +108,7 @@ export const stringifyStatic: HoistTransform = (children, context, parent) => { if (isParentCached) { ;((parent.codegenNode as VNodeCall).children as CacheExpression).value = - createArrayExpression([staticCall]) + createArrayExpression([staticCall, ...children.slice(currentIndex)]) } else { // replace the first node's hoisted expression with the static vnode call ;(currentChunk[0].codegenNode as CacheExpression).value = staticCall From 653926ee2ab4fa9b0a74fc4025a8f97f95623435 Mon Sep 17 00:00:00 2001 From: edison Date: Thu, 12 Sep 2024 15:07:13 +0800 Subject: [PATCH 2/2] Update stringifyStatic.spec.ts.snap --- .../__snapshots__/stringifyStatic.spec.ts.snap | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/packages/compiler-dom/__tests__/transforms/__snapshots__/stringifyStatic.spec.ts.snap b/packages/compiler-dom/__tests__/transforms/__snapshots__/stringifyStatic.spec.ts.snap index 4a743342451..45a46210aa1 100644 --- a/packages/compiler-dom/__tests__/transforms/__snapshots__/stringifyStatic.spec.ts.snap +++ b/packages/compiler-dom/__tests__/transforms/__snapshots__/stringifyStatic.spec.ts.snap @@ -84,17 +84,6 @@ return function render(_ctx, _cache) { }" `; -exports[`stringify static html > should work on eligible content (elements > 20) + non-eligible content 1`] = ` -"const { createElementVNode: _createElementVNode, createStaticVNode: _createStaticVNode, openBlock: _openBlock, createElementBlock: _createElementBlock } = Vue - -return function render(_ctx, _cache) { - return (_openBlock(), _createElementBlock("div", null, _cache[0] || (_cache[0] = [ - _createStaticVNode("", 20), - _createElementVNode("div", { key: "1" }, "1", -1 /* HOISTED */) - ]))) -}" -`; - exports[`stringify static html > should work on eligible content (elements > 20) 1`] = ` "const { createElementVNode: _createElementVNode, createStaticVNode: _createStaticVNode, openBlock: _openBlock, createElementBlock: _createElementBlock } = Vue