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(plugin-vue): rewrite default after ts compiled #3591

Merged
merged 5 commits into from Jun 1, 2021
Merged
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
5 changes: 3 additions & 2 deletions packages/playground/vue/CustomBlock.vue
Expand Up @@ -3,19 +3,20 @@
<p class="custom-block">{{ t('hello') }}</p>
</template>

<script>
<script lang="ts">
import { getCurrentInstance } from 'vue'

function useI18n(locale = 'en') {
const instance = getCurrentInstance()
const resources = instance.type.i18n || { en: {} }
const resources = (instance.type as any).i18n || { en: {} }
function t(key) {
const res = resources[locale] || {}
return res[key]
}
return { t }
}

// export default
export default {
setup() {
return { ...useI18n('ja') }
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-vue/package.json
Expand Up @@ -29,7 +29,7 @@
},
"homepage": "https://github.com/vitejs/vite/tree/main/packages/plugin-vue#readme",
"peerDependencies": {
"@vue/compiler-sfc": "^3.0.6"
"@vue/compiler-sfc": "^3.0.8"
},
"devDependencies": {
"@rollup/pluginutils": "^4.1.0",
Expand Down
18 changes: 2 additions & 16 deletions packages/plugin-vue/src/main.ts
Expand Up @@ -225,9 +225,6 @@ async function genTemplateCode(
}
}

const exportDefaultClassRE =
/(?:(?:^|\n|;)\s*)export\s+default\s+class\s+([\w$]+)/

async function genScriptCode(
descriptor: SFCDescriptor,
options: ResolvedOptions,
Expand All @@ -247,19 +244,7 @@ async function genScriptCode(
(!script.lang || (script.lang === 'ts' && options.devServer)) &&
!script.src
) {
// TODO remove the class check logic after upgrading @vue/compiler-sfc
const classMatch = script.content.match(exportDefaultClassRE)
if (classMatch) {
scriptCode =
script.content.replace(exportDefaultClassRE, `\nclass $1`) +
`\nconst _sfc_main = ${classMatch[1]}`
if (/export\s+default/.test(scriptCode)) {
// fallback if there are still export default
scriptCode = rewriteDefault(script.content, `_sfc_main`)
}
} else {
scriptCode = rewriteDefault(script.content, `_sfc_main`)
}
scriptCode = script.content
map = script.map
if (script.lang === 'ts') {
const result = await options.devServer!.transformWithEsbuild(
Expand All @@ -271,6 +256,7 @@ async function genScriptCode(
scriptCode = result.code
map = result.map
}
scriptCode = rewriteDefault(scriptCode, `_sfc_main`)
} else {
if (script.src) {
await linkSrcToDescriptor(script.src, descriptor, pluginContext)
Expand Down