@@ -11,7 +11,7 @@ interface IMeta {
1111interface IVisitorPass {
1212 opts ?: {
1313 filename : string ;
14- meta : IMeta ;
14+ meta ? : IMeta ;
1515 }
1616 filename ?: string ;
1717}
@@ -40,22 +40,22 @@ const injectMetaVisitor: Visitor<IInjectVisitorPass> = {
4040 ( BabelTypes . isStringLiteral ( property ) && property . value === 'pluginName' )
4141 ) {
4242 const meta = pass . meta ;
43- if ( meta . pluginName ) {
43+ pass . cache . pluginNameAssignmentExists = true ;
44+ if ( meta ?. pluginName ) {
4445 path . node . right = BabelTypes . stringLiteral ( meta . pluginName ) ;
4546 addInfoComment ( path . node ) ;
46- pass . cache . pluginNameAssignmentExists = true ;
4747 }
4848 }
4949 if (
5050 ( BabelTypes . isIdentifier ( property ) && property . name === 'meta' ) ||
5151 ( BabelTypes . isStringLiteral ( property ) && property . value === 'meta' )
5252 ) {
5353 const meta = pass . meta ;
54- if ( meta . meta ) {
54+ pass . cache . metaAssignmentExists = true ;
55+ if ( meta ?. meta ) {
5556 const generateAst = template ( `const a = ${ JSON . stringify ( meta . meta ) } ` ) ( ) as BabelTypes . VariableDeclaration ;
5657 path . node . right = generateAst . declarations [ 0 ] . init ;
5758 addInfoComment ( path . node ) ;
58- pass . cache . metaAssignmentExists = true ;
5959 }
6060 }
6161 }
@@ -78,13 +78,13 @@ export default function (babel: { types: typeof BabelTypes }): {
7878 if ( t . isIdentifier ( path . node . declaration ) ) {
7979 const declarationName = path . node . declaration . name ;
8080 path . parentPath . traverse ( injectMetaVisitor , { ...state . opts , declarationName, cache } )
81- if ( ! cache . pluginNameAssignmentExists ) {
82- const generatedNode = template ( `${ declarationName } .pluginName = "${ state . opts . meta . pluginName } "` ) ( ) ;
81+ if ( ! cache . pluginNameAssignmentExists && state . opts ?. meta ?. pluginName ) {
82+ const generatedNode = template ( `${ declarationName } .pluginName = "${ state . opts ? .meta ? .pluginName } "` ) ( ) ;
8383 addInfoComment ( generatedNode ) ;
8484 path . insertBefore ( generatedNode ) ;
8585 }
86- if ( ! cache . metaAssignmentExists ) {
87- const generatedNode = template ( `${ declarationName } .meta = ${ JSON . stringify ( state . opts . meta . meta ) } ` ) ( ) ;
86+ if ( ! cache . metaAssignmentExists && state . opts ?. meta ?. meta ) {
87+ const generatedNode = template ( `${ declarationName } .meta = ${ JSON . stringify ( state . opts ? .meta ? .meta ) } ` ) ( ) ;
8888 addInfoComment ( generatedNode ) ;
8989 path . insertBefore ( generatedNode ) ;
9090 }
0 commit comments