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

feat(compiler-sfc): add option filenameAsComponentName to SFCScriptCompileOptions #6306

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ export interface SFCScriptCompileOptions {
* options passed to `compiler-dom`.
*/
templateOptions?: Partial<SFCTemplateCompileOptions>
/**
* Enable/disable automatically infer component name from filename when using script setup.
* https://github.com/vuejs/core/pull/4997
* Defaults to true.
*/
filenameAsComponentName?: boolean
}

export interface ImportBinding {
Expand Down Expand Up @@ -153,6 +159,7 @@ export function compileScript(
!!options.reactivityTransform || !!options.propsDestructureTransform
const isProd = !!options.isProd
const genSourceMap = options.sourceMap !== false
const filenameAsComponentName = options.filenameAsComponentName !== false
let refBindings: string[] | undefined

if (!options.id) {
Expand Down Expand Up @@ -1465,7 +1472,12 @@ export function compileScript(

// 11. finalize default export
let runtimeOptions = ``
if (!hasDefaultExportName && filename && filename !== DEFAULT_FILENAME) {
if (
filenameAsComponentName &&
!hasDefaultExportName &&
filename &&
filename !== DEFAULT_FILENAME
) {
const match = filename.match(/([^/\\]+)\.\w+$/)
if (match) {
runtimeOptions += `\n __name: '${match[1]}',`
Expand Down