@@ -40,7 +40,8 @@ type NamespaceMap = Map<
4040export function createFakeJsPlugin ( {
4141 dtsInput,
4242 sourcemap,
43- } : Pick < OptionsResolved , 'dtsInput' | 'sourcemap' > ) : Plugin {
43+ cjsDefault,
44+ } : Pick < OptionsResolved , 'dtsInput' | 'sourcemap' | 'cjsDefault' > ) : Plugin {
4445 let symbolIdx = 0
4546 const identifierMap : Record < string , number > = Object . create ( null )
4647 const symbolMap = new Map < number /* symbol id */ , SymbolInfo > ( )
@@ -264,7 +265,12 @@ export function createFakeJsPlugin({
264265 program . body = program . body
265266 . map ( ( node ) => {
266267 if ( isHelperImport ( node ) ) return null
267- if ( patchImportExport ( node , typeOnlyIds ) ) return node
268+
269+ const newNode = patchImportExport ( node , typeOnlyIds , cjsDefault )
270+ if ( newNode ) {
271+ return newNode
272+ }
273+
268274 if ( node . type !== 'VariableDeclaration' ) return node
269275
270276 const [ decl ] = node . declarations
@@ -523,7 +529,11 @@ function isHelperImport(node: t.Node) {
523529}
524530
525531// patch `.d.ts` suffix in import source to `.js`
526- function patchImportExport ( node : t . Node , typeOnlyIds : string [ ] ) {
532+ function patchImportExport (
533+ node : t . Node ,
534+ typeOnlyIds : string [ ] ,
535+ cjsDefault : boolean ,
536+ ) : t . Statement | undefined {
527537 if (
528538 isTypeOf ( node , [
529539 'ImportDeclaration' ,
@@ -546,7 +556,22 @@ function patchImportExport(node: t.Node, typeOnlyIds: string[]) {
546556
547557 if ( node . source ?. value && RE_DTS . test ( node . source . value ) ) {
548558 node . source . value = filename_dts_to ( node . source . value , 'js' )
549- return true
559+ return node
560+ }
561+
562+ if (
563+ cjsDefault &&
564+ node . type === 'ExportNamedDeclaration' &&
565+ ! node . source &&
566+ node . specifiers . length === 1 &&
567+ node . specifiers [ 0 ] . type === 'ExportSpecifier' &&
568+ resolveString ( node . specifiers [ 0 ] . exported ) === 'default'
569+ ) {
570+ const defaultExport = node . specifiers [ 0 ] as t . ExportSpecifier
571+ return {
572+ type : 'TSExportAssignment' ,
573+ expression : defaultExport . local ,
574+ }
550575 }
551576 }
552577}
0 commit comments