Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1e4a1f4

Browse files
authored
feat: addexportExpose (#376)
1 parent66f18d7 commit1e4a1f4

35 files changed

+625
-3
lines changed

‎.changeset/cool-badgers-hang.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
'unplugin-vue-macros':minor
3+
'@vue-macros/export-expose':patch
4+
'@vue-macros/volar':minor
5+
---
6+
7+
- Disable`exportExpose` and`exportProps` by default.
8+
- Introduce`exportExpose`.
9+
- Add`include` option for Volar plugins (`exportExpose` and`exportProps`).

‎docs/guide/configurations.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ All features are enabled by default except the following.
88
-`defineSlots` (Vue >= 3.3)
99
-`hoistStatic` (Vue >= 3.3)
1010
-`shortEmits` (Vue >= 3.3)
11+
-`exportExpose`
12+
-`exportProps`
1113

1214
You can disable them by setting the option to`false`.
1315

‎packages/export-expose/README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#@vue-macros/export-expose[![npm](https://img.shields.io/npm/v/@vue-macros/export-expose.svg)](https://npmjs.com/package/@vue-macros/export-expose)
2+
3+
Please refer to[README.md](https://github.com/sxzz/vue-macros#readme)
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
{
2+
"name":"@vue-macros/export-expose",
3+
"version":"0.0.0",
4+
"packageManager":"pnpm@8.5.1",
5+
"description":"export-expose feature from Vue Macros.",
6+
"keywords": [
7+
"vue-macros",
8+
"macros",
9+
"vue",
10+
"sfc",
11+
"setup",
12+
"script-setup",
13+
"export-expose",
14+
"unplugin"
15+
],
16+
"license":"MIT",
17+
"homepage":"https://github.com/sxzz/vue-macros#readme",
18+
"bugs": {
19+
"url":"https://github.com/sxzz/vue-macros/issues"
20+
},
21+
"repository": {
22+
"type":"git",
23+
"url":"git+https://github.com/sxzz/vue-macros.git",
24+
"directory":"packages/export-expose"
25+
},
26+
"author":"三咲智子 <sxzz@sxzz.moe>",
27+
"files": [
28+
"dist",
29+
"*.d.ts"
30+
],
31+
"main":"dist/index.js",
32+
"module":"dist/index.mjs",
33+
"types":"dist/index.d.ts",
34+
"exports": {
35+
".": {
36+
"dev":"./src/index.ts",
37+
"types":"./dist/index.d.ts",
38+
"require":"./dist/index.js",
39+
"import":"./dist/index.mjs"
40+
},
41+
"./api": {
42+
"dev":"./src/api.ts",
43+
"types":"./dist/api.d.ts",
44+
"require":"./dist/api.js",
45+
"import":"./dist/api.mjs"
46+
},
47+
"./esbuild": {
48+
"dev":"./src/esbuild.ts",
49+
"types":"./dist/esbuild.d.ts",
50+
"require":"./dist/esbuild.js",
51+
"import":"./dist/esbuild.mjs"
52+
},
53+
"./rollup": {
54+
"dev":"./src/rollup.ts",
55+
"types":"./dist/rollup.d.ts",
56+
"require":"./dist/rollup.js",
57+
"import":"./dist/rollup.mjs"
58+
},
59+
"./vite": {
60+
"dev":"./src/vite.ts",
61+
"types":"./dist/vite.d.ts",
62+
"require":"./dist/vite.js",
63+
"import":"./dist/vite.mjs"
64+
},
65+
"./webpack": {
66+
"dev":"./src/webpack.ts",
67+
"types":"./dist/webpack.d.ts",
68+
"require":"./dist/webpack.js",
69+
"import":"./dist/webpack.mjs"
70+
},
71+
"./*": [
72+
"./*",
73+
"./*.d.ts"
74+
]
75+
},
76+
"typesVersions": {
77+
"<=4.9": {
78+
"*": [
79+
"./dist/*",
80+
"./*"
81+
]
82+
}
83+
},
84+
"scripts": {
85+
"build":"tsup && tsx ../../scripts/postbuild.mts",
86+
"dev":"DEV=true tsup"
87+
},
88+
"peerDependencies": {
89+
"vue":"^2.7.0 || ^3.2.25"
90+
},
91+
"dependencies": {
92+
"@vue-macros/common":"workspace:~",
93+
"@vue/compiler-sfc":"^3.3.4",
94+
"unplugin":"^1.3.1"
95+
},
96+
"devDependencies": {
97+
"vue":"^3.3.4"
98+
},
99+
"engines": {
100+
"node":">=16.14.0"
101+
}
102+
}

‎packages/export-expose/src/api.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export*from'./core'
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import{
2+
HELPER_PREFIX,
3+
MagicString,
4+
getTransformResult,
5+
parseSFC,
6+
}from'@vue-macros/common'
7+
import{extractIdentifiers}from'@vue/compiler-sfc'
8+
9+
constMACROS_VAR_PREFIX=`${HELPER_PREFIX}expose_`
10+
11+
exportfunctiontransformExportExpose(code:string,id:string){
12+
const{ scriptSetup, getSetupAst}=parseSFC(code,id)
13+
if(!scriptSetup)return
14+
15+
consts=newMagicString(code)
16+
constnodes=getSetupAst()!.body
17+
constoffset=scriptSetup.loc.start.offset
18+
19+
constexposed:Record<string,string>={}
20+
21+
leti=0
22+
for(conststmtofnodes){
23+
conststart=offset+stmt.start!
24+
constend=start+6/* 'export'.length */
25+
26+
if(stmt.type==='ExportNamedDeclaration'&&stmt.exportKind==='value'){
27+
if(stmt.declaration){
28+
if(
29+
stmt.declaration.type==='VariableDeclaration'&&
30+
!stmt.declaration.declare
31+
){
32+
for(constdeclofstmt.declaration.declarations){
33+
for(constidofextractIdentifiers(decl.id)){
34+
exposed[id.name]=id.name
35+
}
36+
}
37+
}elseif(
38+
(stmt.declaration.type==='FunctionDeclaration'||
39+
stmt.declaration.type==='ClassDeclaration'||
40+
stmt.declaration.type==='TSEnumDeclaration')&&
41+
!stmt.declaration.declare
42+
){
43+
exposed[stmt.declaration.id!.name]=stmt.declaration.id!.name
44+
}
45+
46+
s.remove(start,end)
47+
}else{
48+
for(constspecifierofstmt.specifiers){
49+
letexported:string,local:string
50+
if(specifier.type==='ExportSpecifier'){
51+
if(specifier.exportKind==='type')continue
52+
53+
exported=
54+
specifier.exported.type==='Identifier'
55+
?specifier.exported.name
56+
:specifier.exported.value
57+
58+
if(stmt.source){
59+
// rename variable
60+
local=MACROS_VAR_PREFIX+String(i++)
61+
if(specifier.local.name===exported){
62+
s.overwriteNode(
63+
specifier.local,
64+
`${specifier.local.name} as${local}`,
65+
{ offset}
66+
)
67+
}else{
68+
s.overwriteNode(specifier.exported,local,{ offset})
69+
}
70+
}else{
71+
local=specifier.local.name
72+
}
73+
}elseif(specifier.type==='ExportNamespaceSpecifier'){
74+
local=MACROS_VAR_PREFIX+String(i++)
75+
exported=specifier.exported.name
76+
77+
s.overwriteNode(specifier.exported,local,{ offset})
78+
}elsecontinue
79+
80+
exposed[exported]=local
81+
}
82+
83+
if(stmt.source){
84+
s.overwrite(start,end,'import')
85+
}else{
86+
s.removeNode(stmt,{ offset})
87+
}
88+
}
89+
}elseif(
90+
stmt.type==='ExportAllDeclaration'&&
91+
stmt.exportKind==='value'
92+
){
93+
thrownewError(
94+
'export from another module is not supported. Please import and export separately.'
95+
)
96+
}elseif(stmt.type==='ExportDefaultDeclaration'){
97+
thrownewError(
98+
'export default is not supported. Please use named export.'
99+
)
100+
}
101+
}
102+
103+
if(Object.keys(exposed).length===0)return
104+
105+
letcodegen=''
106+
for(const[exported,local]ofObject.entries(exposed)){
107+
codegen+=`\n `
108+
if(exported===local){
109+
codegen+=`${exported},`
110+
}else{
111+
codegen+=`${exported}:${local},`
112+
}
113+
}
114+
codegen=`defineExpose({${codegen}\n})`
115+
116+
s.prependLeft(scriptSetup.loc.end.offset,`${codegen}\n`)
117+
118+
returngetTransformResult(s,id)
119+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
importunpluginfrom'.'
2+
3+
exportdefaultunplugin.esbuild
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import{createUnplugin}from'unplugin'
2+
import{
3+
typeBaseOptions,
4+
typeMarkRequired,
5+
REGEX_SETUP_SFC,
6+
REGEX_VUE_SFC,
7+
REGEX_VUE_SUB,
8+
createFilter,
9+
detectVueVersion,
10+
}from'@vue-macros/common'
11+
import{transformExportExpose}from'./core'
12+
13+
export{transformExportExposeastransformDefineProps}from'./core'
14+
15+
exporttypeOptions=BaseOptions
16+
exporttypeOptionsResolved=MarkRequired<Options,'include'|'version'>
17+
18+
functionresolveOption(options:Options):OptionsResolved{
19+
constversion=options.version||detectVueVersion()
20+
return{
21+
include:[REGEX_VUE_SFC,REGEX_SETUP_SFC,REGEX_VUE_SUB],
22+
...options,
23+
version,
24+
}
25+
}
26+
27+
constname='unplugin-vue-export-expose'
28+
29+
exportdefaultcreateUnplugin<Options|undefined,false>(
30+
(userOptions={})=>{
31+
constoptions=resolveOption(userOptions)
32+
constfilter=createFilter(options)
33+
34+
return{
35+
name,
36+
enforce:'pre',
37+
38+
transformInclude(id){
39+
returnfilter(id)
40+
},
41+
42+
transform(code,id){
43+
returntransformExportExpose(code,id)
44+
},
45+
}
46+
}
47+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
importunpluginfrom'.'
2+
3+
exportdefaultunplugin.rollup

‎packages/export-expose/src/vite.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
importunpluginfrom'.'
2+
3+
exportdefaultunplugin.vite

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp