Skip to content

Commit

Permalink
feat: add basePath as config option
Browse files Browse the repository at this point in the history
  • Loading branch information
myasteiner committed Aug 29, 2023
1 parent e699416 commit b2fcecf
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
29 changes: 28 additions & 1 deletion __tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import nextTranslate from '../src/index'
import fs from 'fs'
import path from 'path'

jest.spyOn(fs, 'readdirSync')

Expand Down Expand Up @@ -47,5 +46,33 @@ describe('nextTranslate', () => {
})
)
})

test('should be able to set the base dir in the next config', () => {
fs.readdirSync.mockImplementationOnce(() => [])

const config = nextTranslate({
nextTranslate: {
basePath: process.cwd(),
},
})

expect(config.webpack({})).toEqual(
expect.objectContaining({
module: {
rules: expect.arrayContaining([
expect.objectContaining({
use: expect.objectContaining({
loader: 'next-translate-plugin/loader',
options: expect.objectContaining({
appFolder: 'src/app/',
pagesFolder: 'src/pages/',
}),
}),
}),
]),
},
})
)
})
})
})
8 changes: 5 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ import {
existPages,
existLocalesFolderWithNamespaces,
} from './utils'
import { LoaderOptions } from './types'
import { LoaderOptions, NextConfigWithNextTranslate } from './types'
import type { I18nConfig, NextI18nConfig } from 'next-translate'

const test = /\.(tsx|ts|js|mjs|jsx)$/

function nextTranslate(nextConfig: NextConfig = {}): NextConfig {
let basePath = pkgDir()
function nextTranslate(config: NextConfigWithNextTranslate = {}): NextConfig {
const { nextTranslate = {}, ...nextConfig } = config

let basePath = nextTranslate.basePath ?? pkgDir()

// NEXT_TRANSLATE_PATH env is supported both relative and absolute path
basePath = path.resolve(
Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type ts from 'typescript'
import type { NextConfig } from 'next'

export interface LoaderOptions {
basePath: string
Expand All @@ -24,3 +25,9 @@ export interface ParsedFilePkg {
transform: (transformer: Transformer) => void
getCode: () => string
}

export interface NextConfigWithNextTranslate extends NextConfig {
nextTranslate?: {
basePath?: string
}
}

0 comments on commit b2fcecf

Please sign in to comment.