From 4bcf9c4bacad8db24cf6f5d4699e8780b35f61c6 Mon Sep 17 00:00:00 2001 From: daiwei Date: Fri, 28 Oct 2022 09:07:21 +0800 Subject: [PATCH 1/6] fix(compiler-sfc): avoid gen useCssVars when targeting SSR in scriptSetup --- packages/compiler-sfc/src/compileScript.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index b7e4c0ea778..0c69db29848 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -1433,7 +1433,7 @@ export function compileScript( if ( cssVars.length && // no need to do this when targeting SSR - !(options.inlineTemplate && options.templateOptions?.ssr) + !(scriptSetup && options.templateOptions?.ssr) ) { helperImports.add(CSS_VARS_HELPER) helperImports.add('unref') From 4970c39e981ba1b68932b3a946fe91f97f1265dc Mon Sep 17 00:00:00 2001 From: daiwei Date: Tue, 14 Mar 2023 14:57:56 +0800 Subject: [PATCH 2/6] chore: improve code --- packages/compiler-sfc/src/compileScript.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index 0c69db29848..22b737ea2b6 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -1433,7 +1433,7 @@ export function compileScript( if ( cssVars.length && // no need to do this when targeting SSR - !(scriptSetup && options.templateOptions?.ssr) + !options.templateOptions?.ssr ) { helperImports.add(CSS_VARS_HELPER) helperImports.add('unref') From 587a489733b27f38bcd48638bfb71040c740432f Mon Sep 17 00:00:00 2001 From: daiwei Date: Wed, 12 Apr 2023 10:29:18 +0800 Subject: [PATCH 3/6] chore: improve code --- packages/compiler-sfc/src/script/normalScript.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/compiler-sfc/src/script/normalScript.ts b/packages/compiler-sfc/src/script/normalScript.ts index 76b25c66350..d0f16134273 100644 --- a/packages/compiler-sfc/src/script/normalScript.ts +++ b/packages/compiler-sfc/src/script/normalScript.ts @@ -55,7 +55,7 @@ export function processNormalScript( const s = new MagicString(content) rewriteDefaultAST(scriptAst.body, s, defaultVar) content = s.toString() - if (cssVars.length) { + if (cssVars.length && !ctx.options.templateOptions?.ssr) { content += genNormalScriptCssVarsCode( cssVars, bindings, From 49f0699569235b73f482a186ff2c9f524cf1d515 Mon Sep 17 00:00:00 2001 From: edison1105 Date: Fri, 29 Sep 2023 20:38:02 +0800 Subject: [PATCH 4/6] test: add test case --- .../__snapshots__/cssVars.spec.ts.snap | 13 ++++++++++ .../compiler-sfc/__tests__/cssVars.spec.ts | 26 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/packages/compiler-sfc/__tests__/__snapshots__/cssVars.spec.ts.snap b/packages/compiler-sfc/__tests__/__snapshots__/cssVars.spec.ts.snap index 1e99f6087c1..b66899be19c 100644 --- a/packages/compiler-sfc/__tests__/__snapshots__/cssVars.spec.ts.snap +++ b/packages/compiler-sfc/__tests__/__snapshots__/cssVars.spec.ts.snap @@ -170,6 +170,19 @@ return { color, size, ref } }" `; +exports[`CSS vars injection > w/ normal \n` + + ``, + { + templateOptions: { + ssr: true + } + } + ) + expect(content).not.toMatch(`__injectCSSVars__`) + assertCode(content) + }) + test('w/ \n` + - ``, - { - templateOptions: { - ssr: true - } - } - ) - expect(content).not.toMatch(`_useCssVars`) - assertCode(content) - }) - test('w/ \n` + + ``, + { + inlineTemplate: true, + templateOptions: { + ssr: true + } + } + ) + expect(content).not.toMatch(`_useCssVars`) + }) + + // #6926 + test('script, non-inline', () => { + const { content } = compileSFCScript( + `\n` + + ``, + { + inlineTemplate: false, + templateOptions: { + ssr: true + } + } + ) + expect(content).not.toMatch(`_useCssVars`) + }) + + test('normal script', () => { + const { content } = compileSFCScript( + `\n` + + ``, + { + templateOptions: { + ssr: true + } + } + ) + expect(content).not.toMatch(`_useCssVars`) + }) + }) }) })