diff --git a/.changeset/honest-guests-shout.md b/.changeset/honest-guests-shout.md new file mode 100644 index 0000000000..9a67a27d4d --- /dev/null +++ b/.changeset/honest-guests-shout.md @@ -0,0 +1,5 @@ +--- +'nextra': patch +--- + +fix TypeError: Cannot read properties of undefined (reading 'data') diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 8767d37314..2b25ed9e09 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -9,7 +9,7 @@ module.exports = { overrides: [ { // TODO: enable for `nextra-theme-blog` also - files: 'packages/nextra-theme-docs/**/*.{js,jsx,mjs,cjs,ts,tsx,mts,cts}', + files: 'packages/nextra-theme-docs/**/*', plugins: ['typescript-sort-keys'], rules: { 'no-restricted-imports': [ @@ -20,6 +20,22 @@ module.exports = { } ] } + }, + { + files: 'packages/nextra/src/**/*', + rules: { + 'no-restricted-imports': [ + 'error', + { + patterns: [ + { + group: ['fs', 'node:fs'], + message: 'Use `graceful-fs` instead' + } + ] + } + ] + } } ] } diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 948d933e87..dbed838e41 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6,11 +6,12 @@ on: jobs: test: - name: Test + name: Test (${{matrix.os}}) runs-on: ubuntu-latest strategy: matrix: node-version: [16, 18] + os: [ubuntu-latest, windows-latest] steps: - name: Checkout Master diff --git a/examples/swr-site/theme.config.tsx b/examples/swr-site/theme.config.tsx index a0b1cb180d..71e76585d3 100644 --- a/examples/swr-site/theme.config.tsx +++ b/examples/swr-site/theme.config.tsx @@ -102,8 +102,8 @@ const config: DocsThemeConfig = { }, }, feedback: { - labels: "feedback", content: "Question? Give us feedback →", + labels: "feedback", }, footer: { text() { @@ -121,9 +121,6 @@ const config: DocsThemeConfig = { }, }, gitTimestamp: "Last updated on", - project: { - link: "https://github.com/vercel/swr", - }, head() { const config = useConfig(); const description = @@ -196,6 +193,9 @@ const config: DocsThemeConfig = { nextThemes: { defaultTheme: "dark", }, + project: { + link: "https://github.com/vercel/swr", + }, sidebar: { defaultMenuCollapsed: true, titleComponent: ({ title }) => ( diff --git a/package.json b/package.json index 97e769b7f8..b384ce77fb 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "dev": "turbo run dev", "build": "turbo run build:tailwind build --filter=./packages/*", "types:check": "turbo run types:check", - "lint": "eslint --ignore-path .gitignore .", + "lint": "eslint --ignore-path .gitignore --ext js,jsx,mjs,cjs,ts,tsx,mts,cts .", "dev:core": "turbo run dev --filter=nextra... --filter=!docs", "dev:theme-blog": "turbo run dev --filter=blog... --filter=!docs", "dev:theme-docs": "turbo run dev --filter=swr-site... --filter=!docs", diff --git a/packages/nextra/__test__/compile.test.ts b/packages/nextra/__test__/compile.test.ts index b68289f9f9..f5541b92ca 100644 --- a/packages/nextra/__test__/compile.test.ts +++ b/packages/nextra/__test__/compile.test.ts @@ -1,7 +1,8 @@ +import fs from 'node:fs/promises' +import path from 'node:path' import { compileMdx } from '../src/compile' import { it, describe, expect } from 'vitest' -import path from 'path' -import { promises as fs } from 'fs' +import { CWD } from '../src/constants' type Name = | 'code-h1.mdx' @@ -11,13 +12,7 @@ type Name = | 'static-h1.mdx' function loadFixture(name: Name) { - const filePath = path.join( - process.cwd(), - '__test__', - 'fixture', - 'headings', - name - ) + const filePath = path.join(CWD, '__test__', 'fixture', 'headings', name) return fs.readFile(filePath, 'utf8') } diff --git a/packages/nextra/__test__/context.test.ts b/packages/nextra/__test__/context.test.ts index 676d0adce2..b5e2ae1421 100644 --- a/packages/nextra/__test__/context.test.ts +++ b/packages/nextra/__test__/context.test.ts @@ -5,11 +5,19 @@ import { getPagesUnderRoute } from '../src/context' import { collectFiles } from '../src/plugin' +import { CWD } from '../src/constants' import path from 'node:path' describe('context', () => { beforeAll(async () => { - const PAGES_DIR = path.join(process.cwd(), '../../examples/swr-site/pages') + const PAGES_DIR = path.join( + CWD, + '..', + '..', + 'examples', + 'swr-site', + 'pages' + ) const { items } = await collectFiles(PAGES_DIR) globalThis.__nextra_internal__ = { pageMap: items, diff --git a/packages/nextra/__test__/page-map.test.ts b/packages/nextra/__test__/page-map.test.ts index 82307352eb..97e0f18c32 100644 --- a/packages/nextra/__test__/page-map.test.ts +++ b/packages/nextra/__test__/page-map.test.ts @@ -5,7 +5,7 @@ import { collectFiles } from '../src/plugin' import { FileMap, PageMapItem } from '../src/types' import { CWD } from '../src/constants' -const PAGES_DIR = path.join(CWD, '../../examples/swr-site/pages') +const PAGES_DIR = path.join(CWD, '..', '..', 'examples', 'swr-site', 'pages') const filePath = (name: string) => path.join(PAGES_DIR, name) const defaultLocale = 'en-US' @@ -26,7 +26,11 @@ describe('Page Process', () => { fileMap, defaultLocale }) - expect([indexData.pageMap, indexData.route, indexData.title]).toMatchSnapshot() + expect([ + indexData.pageMap, + indexData.route, + indexData.title + ]).toMatchSnapshot() const gettingStartData = await getPageMap({ filePath: filePath('docs/getting-started.en-US.mdx'), @@ -44,7 +48,11 @@ describe('Page Process', () => { fileMap, defaultLocale }) - expect([indexData.pageMap, indexData.route, indexData.title]).toMatchSnapshot() + expect([ + indexData.pageMap, + indexData.route, + indexData.title + ]).toMatchSnapshot() const gettingStartData = await getPageMap({ filePath: filePath('docs/getting-started.zh-CN.mdx'), diff --git a/packages/nextra/src/constants.ts b/packages/nextra/src/constants.ts index fffd624052..42e297b6b6 100644 --- a/packages/nextra/src/constants.ts +++ b/packages/nextra/src/constants.ts @@ -21,8 +21,8 @@ export const MARKDOWN_EXTENSIONS = ['md', 'mdx'] as const export const PUBLIC_DIR = path.join(CWD, 'public') -export const CACHE_DIR = path.join(CWD, '.next/cache') +export const CACHE_DIR = path.join(CWD, '.next', 'cache') -export const ASSET_DIR = path.join(CWD, '.next/static/chunks') +export const ASSET_DIR = path.join(CWD, '.next', 'static', 'chunks') export const EXTERNAL_URL_REGEX = /^https?:\/\// diff --git a/packages/nextra/src/loader.ts b/packages/nextra/src/loader.ts index b711ea6eec..248c2b2b2d 100644 --- a/packages/nextra/src/loader.ts +++ b/packages/nextra/src/loader.ts @@ -17,8 +17,9 @@ import { DEFAULT_LOCALE, OFFICIAL_THEMES, MARKDOWN_EXTENSION_REGEX, - CWD, + CWD } from './constants' + const PAGES_DIR = findPagesDir(CWD).pages // TODO: create this as a webpack plugin. @@ -179,7 +180,7 @@ export default MDXContent`.trimStart() const pageOpts: Omit = { filePath: slash(path.relative(CWD, mdxPath)), - route: slash(route), + route, frontMatter, pageMap, headings, diff --git a/packages/nextra/src/page-map.ts b/packages/nextra/src/page-map.ts index 99bdedfd4d..4ae185ccc6 100644 --- a/packages/nextra/src/page-map.ts +++ b/packages/nextra/src/page-map.ts @@ -24,13 +24,13 @@ export function getPageMap({ const { locale } = parseFileName(filePath) const pageItem = fileMap[filePath as MdxPath] - const metaPath = path.dirname(filePath) const metaFilename = locale ? META_FILENAME.replace('.', `.${locale}.`) : META_FILENAME + const metaDir = path.dirname(filePath) + const metaPath = path.join(metaDir, metaFilename) as MetaJsonPath - const pageMeta = - fileMap[`${metaPath}/${metaFilename}` as MetaJsonPath].data[pageItem.name] + const pageMeta = fileMap[metaPath].data[pageItem.name] return { pageMap: locale diff --git a/packages/nextra/src/plugin.ts b/packages/nextra/src/plugin.ts index 31fdd7441d..1fa5c4e104 100644 --- a/packages/nextra/src/plugin.ts +++ b/packages/nextra/src/plugin.ts @@ -49,7 +49,7 @@ export async function collectFiles( const files = await readdir(dir, { withFileTypes: true }) const promises = files.map(async f => { - const filePath = path.resolve(dir, f.name) + const filePath = path.join(dir, f.name) const { name, locale, ext } = parseFileName(filePath) const fileRoute = slash(path.join(route, name.replace(/^index$/, ''))) diff --git a/packages/nextra/src/utils.ts b/packages/nextra/src/utils.ts index 48f825a2b0..097995a35f 100644 --- a/packages/nextra/src/utils.ts +++ b/packages/nextra/src/utils.ts @@ -1,5 +1,4 @@ import path from 'node:path' -import fs from 'graceful-fs' import { LOCALE_REGEX } from './constants' import { Meta } from './types'