From e76d7430aa7470342f3fe263145a0fa92f5898ca Mon Sep 17 00:00:00 2001 From: Doctor Wu <44631608+Doctor-wu@users.noreply.github.com> Date: Wed, 6 Mar 2024 10:45:42 +0800 Subject: [PATCH] fix(compiler-sfc): use options module name if options provide runtimeModuleName options (#10457) close #10454 --- .../__snapshots__/compileScript.spec.ts.snap | 21 +++++++++++++++++ .../__tests__/compileScript.spec.ts | 23 +++++++++++++++++++ packages/compiler-sfc/src/compileScript.ts | 7 +++++- 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap index 6efe6fb92af..b557919cb96 100644 --- a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap +++ b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap @@ -1362,3 +1362,24 @@ 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 { } +} + +}" +`; 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) + }) +}) diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index 46fc65c0069..d4131d5c61d 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -989,10 +989,15 @@ export function compileScript( // 11. finalize Vue helper imports if (ctx.helperImports.size > 0) { + 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 'vue'\n`, + .join(', ')} } from ${importSrc}\n`, ) }