@@ -606,7 +606,7 @@ function parseTopScope(j: JSCodeshift, ast: Node, value: string[], action: strin
606
606
607
607
// eslint-disable-next-line @typescript-eslint/no-unused-vars
608
608
function parseMerge ( j : JSCodeshift , ast : Node , value : string [ ] , action : string ) : boolean | Node {
609
- function createMergeProperty ( p : Node ) : boolean {
609
+ function createMergeProperty ( p : Node , configIdentifier : string ) : boolean {
610
610
// FIXME Use j.callExp()
611
611
const exportsDecl : Node [ ] = ( p . value as Node ) . body . map (
612
612
( n : Node ) : Node => {
@@ -626,14 +626,35 @@ function parseMerge(j: JSCodeshift, ast: Node, value: string[], action: string):
626
626
type : "MemberExpression"
627
627
} ,
628
628
operator : "=" ,
629
- right : j . callExpression ( j . identifier ( "merge" ) , [ j . identifier ( value [ 0 ] ) , exportsDecl . pop ( ) ] ) ,
629
+ right : j . callExpression ( j . identifier ( "merge" ) , [ j . identifier ( configIdentifier ) , exportsDecl . pop ( ) ] ) ,
630
630
type : "AssignmentExpression"
631
631
} ;
632
632
( p . value as Node ) . body [ bodyLength - 1 ] = newVal ;
633
633
return false ; // TODO: debug later
634
634
}
635
+
636
+ function addMergeImports ( ast : Node , mergeImports : string [ ] ) {
637
+ ast . find ( j . Program ) . filter ( ( program : Node ) : boolean => {
638
+ mergeImports . forEach (
639
+ ( imp : string ) : void => {
640
+ if ( ( program . value as Node ) . body . indexOf ( imp ) === - 1 ) {
641
+ ( program . value as Node ) . body . splice ( - 1 , 0 , imp ) ;
642
+ }
643
+ }
644
+ )
645
+ return false ;
646
+ } ) ;
647
+ return ast ;
648
+ }
649
+
635
650
if ( value ) {
636
- return ast . find ( j . Program ) . filter ( ( p : Node ) : boolean => createMergeProperty ( p ) ) ;
651
+ const [ configIdentifier , configPath ] = value ;
652
+ const mergeImports = [
653
+ `const ${ configIdentifier } = require('${ configPath } ')` ,
654
+ `const merge = require('webpack-merge')`
655
+ ] ;
656
+ ast = addMergeImports ( ast , mergeImports ) ;
657
+ return ast . find ( j . Program ) . filter ( ( p : Node ) : boolean => createMergeProperty ( p , configIdentifier ) ) ;
637
658
} else {
638
659
return ast ;
639
660
}
0 commit comments