cibernox / cibernox/babel-plugin-precompile-intl
Could not load locales file when default export is not an ObjectExpression
- Dominant language
- TypeScript
- Stars
- 9
- Forks
- 4
- PR merge metrics
- No merged PRs in 30d
Description
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 :)
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with babel-plugin-precompile-intl/src/index.js, especially flattenObjectProperties and the ExportDefaultDeclaration handler shown in the stack trace. Run the linked StackBlitz reproduction with a default export bound to an identifier, then verify that locale files using either an Identifier or ObjectExpression default export load successfully.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- babel, javascript, typescript
- Domain
- internationalization
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100