Skip to content

Commit

Permalink
Support default exports. (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Skelton authored Jul 28, 2022
1 parent 7670f6f commit fb5e8d1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/small-comics-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'i18next-async-backend': patch
---

Support ESModule default exports.
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { BackendModule, ReadCallback, Services } from 'i18next'

type ResourceFetcher = () => Promise<Record<string, string>>
type ResourceFetcher = () => Promise<{
__esModule?: true
default?: Record<string, unknown>
[key: string]: unknown
}>

export interface AsyncBackendOptions {
resources?: {
Expand Down Expand Up @@ -31,7 +35,7 @@ export default class AsyncBackend

if (resourceFetcher) {
resourceFetcher()
.then((resource) => callback(null, resource))
.then((res) => callback(null, res.__esModule ? res.default : res))
.catch((err) => callback(err, false))
} else {
callback(new Error('resource not found'), false)
Expand Down
8 changes: 8 additions & 0 deletions test/AsyncBackend.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,11 @@ it('should load multiple languages', async () => {
expect(t('ns1:fruit')).toBe('Manzanas')
expect(t('ns2:fruit')).toBe('Naranjas')
})

it('should parse ESM default exports', async () => {
const { t } = await init(['translation'], {
en: () => Promise.resolve({ __esModule: true, default: { foo: 'bar' } }),
})

expect(t('foo')).toBe('bar')
})

0 comments on commit fb5e8d1

Please sign in to comment.