cibernox / babel-plugin-precompile-intl

Compile translations in ICU message format to invocable functions at build time
9 stars 4 forks source link

Could not load locales file when default export is not an ObjectExpression #16

Open couetilc opened 2 years ago

couetilc commented 2 years ago

Hi, just running into a small issue when using svelte-intl-precompile, when you have a locale file that declares a default export, it throws an error when the default export is a node of type "Identifier" (e.g. const locale = {}; export default locale; but works as expected when the node is the type "ObjectExpression" (e.g. export default {}).

Here's a minimal reproduction: https://stackblitz.com/edit/sveltejs-kit-template-default-jm1kse?file=locales/en.js

Here's the error

[svelte-intl-precompile] Could not load $locales/en (imported by $locales): /packages/app/src/locales/en.js: Cannot read propert
ies of undefined (reading 'forEach')                                                                                                                    
> Could not load $locales/en (imported by $locales): /packages/app/src/locales/en.js: Cannot read properties of undefined (readi
ng 'forEach')                                                                                                                                           
    at flattenObjectProperties (/node_modules/.pnpm/babel-plugin-precompile-intl@0.3.0/node_modules/babel-plugin-precompile-intl
/src/index.js:169:32)                                                                                                                                   
    at PluginPass.ExportDefaultDeclaration (/Users/connor/wk/ui/main/node_modules/.pnpm/babel-plugin-precompile-intl@0.3.0/node_modules/babel-plugin-pre
compile-intl/src/index.js:216:11)                                                                                                                       
    at newFn (/node_modules/.pnpm/@babel+traverse@7.17.3/node_modules/@babel/traverse/lib/visitors.js:177:21)                   
    at NodePath._call (/node_modules/.pnpm/@babel+traverse@7.17.3/node_modules/@babel/traverse/lib/path/context.js:53:20)       
    at NodePath.call (/node_modules/.pnpm/@babel+traverse@7.17.3/node_modules/@babel/traverse/lib/path/context.js:40:17)        
    at NodePath.visit (/node_modules/.pnpm/@babel+traverse@7.17.3/node_modules/@babel/traverse/lib/path/context.js:100:31)      
    at TraversalContext.visitQueue (/node_modules/.pnpm/@babel+traverse@7.17.3/node_modules/@babel/traverse/lib/context.js:103:1
6)                                                                                                                                                      
    at TraversalContext.visitMultiple (/node_modules/.pnpm/@babel+traverse@7.17.3/node_modules/@babel/traverse/lib/context.js:72
:17)                                                                                                                                                    
    at TraversalContext.visit (/node_modules/.pnpm/@babel+traverse@7.17.3/node_modules/@babel/traverse/lib/context.js:129:19)   
    at traverseNode (/node_modules/.pnpm/@babel+traverse@7.17.3/node_modules/@babel/traverse/lib/traverse-node.js:24:17)  

This is the babel node that throws the error, notice declaration: Node { type: 'Identifier' , ... } is missing the "properties" property?

transforming (170) $locales{                                                                                                                            
  node: Node {                                                                                                                                          
    type: 'ExportDefaultDeclaration',                                                                                                                   
    start: 71,                                                                                                                                          
    end: 89,                                                                                                                                            
    loc: SourceLocation {                                                                                                                               
      start: [Position],                                                                                                                                
      end: [Position],                                                                                                                                  
      filename: undefined,                                                                                                                              
      identifierName: undefined                                                                                                                         
    },                                                                                                                                                  
    declaration: Node {                                                                                                                                 
      type: 'Identifier',                                                                                                                               
      start: 86,                                                                                                                                        
      end: 88,                                                                                                                                          
      loc: [SourceLocation],                                                                                                                            
      name: 'en',                                                                                                                                       
      leadingComments: undefined,                                                                                                                       
      innerComments: undefined,                                                                                                                         
      trailingComments: undefined                                                                                                                       
    },                                                                                                                                                  
    leadingComments: undefined,                                                                                                                         
    innerComments: undefined,                                                                                                                           
    trailingComments: undefined                                                                                                                         
  }                                                                                                                                                     
}                                                                                                                                                       

Works when node type is "ObjectExpression", because it has the "properties" property is an array.

transforming (95) $locales{                                                                                                                             
  node: Node {                                                                                                                                          
    type: 'ExportDefaultDeclaration',                                                                                                                   
    start: 124,                                                                                                                                         
    end: 197,                                                                                                                                           
    loc: SourceLocation {                                                                                                                               
      start: [Position],                                                                                                                                
      end: [Position],                                                                                                                                  
      filename: undefined,                                                                                                                              
      identifierName: undefined                                                                                                                         
    },                                                                                                                                                  
    declaration: Node {                                                                                                                                 
      type: 'ObjectExpression',                                                                                                                         
      start: 139,                                                                                                                                       
      end: 196,                                                                                                                                         
      loc: [SourceLocation],                                                                                                                            
      properties: [Array],                                                                                                                              
      extra: [Object],                                                                                                                                  
      leadingComments: undefined,                                                                                                                       
      innerComments: undefined,                                                                                                                         
      trailingComments: undefined                                                                                                                       
    },                                                                                                                                                  
    leadingComments: [ [Object], [Object], [Object], [Object], [Object], [Object] ],                                                                    
    innerComments: undefined,                                                                                                                           
    trailingComments: undefined                                                                                                                         
  }                                                                                                                                                     
} 

Would be best to handle both types, but just noting it in documentation someplace (website or readme) would be good too.

Thank you :)