@@ -10,7 +10,6 @@ let openTokenDepth,
1010 nextBraceIsClass ,
1111 starExportMap ,
1212 lastStarExportSpecifier ,
13- lastExportsAssignSpecifier ,
1413 _exports ,
1514 reexports ;
1615
@@ -26,7 +25,6 @@ function resetState () {
2625 nextBraceIsClass = false ;
2726 starExportMap = Object . create ( null ) ;
2827 lastStarExportSpecifier = null ;
29- lastExportsAssignSpecifier = null ;
3028
3129 _exports = new Set ( ) ;
3230 reexports = new Set ( ) ;
@@ -49,8 +47,6 @@ module.exports = function parseCJS (source, name = '@') {
4947 e . loc = pos ;
5048 throw e ;
5149 }
52- if ( lastExportsAssignSpecifier )
53- reexports . add ( lastExportsAssignSpecifier ) ;
5450 const result = { exports : [ ..._exports ] , reexports : [ ...reexports ] } ;
5551 resetState ( ) ;
5652 return result ;
@@ -330,8 +326,8 @@ function tryParseObjectDefineOrKeys (keys) {
330326 if ( ch !== 123 /*{*/ ) break ;
331327 pos ++ ;
332328 ch = commentWhitespace ( ) ;
333- if ( ch !== 105 /*i*/ || ! source . startsWith ( 'f ' , pos + 1 ) ) break ;
334- pos += 3 ;
329+ if ( ch !== 105 /*i*/ || source . charCodeAt ( pos + 1 ) !== 102 /*f*/ ) break ;
330+ pos += 2 ;
335331 ch = commentWhitespace ( ) ;
336332 if ( ch !== 40 /*(*/ ) break ;
337333 pos ++ ;
@@ -398,6 +394,61 @@ function tryParseObjectDefineOrKeys (keys) {
398394 }
399395 else break ;
400396
397+ // `if (` IDENTIFIER$2 `in` EXPORTS_IDENTIFIER `&&` EXPORTS_IDENTIFIER `[` IDENTIFIER$2 `] ===` IDENTIFIER$1 `[` IDENTIFIER$2 `]) return` `;`?
398+ if ( ch === 105 /*i*/ && source . charCodeAt ( pos + 1 ) === 102 /*f*/ ) {
399+ pos += 2 ;
400+ ch = commentWhitespace ( ) ;
401+ if ( ch !== 40 /*(*/ ) break ;
402+ pos ++ ;
403+ ch = commentWhitespace ( ) ;
404+ if ( ! source . startsWith ( it_id , pos ) ) break ;
405+ pos += it_id . length ;
406+ ch = commentWhitespace ( ) ;
407+ if ( ch !== 105 /*i*/ || ! source . startsWith ( 'n ' , pos + 1 ) ) break ;
408+ pos += 3 ;
409+ ch = commentWhitespace ( ) ;
410+ if ( ! readExportsOrModuleDotExports ( ch ) ) break ;
411+ ch = commentWhitespace ( ) ;
412+ if ( ch !== 38 /*&*/ || source . charCodeAt ( pos + 1 ) !== 38 /*&*/ ) break ;
413+ pos += 2 ;
414+ ch = commentWhitespace ( ) ;
415+ if ( ! readExportsOrModuleDotExports ( ch ) ) break ;
416+ ch = commentWhitespace ( ) ;
417+ if ( ch !== 91 /*[*/ ) break ;
418+ pos ++ ;
419+ ch = commentWhitespace ( ) ;
420+ if ( ! source . startsWith ( it_id , pos ) ) break ;
421+ pos += it_id . length ;
422+ ch = commentWhitespace ( ) ;
423+ if ( ch !== 93 /*]*/ ) break ;
424+ pos ++ ;
425+ ch = commentWhitespace ( ) ;
426+ if ( ch !== 61 /*=*/ || ! source . startsWith ( '==' , pos + 1 ) ) break ;
427+ pos += 3 ;
428+ ch = commentWhitespace ( ) ;
429+ if ( ! source . startsWith ( id , pos ) ) break ;
430+ pos += id . length ;
431+ ch = commentWhitespace ( ) ;
432+ if ( ch !== 91 /*[*/ ) break ;
433+ pos ++ ;
434+ ch = commentWhitespace ( ) ;
435+ if ( ! source . startsWith ( it_id , pos ) ) break ;
436+ pos += it_id . length ;
437+ ch = commentWhitespace ( ) ;
438+ if ( ch !== 93 /*]*/ ) break ;
439+ pos ++ ;
440+ ch = commentWhitespace ( ) ;
441+ if ( ch !== 41 /*)*/ ) break ;
442+ pos ++ ;
443+ ch = commentWhitespace ( ) ;
444+ if ( ch !== 114 /*r*/ || ! source . startsWith ( 'eturn' , pos + 1 ) ) break ;
445+ pos += 6 ;
446+ ch = commentWhitespace ( ) ;
447+ if ( ch === 59 /*;*/ )
448+ pos ++ ;
449+ ch = commentWhitespace ( ) ;
450+ }
451+
401452 // EXPORTS_IDENTIFIER `[` IDENTIFIER$2 `] =` IDENTIFIER$1 `[` IDENTIFIER$2 `]`
402453 if ( readExportsOrModuleDotExports ( ch ) ) {
403454 ch = commentWhitespace ( ) ;
@@ -622,6 +673,8 @@ function tryParseExportsDotAssign (assign) {
622673 // module.exports =
623674 case 61 /*=*/ : {
624675 if ( assign ) {
676+ if ( reexports . size )
677+ reexports = new Set ( ) ;
625678 pos ++ ;
626679 ch = commentWhitespace ( ) ;
627680 // { ... }
@@ -641,9 +694,9 @@ function tryParseExportsDotAssign (assign) {
641694
642695function tryParseRequire ( requireType ) {
643696 // require('...')
697+ const revertPos = pos ;
644698 if ( source . startsWith ( 'equire' , pos + 1 ) ) {
645699 pos += 7 ;
646- const revertPos = pos - 1 ;
647700 let ch = commentWhitespace ( ) ;
648701 if ( ch === 40 /*(*/ ) {
649702 pos ++ ;
@@ -656,7 +709,7 @@ function tryParseRequire (requireType) {
656709 if ( ch === 41 /*)*/ ) {
657710 switch ( requireType ) {
658711 case ExportAssign :
659- lastExportsAssignSpecifier = source . slice ( reexportStart , reexportEnd ) ;
712+ reexports . add ( source . slice ( reexportStart , reexportEnd ) ) ;
660713 return true ;
661714 case ExportStar :
662715 reexports . add ( source . slice ( reexportStart , reexportEnd ) ) ;
@@ -674,7 +727,7 @@ function tryParseRequire (requireType) {
674727 if ( ch === 41 /*)*/ ) {
675728 switch ( requireType ) {
676729 case ExportAssign :
677- lastExportsAssignSpecifier = source . slice ( reexportStart , reexportEnd ) ;
730+ reexports . add ( source . slice ( reexportStart , reexportEnd ) ) ;
678731 return true ;
679732 case ExportStar :
680733 reexports . add ( source . slice ( reexportStart , reexportEnd ) ) ;
@@ -711,6 +764,17 @@ function tryParseLiteralExports () {
711764 }
712765 addExport ( source . slice ( startPos , endPos ) ) ;
713766 }
767+ else if ( ch === 46 /*.*/ && source . startsWith ( '..' , pos + 1 ) ) {
768+ pos += 3 ;
769+ if ( source . charCodeAt ( pos ) === 114 /*r*/ && tryParseRequire ( ExportAssign ) ) {
770+ pos ++ ;
771+ }
772+ else if ( ! identifier ( ) ) {
773+ pos = revertPos ;
774+ return ;
775+ }
776+ ch = commentWhitespace ( ) ;
777+ }
714778 else if ( ch === 39 /*'*/ || ch === 34 /*"*/ ) {
715779 const startPos = ++ pos ;
716780 if ( identifier ( ) && source . charCodeAt ( pos ) === ch ) {
0 commit comments