Skip to content

Commit

Permalink
Fix types (#893)
Browse files Browse the repository at this point in the history
* Improve config loader types

* Add missing config type

* Remove excess properties

* Fix config merge

* Update index.ts
  • Loading branch information
Dragate committed Aug 18, 2022
1 parent b2bda20 commit 66abd43
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export interface I18nConfig {
loader?: boolean
logBuild?: boolean
revalidate?: number
pagesInDir?: string
interpolation?: {
format?: Function
prefix: string
Expand Down
44 changes: 27 additions & 17 deletions src/plugin/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { NextConfig } from 'next'
import type { I18nConfig } from '..'
import { hasHOC } from './utils'

export default function nextTranslate(nextConfig: any = {}) {
export default function nextTranslate(nextConfig: NextConfig = {}): NextConfig {
const fs = require('fs')
const path = require('path')
const test = /\.(tsx|ts|js|mjs|jsx)$/
Expand All @@ -10,16 +12,24 @@ export default function nextTranslate(nextConfig: any = {}) {
path.relative(pkgDir(), process.env.NEXT_TRANSLATE_PATH || '.')
)

const i18n = nextConfig.i18n || {}
let {
locales,
defaultLocale,
loader = true,
pagesInDir,
loadLocaleFrom,
localesToIgnore,
pages,
logger,
loggerEnvironment,
staticsHoc,
extensionsRgx,
loader = true,
logBuild,
revalidate,
pagesInDir,
interpolation,
keySeparator,
nsSeparator,
defaultNS,
...restI18n
} = require(path.join(dir, 'i18n'))
} = require(path.join(dir, 'i18n')) as I18nConfig & NextConfig["i18n"]

let hasGetInitialPropsOnAppJs = false

Expand All @@ -46,15 +56,15 @@ export default function nextTranslate(nextConfig: any = {}) {
!!code.match(/\WgetInitialProps\W/g) || hasHOC(code)
}

const i18n = {
...(nextConfig.i18n || {}),
...restI18n,
}

return {
...nextConfig,
i18n: {
...i18n,
...restI18n,
locales,
defaultLocale,
},
webpack(conf: any, options: Record<string, any>) {
i18n,
webpack(conf, options) {
const config =
typeof nextConfig.webpack === 'function'
? nextConfig.webpack(conf, options)
Expand All @@ -75,12 +85,12 @@ export default function nextTranslate(nextConfig: any = {}) {
use: {
loader: 'next-translate/plugin/loader',
options: {
extensionsRgx: restI18n.extensionsRgx || test,
revalidate: restI18n.revalidate || 0,
extensionsRgx: extensionsRgx || test,
revalidate: revalidate || 0,
hasGetInitialPropsOnAppJs,
hasAppJs: !!app,
pagesPath: path.join(pagesPath, '/'),
hasLoadLocaleFrom: typeof restI18n.loadLocaleFrom === 'function',
hasLoadLocaleFrom: typeof loadLocaleFrom === 'function',
},
},
})
Expand Down

0 comments on commit 66abd43

Please sign in to comment.