Skip to content

Commit

Permalink
fix(compiler-sfc): use options module name if options provide runtime…
Browse files Browse the repository at this point in the history
…ModuleName options (#10457)

close #10454
  • Loading branch information
Doctor-wu authored Mar 6, 2024
1 parent f66a75e commit e76d743
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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 { }
}
}"
`;
23 changes: 23 additions & 0 deletions packages/compiler-sfc/__tests__/compileScript.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1472,3 +1472,26 @@ describe('SFC genDefaultAs', () => {
})
})
})

describe('compileScript', () => {
test('should care about runtimeModuleName', () => {
const { content } = compile(
`
<script setup>
await Promise.resolve(1)
</script>
`,
{
templateOptions: {
compilerOptions: {
runtimeModuleName: 'npm:vue',
},
},
},
)
expect(content).toMatch(
`import { withAsyncContext as _withAsyncContext } from "npm:vue"\n`,
)
assertCode(content)
})
})
7 changes: 6 additions & 1 deletion packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
)
}

Expand Down

0 comments on commit e76d743

Please sign in to comment.