Skip to content

Commit e76d743

Browse files
authored
fix(compiler-sfc): use options module name if options provide runtimeModuleName options (#10457)
close #10454
1 parent f66a75e commit e76d743

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,3 +1362,24 @@ return { get foo() { return foo } }
13621362
13631363
}"
13641364
`;
1365+
1366+
exports[`compileScript > should care about runtimeModuleName 1`] = `
1367+
"import { withAsyncContext as _withAsyncContext } from "npm:vue"
1368+
1369+
export default {
1370+
async setup(__props, { expose: __expose }) {
1371+
__expose();
1372+
1373+
let __temp, __restore
1374+
1375+
;(
1376+
([__temp,__restore] = _withAsyncContext(() => Promise.resolve(1))),
1377+
await __temp,
1378+
__restore()
1379+
)
1380+
1381+
return { }
1382+
}
1383+
1384+
}"
1385+
`;

packages/compiler-sfc/__tests__/compileScript.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,3 +1472,26 @@ describe('SFC genDefaultAs', () => {
14721472
})
14731473
})
14741474
})
1475+
1476+
describe('compileScript', () => {
1477+
test('should care about runtimeModuleName', () => {
1478+
const { content } = compile(
1479+
`
1480+
<script setup>
1481+
await Promise.resolve(1)
1482+
</script>
1483+
`,
1484+
{
1485+
templateOptions: {
1486+
compilerOptions: {
1487+
runtimeModuleName: 'npm:vue',
1488+
},
1489+
},
1490+
},
1491+
)
1492+
expect(content).toMatch(
1493+
`import { withAsyncContext as _withAsyncContext } from "npm:vue"\n`,
1494+
)
1495+
assertCode(content)
1496+
})
1497+
})

packages/compiler-sfc/src/compileScript.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -989,10 +989,15 @@ export function compileScript(
989989

990990
// 11. finalize Vue helper imports
991991
if (ctx.helperImports.size > 0) {
992+
const runtimeModuleName =
993+
options.templateOptions?.compilerOptions?.runtimeModuleName
994+
const importSrc = runtimeModuleName
995+
? JSON.stringify(runtimeModuleName)
996+
: `'vue'`
992997
ctx.s.prepend(
993998
`import { ${[...ctx.helperImports]
994999
.map(h => `${h} as _${h}`)
995-
.join(', ')} } from 'vue'\n`,
1000+
.join(', ')} } from ${importSrc}\n`,
9961001
)
9971002
}
9981003

0 commit comments

Comments
 (0)