@@ -81,9 +81,15 @@ function convertToUmd(args, initialContents) {
8181// Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules
8282function convertToESM ( args , initialContents ) {
8383 const replaceGoogExtendWithExports = ( contents ) => {
84- return contents . replace ( / g o o g \. o b j e c t \. e x t e n d \( e x p o r t s , ( [ \w \. ] + ) \) ; / g, ( _ , packageName ) => {
84+ const symbols = [ ] ;
85+ let exportVariable ;
86+ let packageName ;
87+
88+ contents = contents . replace ( / g o o g \. o b j e c t \. e x t e n d \( e x p o r t s , ( [ \w \. ] + ) \) ; / g, ( _ , p ) => {
89+ packageName = p ;
90+ exportVariable = contents . includes ( 'const grpc = {}' ) ? 'exportVariable' : packageName ;
91+
8592 const exportSymbols = / g o o g \. e x p o r t S y m b o l \( ' ( [ \w \. ] + ) ' , .* \) ; / g;
86- const symbols = [ ] ;
8793
8894 let match ;
8995 while ( ( match = exportSymbols . exec ( initialContents ) ) ) {
@@ -95,15 +101,25 @@ function convertToESM(args, initialContents) {
95101 }
96102 }
97103
98- return `export const { ${ symbols . join ( ', ' ) } } = ${ packageName } ` ;
104+ return `export const { ${ symbols . join ( ', ' ) } } = ${ exportVariable } ` ;
99105 } ) ;
106+
107+ return symbols . reduce (
108+ ( contents , symbol ) => { return contents . replace (
109+ new RegExp ( `${ packageName } \\.${ symbol } ` , 'g' ) , `${ exportVariable } .${ symbol } ` ) } ,
110+ contents )
100111 } ;
101112
102113 const replaceCMDefaultExportWithExports = ( contents ) => {
103- return contents . replace ( / m o d u l e .e x p o r t s = ( [ \w \. ] + ) \; / g, ( _ , packageName ) => {
104- const exportSymbols = new RegExp ( `${ packageName . replace ( '.' , '\\.' ) } \.([\\w\\.]+) =` , 'g' ) ;
114+ const symbols = [ ] ;
115+ let exportVariable ;
116+ let packageName ;
105117
106- const symbols = [ ] ;
118+ contents = contents . replace ( / m o d u l e .e x p o r t s = ( [ \w \. ] + ) \; / g, ( _ , p ) => {
119+ packageName = p ;
120+ exportVariable = contents . includes ( 'const grpc = {}' ) ? 'exportVariable' : packageName ;
121+
122+ const exportSymbols = new RegExp ( `${ packageName . replace ( '.' , '\\.' ) } \.([\\w\\.]+) =` , 'g' ) ;
107123
108124 let match ;
109125 while ( ( match = exportSymbols . exec ( initialContents ) ) ) {
@@ -115,8 +131,13 @@ function convertToESM(args, initialContents) {
115131 }
116132 }
117133
118- return `export const { ${ symbols . join ( ', ' ) } } = ${ packageName } ;` ;
134+ return `export const { ${ symbols . join ( ', ' ) } } = ${ exportVariable } ;` ;
119135 } ) ;
136+
137+ return symbols . reduce (
138+ ( contents , symbol ) => { return contents . replace (
139+ new RegExp ( `${ packageName } \\.${ symbol } ` , 'g' ) , `${ exportVariable } .${ symbol } ` ) } ,
140+ contents )
120141 } ;
121142
122143 const replaceRequiresWithImports = ( contents ) => {
@@ -151,7 +172,8 @@ function convertToESM(args, initialContents) {
151172 replaceCMDefaultExportWithExports ,
152173 replaceCJSExportsWithECMAExports ,
153174 ] ;
154- return transformations . reduce ( ( currentContents , transform ) => {
175+
176+ return `const exportVariable = {}\n` + transformations . reduce ( ( currentContents , transform ) => {
155177 return transform ( currentContents ) ;
156178 } , initialContents ) ;
157179}
0 commit comments