Skip to content

Commit

Permalink
use findPagesDir from next/dist/lib/find-pages-dir (#703)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimaMachina committed Aug 16, 2022
1 parent a9ca0b9 commit 803553c
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/cold-rockets-brake.md
@@ -0,0 +1,5 @@
---
'nextra': patch
---

use `findPagesDir` from `next/dist/lib/find-pages-dir`
5 changes: 3 additions & 2 deletions packages/nextra/src/loader.ts
Expand Up @@ -5,11 +5,12 @@ import grayMatter from 'gray-matter'
import slash from 'slash'
import { LoaderContext } from 'webpack'
import { Repository } from '@napi-rs/simple-git'
import { findPagesDir } from 'next/dist/lib/find-pages-dir.js'

import { addPage } from './content-dump'
import { parseFileName } from './utils'
import { compileMdx } from './compile'
import { getPageMap, findPagesDir } from './page-map'
import { getPageMap } from './page-map'
import { collectFiles, collectMdx } from './plugin'
import {
IS_PRODUCTION,
Expand All @@ -21,7 +22,7 @@ import {
// TODO: create this as a webpack plugin.
const indexContentEmitted = new Set<string>()

const pagesDir = path.resolve(findPagesDir())
const pagesDir = findPagesDir(process.cwd()).pages

const [repository, gitRoot] = (function () {
try {
Expand Down
12 changes: 1 addition & 11 deletions packages/nextra/src/page-map.ts
@@ -1,18 +1,8 @@
import { PageMapItem } from './types'
import { parseFileName, existsSync } from './utils'
import { parseFileName } from './utils'
import path from 'path'
import filterRouteLocale from './filter-route-locale'

export function findPagesDir(dir = process.cwd()): string {
// prioritize ./pages over ./src/pages
if (existsSync(path.join(dir, 'pages'))) return 'pages'
if (existsSync(path.join(dir, 'src/pages'))) return 'src/pages'

throw new Error(
"> Couldn't find a `pages` directory. Please create one under the project root"
)
}

export function getPageMap(
currentResourcePath: string,
pageMap: PageMapItem[],
Expand Down
6 changes: 3 additions & 3 deletions packages/nextra/src/plugin.ts
Expand Up @@ -5,8 +5,9 @@ import { parseFileName, parseJsonFile } from './utils'
import path from 'path'
import slash from 'slash'
import grayMatter from 'gray-matter'
import { findPagesDir } from './page-map'
import { findPagesDir } from 'next/dist/lib/find-pages-dir.js'
import { Compiler } from 'webpack'

import { restoreCache } from './content-dump'
import { MARKDOWN_EXTENSION_REGEX } from './constants'

Expand Down Expand Up @@ -109,9 +110,8 @@ export class NextraPlugin {
// Restore the search data from the cache.
restoreCache()
}

const result = await collectFiles(
path.join(process.cwd(), findPagesDir()),
findPagesDir(process.cwd()).pages,
'/'
)
pageMapCache.set(result)
Expand Down

0 comments on commit 803553c

Please sign in to comment.