From b2fc168c00cf2670d8f4c98e965b5c9b3d06e200 Mon Sep 17 00:00:00 2001 From: Dimitri POSTOLOV Date: Sat, 17 Dec 2022 22:08:58 +0100 Subject: [PATCH] [nextra] support `nextConfig.distDir` (#1089) * support `nextConfig.distDir` * fix --- .changeset/thin-bulldogs-enjoy.md | 5 +++ examples/swr-site/next.config.js | 3 +- packages/nextra/src/constants.ts | 4 --- packages/nextra/src/content-dump.ts | 56 +++++++++++++++++++---------- packages/nextra/src/file-system.ts | 14 ++------ packages/nextra/src/index.js | 14 ++++---- packages/nextra/src/loader.ts | 6 ++-- packages/nextra/src/plugin.ts | 7 ++-- packages/nextra/src/types.ts | 1 + 9 files changed, 63 insertions(+), 47 deletions(-) create mode 100644 .changeset/thin-bulldogs-enjoy.md diff --git a/.changeset/thin-bulldogs-enjoy.md b/.changeset/thin-bulldogs-enjoy.md new file mode 100644 index 0000000000..fe1c5e9120 --- /dev/null +++ b/.changeset/thin-bulldogs-enjoy.md @@ -0,0 +1,5 @@ +--- +'nextra': patch +--- + +support `nextConfig.distDir` diff --git a/examples/swr-site/next.config.js b/examples/swr-site/next.config.js index ff4b1de9f0..bdc4a50c09 100644 --- a/examples/swr-site/next.config.js +++ b/examples/swr-site/next.config.js @@ -5,7 +5,7 @@ const withNextra = require("nextra")({ codeblocks: true, }, staticImage: true, - defaultShowCopyCode: true + defaultShowCopyCode: true, }); module.exports = withNextra({ @@ -13,6 +13,7 @@ module.exports = withNextra({ locales: ["en-US", "es-ES", "ja", "ko", "ru", "zh-CN"], defaultLocale: "en-US", }, + distDir: "./.next", // Nextra supports custom `nextConfig.distDir` redirects: () => { return [ // { diff --git a/packages/nextra/src/constants.ts b/packages/nextra/src/constants.ts index eccd46be6b..d319c4ee9e 100644 --- a/packages/nextra/src/constants.ts +++ b/packages/nextra/src/constants.ts @@ -28,8 +28,4 @@ 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 ASSET_DIR = path.join(CWD, '.next', 'static', 'chunks') - export const EXTERNAL_URL_REGEX = /^https?:\/\// diff --git a/packages/nextra/src/content-dump.ts b/packages/nextra/src/content-dump.ts index 253ff1147e..6f27f18b9f 100644 --- a/packages/nextra/src/content-dump.ts +++ b/packages/nextra/src/content-dump.ts @@ -1,24 +1,19 @@ import path from 'node:path' import fs from 'graceful-fs' import { existsSync } from './file-system' -import { ASSET_DIR, CACHE_DIR } from './constants' +import { CWD } from './constants' + +let cacheDir: string +let assetDir: string +let cacheDirExist = false const asset: { [locale: string]: any } = Object.create(null) const cached = new Map() -if (!existsSync(ASSET_DIR)) { - fs.mkdirSync(ASSET_DIR, { recursive: true }) -} - -const cacheDirExist = existsSync(CACHE_DIR) -if (!cacheDirExist) { - fs.mkdirSync(CACHE_DIR, { recursive: true }) -} - function initFromCache(filename: string) { if (!cached.has(filename)) { try { - const content = fs.readFileSync(path.join(ASSET_DIR, filename), 'utf8') + const content = fs.readFileSync(path.join(assetDir, filename), 'utf8') cached.set(filename, true) return JSON.parse(content) } catch { @@ -32,13 +27,18 @@ export function addPage({ locale, route, title, - structurizedData + structurizedData, + distDir }: { locale: string route: string title: string structurizedData: any + distDir?: string }): void { + if (!cacheDir || !assetDir) { + initConfig(distDir) + } const dataFilename = `nextra-data-${locale}.json` asset[locale] ||= initFromCache(dataFilename) @@ -51,23 +51,41 @@ export function addPage({ // @TODO: introduce mutex lock, or only generate the asset when finishing the // entire build. const content = JSON.stringify(asset[locale]) - fs.writeFileSync(path.join(ASSET_DIR, dataFilename), content) - fs.writeFileSync(path.join(CACHE_DIR, dataFilename), content) + fs.writeFileSync(path.join(assetDir, dataFilename), content) + fs.writeFileSync(path.join(cacheDir, dataFilename), content) +} + +export function initConfig(distDir = '.next') { + cacheDir = path.join(CWD, distDir, 'cache') + assetDir = path.join(CWD, distDir, 'static', 'chunks') } // Copy cached data to the asset directory. -export function restoreCache(): void { +export function restoreCache(distDir?: string): void { + if (!cacheDir || !assetDir) { + initConfig(distDir) + if (!existsSync(assetDir)) { + fs.mkdirSync(assetDir, { recursive: true }) + } + + cacheDirExist = existsSync(cacheDir) + if (!cacheDirExist) { + fs.mkdirSync(cacheDir, { recursive: true }) + } + } + if (!cacheDirExist) { return } - if (!existsSync(ASSET_DIR)) { - fs.mkdirSync(ASSET_DIR, { recursive: true }) + + if (!existsSync(assetDir)) { + fs.mkdirSync(assetDir, { recursive: true }) } - const files = fs.readdirSync(CACHE_DIR) + const files = fs.readdirSync(cacheDir) for (const file of files) { if (file.startsWith('nextra-data-')) { - fs.copyFileSync(path.join(CACHE_DIR, file), path.join(ASSET_DIR, file)) + fs.copyFileSync(path.join(cacheDir, file), path.join(assetDir, file)) } } } diff --git a/packages/nextra/src/file-system.ts b/packages/nextra/src/file-system.ts index 384e34554a..219e7ad8b3 100644 --- a/packages/nextra/src/file-system.ts +++ b/packages/nextra/src/file-system.ts @@ -1,17 +1,9 @@ -import fs from 'graceful-fs' -import { findPagesDir } from 'next/dist/lib/find-pages-dir.js' +import { findPagesDir, existsSync } from 'next/dist/lib/find-pages-dir.js' import { CWD } from './constants' -export const existsSync = (filePath: string): boolean => { - try { - fs.statSync(filePath) - return true - } catch { - return false - } -} +export { existsSync } -export const findPagesDirectory = (): string => { +export function findPagesDirectory(): string { const res = findPagesDir(CWD, false) return ( res.pagesDir || // next v13 diff --git a/packages/nextra/src/index.js b/packages/nextra/src/index.js index cc68315e49..b92f32c342 100644 --- a/packages/nextra/src/index.js +++ b/packages/nextra/src/index.js @@ -21,7 +21,10 @@ const nextra = (...config) => : config[0] ) - const nextraPlugin = new NextraPlugin(nextraConfig) + const nextraPlugin = new NextraPlugin({ + ...nextraConfig, + distDir: nextConfig.distDir + }) if (nextConfig.i18n?.locales) { console.log( @@ -44,7 +47,8 @@ const nextra = (...config) => locales: nextConfig.i18n?.locales || [DEFAULT_LOCALE], defaultLocale: nextConfig.i18n?.defaultLocale || DEFAULT_LOCALE, pageMapCache, - newNextLinkBehavior: nextConfig.experimental?.newNextLinkBehavior + newNextLinkBehavior: nextConfig.experimental?.newNextLinkBehavior, + distDir: nextConfig.distDir } config.module.rules.push( @@ -78,11 +82,7 @@ const nextra = (...config) => } ) - if (typeof nextConfig.webpack === 'function') { - return nextConfig.webpack(config, options) - } - - return config + return nextConfig.webpack?.(config, options) || config } } } diff --git a/packages/nextra/src/loader.ts b/packages/nextra/src/loader.ts index e237d5d20a..53d466fc0f 100644 --- a/packages/nextra/src/loader.ts +++ b/packages/nextra/src/loader.ts @@ -71,7 +71,8 @@ async function loader( readingTime: _readingTime, mdxOptions, pageMapCache, - newNextLinkBehavior + newNextLinkBehavior, + distDir } = context.getOptions() context.cacheable(true) @@ -158,7 +159,8 @@ export default MDXContent`.trimStart() locale: locale || DEFAULT_LOCALE, route, title, - structurizedData + structurizedData, + distDir }) } indexContentEmitted.add(mdxPath) diff --git a/packages/nextra/src/plugin.ts b/packages/nextra/src/plugin.ts index bed6c1d362..48cbe46690 100644 --- a/packages/nextra/src/plugin.ts +++ b/packages/nextra/src/plugin.ts @@ -163,15 +163,16 @@ export class PageMapCache { export const pageMapCache = new PageMapCache() export class NextraPlugin { - constructor(private config: NextraConfig) {} + constructor(private config: NextraConfig & { distDir?: string }) {} apply(compiler: Compiler) { compiler.hooks.beforeCompile.tapAsync( 'NextraPlugin', async (_, callback) => { - if (this.config?.flexsearch) { + const { flexsearch, distDir } = this.config + if (flexsearch) { // Restore the search data from the cache. - restoreCache() + restoreCache(distDir) } const PAGES_DIR = findPagesDirectory() const result = await collectFiles(PAGES_DIR) diff --git a/packages/nextra/src/types.ts b/packages/nextra/src/types.ts index dd8f259f93..2c477befda 100644 --- a/packages/nextra/src/types.ts +++ b/packages/nextra/src/types.ts @@ -15,6 +15,7 @@ export interface LoaderOptions extends NextraConfig { defaultLocale: string pageMapCache: PageMapCache newNextLinkBehavior?: boolean + distDir?: string } export interface Folder {