Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(vue-plugin): support syntax 'export { default } from ...' in vue script block #8016

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/playground/vue/ExportDefault.vue
@@ -0,0 +1,7 @@
<template>
<div class="export-default">{{ count }}</div>
</template>

<script>
export { default } from './exportDefaultTest'
</script>
2 changes: 2 additions & 0 deletions packages/playground/vue/Main.vue
Expand Up @@ -21,6 +21,7 @@
<ReactivityTransform :foo="time" />
<SetupImportTemplate />
<WorkerTest />
<ExportDefault />
</template>

<script setup lang="ts">
Expand All @@ -37,6 +38,7 @@ import AsyncComponent from './AsyncComponent.vue'
import ReactivityTransform from './ReactivityTransform.vue'
import SetupImportTemplate from './setup-import-template/SetupImportTemplate.vue'
import WorkerTest from './worker.vue'
import ExportDefault from './ExportDefault.vue'
import { ref } from 'vue'

const time = ref('loading...')
Expand Down
6 changes: 6 additions & 0 deletions packages/playground/vue/__tests__/vue.spec.ts
Expand Up @@ -248,3 +248,9 @@ describe('vue worker', () => {
expect(await page.textContent('.vue-worker')).toMatch('worker load!')
})
})

describe('export default', () => {
test(`should support the syntax 'export {default} from' in script block`, async () => {
expect(await page.textContent('.export-default')).toMatch('1')
})
})
10 changes: 10 additions & 0 deletions packages/playground/vue/exportDefaultTest.ts
@@ -0,0 +1,10 @@
import { ref } from 'vue'

export default {
setup() {
const count = ref(1)
return {
count
}
}
}
8 changes: 7 additions & 1 deletion packages/plugin-vue/src/main.ts
Expand Up @@ -17,6 +17,8 @@ import { createRollupError } from './utils/error'
import { transformWithEsbuild } from 'vite'
import { EXPORT_HELPER_ID } from './helper'

const exportDefaultRE = /(^|\n|;)\s*export\s*\{\s*default\s*\}\s*from/

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export async function transformMain(
code: string,
Expand Down Expand Up @@ -267,7 +269,11 @@ async function genScriptCode(
if (script) {
// If the script is js/ts and has no external src, it can be directly placed
// in the main module.
if ((!script.lang || script.lang === 'ts') && !script.src) {
if (
(!script.lang || script.lang === 'ts') &&
!script.src &&
!exportDefaultRE.test(script.content)
) {
scriptCode = options.compiler.rewriteDefault(
script.content,
'_sfc_main',
Expand Down