Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
141 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,18 @@ | ||
import { type FilterPattern } from '@rollup/pluginutils' | ||
|
||
export interface VolarOptions { | ||
defineModels?: { | ||
unified?: boolean | ||
} | ||
shortVmodel?: { | ||
prefix?: '::' | '$' | '*' | ||
} | ||
exportExpose?: { | ||
include?: FilterPattern | ||
exclude?: FilterPattern | ||
} | ||
exportProps?: { | ||
include?: FilterPattern | ||
exclude?: FilterPattern | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { FileKind } from '@volar/language-core' | ||
import { | ||
FileRangeCapabilities, | ||
type Segment, | ||
type Sfc, | ||
type VueEmbeddedFile, | ||
type VueLanguagePlugin, | ||
replace, | ||
replaceSourceRange, | ||
} from '@volar/vue-language-core' | ||
import { createFilter } from '@rollup/pluginutils' | ||
import { type VolarOptions } from '..' | ||
import { getVolarOptions } from './common' | ||
|
||
function transform({ | ||
fileName, | ||
file, | ||
sfc, | ||
ts, | ||
volarOptions, | ||
}: { | ||
fileName: string | ||
file: VueEmbeddedFile | ||
sfc: Sfc | ||
ts: typeof import('typescript/lib/tsserverlibrary') | ||
volarOptions: NonNullable<VolarOptions['exportExpose']> | ||
}) { | ||
const filter = createFilter( | ||
volarOptions.include || /.*/, | ||
volarOptions.exclude | ||
) | ||
if (!filter(fileName)) return | ||
|
||
const exposed: Record<string, Segment<FileRangeCapabilities>> = {} | ||
for (const stmt of sfc.scriptSetupAst!.statements) { | ||
if (!ts.isVariableStatement(stmt)) continue | ||
const exportModifier = stmt.modifiers?.find( | ||
(m) => m.kind === ts.SyntaxKind.ExportKeyword | ||
) | ||
if (!exportModifier) continue | ||
|
||
const start = exportModifier.getStart(sfc.scriptSetupAst!) | ||
const end = exportModifier.getEnd() | ||
replaceSourceRange(file.content, 'scriptSetup', start, end) | ||
|
||
for (const decl of stmt.declarationList.declarations) { | ||
if (!ts.isIdentifier(decl.name)) continue | ||
const name = decl.name.text | ||
const start = decl.name.getStart(sfc.scriptSetupAst!) | ||
|
||
exposed[name] = [name, 'scriptSetup', start, FileRangeCapabilities.full] | ||
} | ||
} | ||
|
||
if (Object.keys(exposed).length === 0) return | ||
|
||
const exposedStrings = Object.entries(exposed).flatMap(([key, value]) => [ | ||
`${key}: `, | ||
value, | ||
',\n', | ||
]) | ||
|
||
replace( | ||
file.content, | ||
'return {\n', | ||
'return {\n...{ ', | ||
...exposedStrings, | ||
' },\n' | ||
) | ||
} | ||
|
||
const plugin: VueLanguagePlugin = ({ | ||
vueCompilerOptions, | ||
modules: { typescript: ts }, | ||
}) => { | ||
return { | ||
name: 'vue-macros-export-expose', | ||
version: 1, | ||
resolveEmbeddedFile(fileName, sfc, embeddedFile) { | ||
if ( | ||
embeddedFile.kind !== FileKind.TypeScriptHostFile || | ||
!sfc.scriptSetup || | ||
!sfc.scriptSetupAst | ||
) | ||
return | ||
|
||
transform({ | ||
fileName, | ||
file: embeddedFile, | ||
sfc, | ||
ts, | ||
volarOptions: getVolarOptions(vueCompilerOptions)?.exportExpose || {}, | ||
}) | ||
}, | ||
} | ||
} | ||
export = plugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
<script setup lang="ts"> | ||
export const foo = 'foo' | ||
export const bar = 'baz' | ||
export const bar = 'bar' | ||
</script> | ||
|
||
<template>exportExpose child.</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.