From a9523c9744e84eeda63464ed0965f8cf29d90bfa Mon Sep 17 00:00:00 2001 From: Shu Ding Date: Tue, 13 Sep 2022 16:37:56 +0200 Subject: [PATCH] Fix Nextra not able to run on WebContainers (#843) * fix nextra on webcontainers * add changeset --- .changeset/purple-poems-argue.md | 5 +++ examples/swr-site/theme.config.tsx | 2 +- packages/nextra/src/loader.ts | 52 ++++++++++++++++-------------- pnpm-lock.yaml | 35 ++++++++++++++++++-- 4 files changed, 67 insertions(+), 27 deletions(-) create mode 100644 .changeset/purple-poems-argue.md diff --git a/.changeset/purple-poems-argue.md b/.changeset/purple-poems-argue.md new file mode 100644 index 0000000000..df45df285d --- /dev/null +++ b/.changeset/purple-poems-argue.md @@ -0,0 +1,5 @@ +--- +'nextra': patch +--- + +fix nextra on webcontainers diff --git a/examples/swr-site/theme.config.tsx b/examples/swr-site/theme.config.tsx index fc611eca1f..6a03c76726 100644 --- a/examples/swr-site/theme.config.tsx +++ b/examples/swr-site/theme.config.tsx @@ -121,7 +121,7 @@ const config: DocsThemeConfig = { ); }, }, - gitTimestamp: "Last updated on", + gitTimestamp: ({ timestamp }) => <>Last updated on {timestamp.toString()}, head() { const config = useConfig(); const description = diff --git a/packages/nextra/src/loader.ts b/packages/nextra/src/loader.ts index 248c2b2b2d..680919249d 100644 --- a/packages/nextra/src/loader.ts +++ b/packages/nextra/src/loader.ts @@ -4,7 +4,6 @@ import path from 'node:path' 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' @@ -25,32 +24,36 @@ const PAGES_DIR = findPagesDir(CWD).pages // TODO: create this as a webpack plugin. const indexContentEmitted = new Set() -const [repository, gitRoot] = (function () { - try { - const repo = Repository.discover(CWD) - if (repo.isShallow()) { - if (process.env.VERCEL) { - console.warn( - '[nextra] The repository is shallow cloned, so the latest modified time will not be presented. Set the VERCEL_DEEP_CLONE=true environment variable to enable deep cloning.' - ) - } else if (process.env.GITHUB_ACTION) { - console.warn( - '[nextra] The repository is shallow cloned, so the latest modified time will not be presented. See https://github.com/actions/checkout#fetch-all-history-for-all-tags-and-branches to fetch all the history.' - ) - } else { - console.warn( - '[nextra] The repository is shallow cloned, so the latest modified time will not be presented.' - ) +const IS_WEB_CONTAINER = !!process.versions.webcontainer + +const initGitRepo = (async () => { + if (!IS_WEB_CONTAINER) { + const { Repository } = await import('@napi-rs/simple-git') + try { + const repository = Repository.discover(CWD) + if (repository.isShallow()) { + if (process.env.VERCEL) { + console.warn( + '[nextra] The repository is shallow cloned, so the latest modified time will not be presented. Set the VERCEL_DEEP_CLONE=true environment variable to enable deep cloning.' + ) + } else if (process.env.GITHUB_ACTION) { + console.warn( + '[nextra] The repository is shallow cloned, so the latest modified time will not be presented. See https://github.com/actions/checkout#fetch-all-history-for-all-tags-and-branches to fetch all the history.' + ) + } else { + console.warn( + '[nextra] The repository is shallow cloned, so the latest modified time will not be presented.' + ) + } } + // repository.path() returns the `/path/to/repo/.git`, we need the parent directory of it + const gitRoot = path.join(repository.path(), '..') + return { repository, gitRoot } + } catch (e) { + console.warn('[nextra] Init git repository failed', e) } - // repository.path() returns the `/path/to/repo/.git`, we need the parent directory of it - const gitRoot = path.join(repo.path(), '..') - - return [repo, gitRoot] - } catch (e) { - console.warn('[nextra] Init git repository failed', e) - return [] } + return {} })() async function loader( @@ -160,6 +163,7 @@ export default MDXContent`.trimStart() } let timestamp: PageOpts['timestamp'] + const { repository, gitRoot } = await initGitRepo if (repository && gitRoot) { try { timestamp = await repository.getFileLatestModifiedDateAsync( diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 02c5cc56ac..1a1b62da67 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -291,7 +291,7 @@ importers: postcss-import: 14.1.0_postcss@8.4.14 react: 18.2.0 react-dom: 18.2.0_react@18.2.0 - tailwindcss: 3.1.8_postcss@8.4.14 + tailwindcss: 3.1.8 vitest: 0.21.0 packages: @@ -972,7 +972,7 @@ packages: lodash.castarray: 4.4.0 lodash.isplainobject: 4.0.6 lodash.merge: 4.6.2 - tailwindcss: 3.1.8_postcss@8.4.14 + tailwindcss: 3.1.8 dev: true /@trysound/sax/0.2.0: @@ -5667,6 +5667,37 @@ packages: stable: 0.1.8 dev: true + /tailwindcss/3.1.8: + resolution: {integrity: sha512-YSneUCZSFDYMwk+TGq8qYFdCA3yfBRdBlS7txSq0LUmzyeqRe3a8fBQzbz9M3WS/iFT4BNf/nmw9mEzrnSaC0g==} + engines: {node: '>=12.13.0'} + hasBin: true + dependencies: + arg: 5.0.2 + chokidar: 3.5.3 + color-name: 1.1.4 + detective: 5.2.1 + didyoumean: 1.2.2 + dlv: 1.1.3 + fast-glob: 3.2.11 + glob-parent: 6.0.2 + is-glob: 4.0.3 + lilconfig: 2.0.6 + normalize-path: 3.0.0 + object-hash: 3.0.0 + picocolors: 1.0.0 + postcss: 8.4.14 + postcss-import: 14.1.0_postcss@8.4.14 + postcss-js: 4.0.0_postcss@8.4.14 + postcss-load-config: 3.1.4_postcss@8.4.14 + postcss-nested: 5.0.6_postcss@8.4.14 + postcss-selector-parser: 6.0.10 + postcss-value-parser: 4.2.0 + quick-lru: 5.1.1 + resolve: 1.22.1 + transitivePeerDependencies: + - ts-node + dev: true + /tailwindcss/3.1.8_postcss@8.4.14: resolution: {integrity: sha512-YSneUCZSFDYMwk+TGq8qYFdCA3yfBRdBlS7txSq0LUmzyeqRe3a8fBQzbz9M3WS/iFT4BNf/nmw9mEzrnSaC0g==} engines: {node: '>=12.13.0'}