|
| 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 | +} |