Skip to content

Commit

Permalink
fix TypeError: Cannot read properties of undefined (reading 'data') (#…
Browse files Browse the repository at this point in the history
…831)

* fix TypeError: Cannot read properties of undefined (reading 'data')

* test on windows as well
  • Loading branch information
dimaMachina committed Sep 11, 2022
1 parent a0e5847 commit b365bb0
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 29 deletions.
5 changes: 5 additions & 0 deletions .changeset/honest-guests-shout.md
@@ -0,0 +1,5 @@
---
'nextra': patch
---

fix TypeError: Cannot read properties of undefined (reading 'data')
18 changes: 17 additions & 1 deletion .eslintrc.cjs
Expand Up @@ -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': [
Expand All @@ -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'
}
]
}
]
}
}
]
}
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions examples/swr-site/theme.config.tsx
Expand Up @@ -102,8 +102,8 @@ const config: DocsThemeConfig = {
},
},
feedback: {
labels: "feedback",
content: "Question? Give us feedback →",
labels: "feedback",
},
footer: {
text() {
Expand All @@ -121,9 +121,6 @@ const config: DocsThemeConfig = {
},
},
gitTimestamp: "Last updated on",
project: {
link: "https://github.com/vercel/swr",
},
head() {
const config = useConfig();
const description =
Expand Down Expand Up @@ -196,6 +193,9 @@ const config: DocsThemeConfig = {
nextThemes: {
defaultTheme: "dark",
},
project: {
link: "https://github.com/vercel/swr",
},
sidebar: {
defaultMenuCollapsed: true,
titleComponent: ({ title }) => (
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -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",
Expand Down
13 changes: 4 additions & 9 deletions 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'
Expand All @@ -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')
}

Expand Down
10 changes: 9 additions & 1 deletion packages/nextra/__test__/context.test.ts
Expand Up @@ -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,
Expand Down
14 changes: 11 additions & 3 deletions packages/nextra/__test__/page-map.test.ts
Expand Up @@ -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'
Expand All @@ -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'),
Expand All @@ -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'),
Expand Down
4 changes: 2 additions & 2 deletions packages/nextra/src/constants.ts
Expand Up @@ -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?:\/\//
5 changes: 3 additions & 2 deletions packages/nextra/src/loader.ts
Expand Up @@ -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.
Expand Down Expand Up @@ -179,7 +180,7 @@ export default MDXContent`.trimStart()

const pageOpts: Omit<PageOpts, 'title'> = {
filePath: slash(path.relative(CWD, mdxPath)),
route: slash(route),
route,
frontMatter,
pageMap,
headings,
Expand Down
6 changes: 3 additions & 3 deletions packages/nextra/src/page-map.ts
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/nextra/src/plugin.ts
Expand Up @@ -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$/, '')))

Expand Down
1 change: 0 additions & 1 deletion 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'

Expand Down

0 comments on commit b365bb0

Please sign in to comment.