From 6965652a415dd1e7f25e876c3f2405f3a4411924 Mon Sep 17 00:00:00 2001 From: Doctorwu Date: Tue, 5 Mar 2024 17:42:00 +0800 Subject: [PATCH 1/8] fix(compiler-sfc): use options module name if options provide runtimeModuleName options close #10454 --- packages/compiler-sfc/src/compileScript.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index 46fc65c0069..a9aaa8dfe54 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -992,7 +992,9 @@ export function compileScript( ctx.s.prepend( `import { ${[...ctx.helperImports] .map(h => `${h} as _${h}`) - .join(', ')} } from 'vue'\n`, + .join( + ', ', + )} } from '${options.templateOptions?.compilerOptions?.runtimeModuleName || 'vue'}'\n`, ) } From 81325de58ab5c75715be33497cdc405b3ba6a93b Mon Sep 17 00:00:00 2001 From: Doctorwu Date: Tue, 5 Mar 2024 21:34:50 +0800 Subject: [PATCH 2/8] feat: use json util function --- 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 a9aaa8dfe54..b28f33a8610 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -994,7 +994,7 @@ export function compileScript( .map(h => `${h} as _${h}`) .join( ', ', - )} } from '${options.templateOptions?.compilerOptions?.runtimeModuleName || 'vue'}'\n`, + )} } from ${JSON.stringify(options.templateOptions?.compilerOptions?.runtimeModuleName || 'vue')}\n`, ) } From 94e001767bdd01cf3d31b90e311a99f6a8fa559b Mon Sep 17 00:00:00 2001 From: Doctorwu Date: Tue, 5 Mar 2024 21:40:35 +0800 Subject: [PATCH 3/8] fix: correct output --- packages/compiler-sfc/src/compileScript.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index b28f33a8610..28bb44cdecc 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -992,9 +992,13 @@ export function compileScript( ctx.s.prepend( `import { ${[...ctx.helperImports] .map(h => `${h} as _${h}`) - .join( - ', ', - )} } from ${JSON.stringify(options.templateOptions?.compilerOptions?.runtimeModuleName || 'vue')}\n`, + .join(', ')} } from ${ + options.templateOptions?.compilerOptions?.runtimeModuleName + ? JSON.stringify( + options.templateOptions?.compilerOptions?.runtimeModuleName, + ) + : "'vue'" + }\n`, ) } From 1ebcaf6e5aa5d51af4047697aab536f043890ce4 Mon Sep 17 00:00:00 2001 From: Doctorwu Date: Tue, 5 Mar 2024 21:43:44 +0800 Subject: [PATCH 4/8] feat: extract varible --- packages/compiler-sfc/src/compileScript.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index 28bb44cdecc..4e28fdc82b0 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -989,16 +989,16 @@ export function compileScript( // 11. finalize Vue helper imports if (ctx.helperImports.size > 0) { + const runtimeModule = options.templateOptions?.compilerOptions + ?.runtimeModuleName + ? JSON.stringify( + options.templateOptions?.compilerOptions?.runtimeModuleName, + ) + : `'vue'` ctx.s.prepend( `import { ${[...ctx.helperImports] .map(h => `${h} as _${h}`) - .join(', ')} } from ${ - options.templateOptions?.compilerOptions?.runtimeModuleName - ? JSON.stringify( - options.templateOptions?.compilerOptions?.runtimeModuleName, - ) - : "'vue'" - }\n`, + .join(', ')} } from ${runtimeModule}\n`, ) } From abfd6845faa85f856de08c688f318502975e1cac Mon Sep 17 00:00:00 2001 From: Doctorwu Date: Tue, 5 Mar 2024 22:18:18 +0800 Subject: [PATCH 5/8] feat: add test case --- .../__snapshots__/compileScript.spec.ts.snap | 42 +++++++++++++++++++ .../__tests__/compileScript.spec.ts | 23 ++++++++++ 2 files changed, 65 insertions(+) diff --git a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap index 6efe6fb92af..d882a02fd9f 100644 --- a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap +++ b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap @@ -1362,3 +1362,45 @@ return { get foo() { return foo } } }" `; + +exports[`compileScript > should care about runtimeModuleName 1`] = ` +"import { withAsyncContext as _withAsyncContext } from "npm:vue" + +export default { + async setup(__props, { expose: __expose }) { + __expose(); + +let __temp, __restore + + ;( + ([__temp,__restore] = _withAsyncContext(() => Promise.resolve(1))), + await __temp, + __restore() +) + +return { } +} + +}" +`; + +exports[`compileScript > should care about runtimeModuleName 2`] = ` +"import { withAsyncContext as _withAsyncContext } from "npm:vue" + +export default { + async setup(__props, { expose: __expose }) { + __expose(); + +let __temp, __restore + + ;( + ([__temp,__restore] = _withAsyncContext(() => Promise.resolve(1))), + await __temp, + __restore() +) + +return { } +} + +}" +`; diff --git a/packages/compiler-sfc/__tests__/compileScript.spec.ts b/packages/compiler-sfc/__tests__/compileScript.spec.ts index 2b9acbc7fd2..efc5af522e7 100644 --- a/packages/compiler-sfc/__tests__/compileScript.spec.ts +++ b/packages/compiler-sfc/__tests__/compileScript.spec.ts @@ -1472,3 +1472,26 @@ describe('SFC genDefaultAs', () => { }) }) }) + +describe('compileScript', () => { + test('should care about runtimeModuleName', () => { + const { content } = compile( + ` + + `, + { + templateOptions: { + compilerOptions: { + runtimeModuleName: 'npm:vue', + }, + }, + }, + ) + expect(content).toMatch( + `import { withAsyncContext as _withAsyncContext } from "npm:vue"\n`, + ) + assertCode(content) + }) +}) From f9ac651ca30bf41540068b1d950cd31f12ea7a00 Mon Sep 17 00:00:00 2001 From: Doctorwu Date: Tue, 5 Mar 2024 22:20:43 +0800 Subject: [PATCH 6/8] chore: clear snapshot --- .../__snapshots__/compileScript.spec.ts.snap | 21 ------------------- 1 file changed, 21 deletions(-) diff --git a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap index d882a02fd9f..b557919cb96 100644 --- a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap +++ b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap @@ -1383,24 +1383,3 @@ return { } }" `; - -exports[`compileScript > should care about runtimeModuleName 2`] = ` -"import { withAsyncContext as _withAsyncContext } from "npm:vue" - -export default { - async setup(__props, { expose: __expose }) { - __expose(); - -let __temp, __restore - - ;( - ([__temp,__restore] = _withAsyncContext(() => Promise.resolve(1))), - await __temp, - __restore() -) - -return { } -} - -}" -`; From be278fd20b970259266cdcd453ca2067c44809ad Mon Sep 17 00:00:00 2001 From: Evan You Date: Wed, 6 Mar 2024 10:39:24 +0800 Subject: [PATCH 7/8] Update compileScript.ts --- packages/compiler-sfc/src/compileScript.ts | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index 4e28fdc82b0..ae17266ae9f 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -989,12 +989,9 @@ export function compileScript( // 11. finalize Vue helper imports if (ctx.helperImports.size > 0) { - const runtimeModule = options.templateOptions?.compilerOptions - ?.runtimeModuleName - ? JSON.stringify( - options.templateOptions?.compilerOptions?.runtimeModuleName, - ) - : `'vue'` + const runtimeModule = JSON.stringify( + options.templateOptions?.compilerOptions?.runtimeModuleName || 'vue', + ) ctx.s.prepend( `import { ${[...ctx.helperImports] .map(h => `${h} as _${h}`) From a8d84e666bf9a8947d8ec79e802ba41e726f86c3 Mon Sep 17 00:00:00 2001 From: Evan You Date: Wed, 6 Mar 2024 10:43:27 +0800 Subject: [PATCH 8/8] chore: adjust to avoid breaking snapshots --- packages/compiler-sfc/src/compileScript.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index ae17266ae9f..d4131d5c61d 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -989,13 +989,15 @@ export function compileScript( // 11. finalize Vue helper imports if (ctx.helperImports.size > 0) { - const runtimeModule = JSON.stringify( - options.templateOptions?.compilerOptions?.runtimeModuleName || 'vue', - ) + const runtimeModuleName = + options.templateOptions?.compilerOptions?.runtimeModuleName + const importSrc = runtimeModuleName + ? JSON.stringify(runtimeModuleName) + : `'vue'` ctx.s.prepend( `import { ${[...ctx.helperImports] .map(h => `${h} as _${h}`) - .join(', ')} } from ${runtimeModule}\n`, + .join(', ')} } from ${importSrc}\n`, ) }