Skip to content
This repository has been archived by the owner on Apr 23, 2024. It is now read-only.

Commit

Permalink
feat: update NextJS peer dependencies (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhrebecek authored Apr 2, 2024
1 parent 12b638b commit 1afb36d
Show file tree
Hide file tree
Showing 14 changed files with 111 additions and 198 deletions.
2 changes: 1 addition & 1 deletion example/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ __metadata:
url: ^0.11.3
yamljs: ^0.3.0
peerDependencies:
next: ">11.0.0"
next: ">13.5.0"
react: ">=16.8.0"
languageName: node
linkType: soft
Expand Down
13 changes: 6 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
"format": "yon prettier -c --write .",
"lint": "yon eslint --ignore-path .gitignore .",
"test": "jest",
"prerelease": "yon lint && yon build",
"release": "npx release-it",
"test:watch": "jest --watchAll",
"start": "yon install && yon build && cd ./example && yon install && yon build && yon start"
},
"dependencies": {
Expand All @@ -50,11 +49,11 @@
"@ianvs/prettier-plugin-sort-imports": "^4.2.1",
"@testing-library/react": "^14.2.2",
"@types/jest": "^29.5.12",
"@types/react": "^18.2.67",
"@types/react-dom": "^18.2.22",
"@types/react": "^18.2.73",
"@types/react-dom": "^18.2.23",
"@types/yamljs": "^0.2.34",
"@typescript-eslint/eslint-plugin": "^7.3.1",
"@typescript-eslint/parser": "^7.3.1",
"@typescript-eslint/eslint-plugin": "^7.5.0",
"@typescript-eslint/parser": "^7.5.0",
"babel-jest": "^29.7.0",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
Expand All @@ -74,7 +73,7 @@
"yarn-or-npm": "^3.0.1"
},
"peerDependencies": {
"next": ">11.0.0",
"next": ">=13.5.0",
"react": ">=16.8.0"
},
"publishConfig": {
Expand Down
45 changes: 0 additions & 45 deletions src/plugin/checkNextVersion.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/plugin/createNtrData.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// import type { NextConfig } from 'next/dist/server/config-shared'
import type { NextConfigWithNTR, NTRNextConfig, TNtrData } from '../types'
import { getPagesPath } from './getPagesPath'
import { parsePages } from './parsePages'
Expand Down
23 changes: 10 additions & 13 deletions src/plugin/getRouteBranchReRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
import type { Redirect, Rewrite } from 'next/dist/lib/load-custom-routes'
import { pathToRegexp } from 'path-to-regexp'

import { getNtrData } from '../shared/ntrData'
import { ignoreSegmentPathRegex } from '../shared/regex'
import type { TAnyLocale, TReRoutes, TRouteBranch, TRouteSegment } from '../types'
import { checkNextVersion } from './checkNextVersion'
import { fileNameToPath } from './fileNameToPaths'
import { getLocalePathFromPaths } from './getPathFromPaths'

/** Prevent prefetches redirections and rewrites. See #49 and https://github.com/vercel/next.js/issues/39531 */
const getNextjsDataHeaderCheck = (): Pick<Redirect, 'missing'> | false =>
checkNextVersion('>=13.3.0') && {
missing: [
{
type: 'header',
key: 'x-nextjs-data',
},
],
}
const nextjsDataHeaderCheck: Pick<Redirect, 'missing'> = {
missing: [
{
type: 'header',
key: 'x-nextjs-data',
},
],
}

/** Remove brackets and custom regexp from source to get valid destination */
const sourceToDestination = (sourcePath: string) =>
Expand Down Expand Up @@ -179,7 +176,7 @@ export const getPageReRoutes = <L extends TAnyLocale>(routeSegments: TRouteSegme
permanent: false,
// Take source locale into account
locale: false as const,
...getNextjsDataHeaderCheck(),
...nextjsDataHeaderCheck,
},
]
}, [] as Redirect[]),
Expand Down Expand Up @@ -225,7 +222,7 @@ export const getPageReRoutes = <L extends TAnyLocale>(routeSegments: TRouteSegme
{
source,
destination,
...getNextjsDataHeaderCheck(),
...nextjsDataHeaderCheck,
},
]
}, [] as Rewrite[])
Expand Down
11 changes: 7 additions & 4 deletions src/plugin/routesFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ export const isRoutesFileName = (fileName: string, routesDataFileName?: string)
export const getPagesDir = () => {
// @ts-expect-error: support previous Next versions
const pagesDirs = findPagesDir(process.cwd(), false)
const pagesDir = (pagesDirs as Record<string, string | undefined>).pages || pagesDirs.pagesDir
const pagesDir =
typeof pagesDirs === 'object'
? (pagesDirs as Record<string, string | undefined>).pages || pagesDirs.pagesDir
: pagesDirs

if (!pagesDir) {
throw new Error(
ntrMessagePrefix + '`/pages` directory not found.' + pagesDirs.appDir
? ' next-translate-routes does not support `/app` directory yet.'
: '',
ntrMessagePrefix +
'`/pages` directory not found.' +
(pagesDirs.appDir ? ' edu-next-translate-routes does not support `/app` directory yet.' : ''),
)
}

Expand Down
17 changes: 2 additions & 15 deletions src/plugin/withTranslateRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import type { Redirect, Rewrite } from 'next/dist/lib/load-custom-routes'
import type { NextConfig } from 'next/dist/server/config-shared'
import { type Configuration as WebpackConfiguration, type FileCacheOptions, DefinePlugin } from 'webpack'
import { type Configuration as WebpackConfiguration, type FileCacheOptions } from 'webpack'

import { setNtrData } from '../shared/ntrData'
import { ntrMessagePrefix } from '../shared/withNtrPrefix'
import { NextConfigWithNTR } from '../types'
import { checkNextVersion } from './checkNextVersion'
import { createNtrData } from './createNtrData'
import { getPagesPath } from './getPagesPath'
import { getRouteBranchReRoutes } from './getRouteBranchReRoutes'
Expand Down Expand Up @@ -55,14 +54,10 @@ export const withTranslateRoutes = (userNextConfig: NextConfigWithNTR): NextConf
console.log(ntrMessagePrefix + 'Rewrites:', sortedRewrites)
}

const hasOldRouterContextPath = checkNextVersion('<13.5.0')

return {
...nextConfig,

transpilePackages: hasOldRouterContextPath
? nextConfig.transpilePackages
: [...(nextConfig.transpilePackages || []), '@almacareer/edu-next-translate-routes'],
transpilePackages: [...(nextConfig.transpilePackages || []), '@almacareer/edu-next-translate-routes'],

webpack(conf: WebpackConfiguration, context) {
const config =
Expand All @@ -72,14 +67,6 @@ export const withTranslateRoutes = (userNextConfig: NextConfigWithNTR): NextConf
config.cache.buildDependencies = config.cache.buildDependencies || {}
config.cache.buildDependencies.ntrRoutes = getAllRoutesFiles()

if (!config.plugins) {
config.plugins = []
}
const ROUTER_CONTEXT_PATH = hasOldRouterContextPath
? "'next/dist/shared/lib/router-context'"
: "'next/dist/shared/lib/router-context.shared-runtime'"
config.plugins.push(new DefinePlugin({ ROUTER_CONTEXT_PATH }))

if (!config.module) {
config.module = {}
}
Expand Down
30 changes: 16 additions & 14 deletions src/react/withTranslateRoutes.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { NextComponentType } from 'next'
import { RouterContext } from 'next/dist/shared/lib/router-context.shared-runtime'
import { AppContextType, AppInitialProps } from 'next/dist/shared/lib/utils'
import { NextRouter, useRouter as useNextRouter } from 'next/router'
import React, { useMemo } from 'react'
Expand All @@ -8,8 +9,6 @@ import { ntrMessagePrefix } from '../shared/withNtrPrefix'
import type { TNtrData } from '../types'
import { enhanceNextRouter } from './enhanceNextRouter'

const { RouterContext } = require(ROUTER_CONTEXT_PATH)

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type TWrappedAppComponent = NextComponentType<AppContextType<NextRouter>, AppInitialProps, any>

Expand All @@ -19,21 +18,24 @@ type TWrappedAppComponent = NextComponentType<AppContextType<NextRouter>, AppIni
*/
export const withTranslateRoutes = (...args: (TWrappedAppComponent | TNtrData)[]) => {
// ntrData argument is added as a argument by webpack next-translate-routes/loader, and can also be added manually
const { ntrData, AppComponent } = args.reduce((acc, arg) => {
if (typeof arg === 'function') {
const { ntrData, AppComponent } = args.reduce(
(acc, arg) => {
if (typeof arg === 'function') {
return {
...acc,
AppComponent: arg,
}
}
return {
...acc,
AppComponent: arg,
ntrData: {
...acc.ntrData,
...arg,
},
}
}
return {
...acc,
ntrData: {
...acc.ntrData,
...arg,
},
}
}, {} as { ntrData: TNtrData; AppComponent: TWrappedAppComponent })
},
{} as { ntrData: TNtrData; AppComponent: TWrappedAppComponent },
)

if (!AppComponent) {
throw new Error(ntrMessagePrefix + 'No wrapped App component in withTranslateRoutes')
Expand Down
2 changes: 1 addition & 1 deletion src/shared/withNtrPrefix.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const ntrMessagePrefix = '[next-translate-routes] - '
export const ntrMessagePrefix = '[edu-next-translate-routes] - '
2 changes: 0 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ export type NextConfigCompleteWithNTR = NextConfigComplete & { i18n: NTRI18NConf
declare global {
// eslint-disable-next-line no-var
var __NEXT_TRANSLATE_ROUTES_DATA: TNtrData
// eslint-disable-next-line no-var
var ROUTER_CONTEXT_PATH: string

interface Window {
__NEXT_TRANSLATE_ROUTES_DATA: TNtrData
Expand Down
28 changes: 0 additions & 28 deletions tests/plugin/checkNextVersion.test.ts

This file was deleted.

6 changes: 4 additions & 2 deletions tests/plugin/createNtrData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ const pagesPath = path.resolve(process.cwd(), './tests/fixtures/pages')

describe('createNtrData.', () => {
test('with fallbackLng.', () => {
const i18n = { locales: ['en', 'fr', 'fr-FR'], defaultLocale: 'en', fallbackLng: { 'fr-FR': ['fr'] } }
const i18n = { locales: ['en', 'cs', 'sk'], defaultLocale: 'cs', fallbackLng: { 'en-US': ['en'] } }
const ntrData = createNtrData({ i18n }, pagesPath)

expect(ntrData.routesTree).toEqual(routesTree)
expect(ntrData.locales).toEqual(i18n.locales)
expect(ntrData.defaultLocale).toEqual(i18n.defaultLocale)
Expand All @@ -33,8 +34,9 @@ describe('createNtrData.', () => {

test('with debug and custom routesTree.', () => {
const customRoutesTree = {}
const i18n = { locales: ['en', 'fr'], defaultLocale: 'fr' }
const i18n = { locales: ['en', 'cs'], defaultLocale: 'cs' }
const ntrData = createNtrData({ i18n, translateRoutes: { routesTree: customRoutesTree, debug: true } }, pagesPath)

expect(ntrData.routesTree).toEqual(customRoutesTree)
expect(ntrData.locales).toEqual(i18n.locales)
expect(ntrData.defaultLocale).toEqual(i18n.defaultLocale)
Expand Down
2 changes: 1 addition & 1 deletion tests/react/fileUrlToUrl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('fileUrlToUrl', () => {
origin: 'https://next-translate-routes.com',
pathname: '/current/path',
} as Window['location'],
} as Window & typeof globalThis),
}) as Window & typeof globalThis,
)

const testDataItems: { url: string | (UrlObject & { pathname: string }); translation: string; locale?: string }[] = [
Expand Down
Loading

0 comments on commit 1afb36d

Please sign in to comment.