Skip to content

Commit 0b5cc9d

Browse files
author
Dimitri POSTOLOV
authoredOct 14, 2023
[v3] make nextra compatible with windows (#2444)
1 parent e73198c commit 0b5cc9d

File tree

24 files changed

+95
-157
lines changed

24 files changed

+95
-157
lines changed
 

Diff for: ‎.changeset/five-falcons-grin.md

-6
This file was deleted.

Diff for: ‎.changeset/happy-steaks-suffer.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'nextra-theme-blog': patch
3+
'nextra-theme-docs': patch
4+
'nextra': patch
5+
---
6+
7+
make nextra compatible with windows

Diff for: ‎docs/components/index-page/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function I18n() {
1919
const [active, setActive] = useState('')
2020

2121
return (
22-
<div className={cn(styles.comparison)}>
22+
<div className={styles.comparison}>
2323
<div style={{ display: 'flex', flexDirection: 'column', gap: '.5rem' }}>
2424
{LANGUAGES.map(({ lang }) => (
2525
<span

Diff for: ‎docs/next-env.d.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/// <reference types="next" />
22
/// <reference types="next/image-types/global" />
3-
/// <reference types="next/navigation-types/compat/navigation" />
43

54
// NOTE: This file should not be edited
65
// see https://nextjs.org/docs/basic-features/typescript for more information.

Diff for: ‎docs/next.config.mjs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import path from 'node:path'
12
import nextra from 'nextra'
23

34
const withNextra = nextra({
@@ -10,6 +11,10 @@ const withNextra = nextra({
1011
defaultShowCopyCode: true
1112
})
1213

14+
const sep = path.sep === '/' ? '/' : '\\\\'
15+
16+
const ALLOWED_SVG_REGEX = new RegExp(`components${sep}icons${sep}.+\\.svg$`)
17+
1318
export default withNextra({
1419
reactStrictMode: true,
1520
eslint: {
@@ -29,15 +34,13 @@ export default withNextra({
2934
}
3035
],
3136
webpack(config) {
32-
const allowedSvgRegex = /components\/icons\/.+\.svg$/
33-
3437
const fileLoaderRule = config.module.rules.find(rule =>
3538
rule.test?.test?.('.svg')
3639
)
37-
fileLoaderRule.exclude = allowedSvgRegex
40+
fileLoaderRule.exclude = ALLOWED_SVG_REGEX
3841

3942
config.module.rules.push({
40-
test: allowedSvgRegex,
43+
test: ALLOWED_SVG_REGEX,
4144
use: ['@svgr/webpack']
4245
})
4346
return config

Diff for: ‎docs/pages/docs/custom-theme.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ It accepts a `children` prop, which is the MDX content of the current page, and
5454
wraps some other elements around the content. After creating the theme, you can
5555
simply add a MDX file as `pages/index.mdx` and see the result:
5656

57-
import customThemeImage from '../../public/assets/docs/custom-theme.png'
57+
import customThemeImage from 'public/assets/docs/custom-theme.png'
5858

5959
<Screenshot src={customThemeImage} alt="Custom theme" />
6060

Diff for: ‎docs/pages/docs/docs-theme/page-configuration.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ navigation bar and the sidebar.
1010
import { Screenshot } from 'components/screenshot'
1111
import { Video } from 'components/video'
1212
import { Callout, FileTree } from 'nextra/components'
13-
import menuImage from '../../../public/assets/docs/menu.png'
13+
import menuImage from 'public/assets/docs/menu.png'
1414

1515
<Callout>
1616
Read more about Nextra's `_meta.json` files

Diff for: ‎docs/pages/docs/docs-theme/theme-configuration.mdx

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Screenshot } from 'components/screenshot'
22
import { OptionTable } from 'components/table'
3-
import sidebarTitleComponentImage from '../../../public/assets/docs/sidebar-customized.png'
3+
import sidebarTitleComponentImage from 'public/assets/docs/sidebar-customized.png'
44

55
# Theme Configuration
66

@@ -218,7 +218,7 @@ function component.
218218
]}
219219
/>
220220

221-
import logoImage from '../../../public/assets/docs/logo.png'
221+
import logoImage from 'public/assets/docs/logo.png'
222222

223223
<Screenshot src={logoImage} alt="Customized Logo" />
224224

@@ -264,7 +264,7 @@ it links to Nextra’s GitHub repository.
264264
You can configure `project.link` and `project.icon` to customize the project
265265
link, for example make it link to your GitLab repository:
266266

267-
import projectLinkImage from '../../../public/assets/docs/project-link.png'
267+
import projectLinkImage from 'public/assets/docs/project-link.png'
268268

269269
<Screenshot src={projectLinkImage} alt="Project link" full />
270270

@@ -376,7 +376,7 @@ If you have updated your banner text, you should change the key to make sure the
376376
banner is shown again. The best practice is to always use a descriptive key for
377377
the current text, for example:
378378

379-
import bannerImage from '../../../public/assets/docs/banner.png'
379+
import bannerImage from 'public/assets/docs/banner.png'
380380

381381
<Screenshot src={bannerImage} alt="Banner" />
382382

@@ -611,7 +611,7 @@ navigating between pages.
611611
]}
612612
/>
613613

614-
import navigationImage from '../../../public/assets/docs/navigation.png'
614+
import navigationImage from 'public/assets/docs/navigation.png'
615615

616616
<Screenshot src={navigationImage} alt="Navigation" full />
617617

Diff for: ‎docs/pages/docs/guide/markdown.mdx

+2-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ export function Counter({ children }) {
2929

3030
Generates:
3131

32+
import { StarsIcon } from '@components/icons'
3233
import { Shadow } from '@components/utils/shadow'
34+
import { Cards } from 'nextra/components'
3335
import { useState } from 'react'
3436

3537
export function Counter({ children }) {
@@ -114,9 +116,6 @@ default `#long-heading-about-nextra`.
114116

115117
## Extended Syntax Highlighting
116118

117-
import { StarsIcon } from '@components/icons'
118-
import { Cards } from 'nextra/components'
119-
120119
Check out the Syntax Highlighting section for more information:
121120

122121
<Cards>

Diff for: ‎packages/nextra/__test__/fixture/page-maps/*-settings-and-page-dont-exist/[foo].mdx

Whitespace-only changes.

Diff for: ‎packages/nextra/__test__/fixture/page-maps/*-settings-and-page-dont-exist/_meta.ts

-16
This file was deleted.

Diff for: ‎packages/nextra/__test__/fixture/page-maps/*-settings-and-page-dont-exist/generated-page-map.js

-12
This file was deleted.

Diff for: ‎packages/nextra/__test__/fixture/page-maps/display-hidden-for-mobile/generated-page-map.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import test_fixture_page_maps_display_hidden_for_mobile_meta from "/Users/dmytro/Desktop/nextra/packages/nextra/__test__/fixture/page-maps/display-hidden-for-mobile/_meta.ts";
1+
import test_fixture_page_maps_display_hidden_for_mobile_meta from "./_meta.ts";
22
export const pageMap = [{
33
data: test_fixture_page_maps_display_hidden_for_mobile_meta
44
}, {

Diff for: ‎packages/nextra/__test__/normalize-page.spec.ts

+13-52
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ import { normalizePages } from '../src/client/normalize-pages.js'
44
import { collectPageMap } from '../src/server/page-map.js'
55
import { cnPageMap, usPageMap } from './fixture/page-maps/pageMap.js'
66

7+
vi.mock('next/dist/lib/find-pages-dir.js', () => ({
8+
findPagesDir: () => ({
9+
pagesDir: 'update me in related test'
10+
})
11+
}))
12+
713
describe('normalize-page', () => {
814
it('zh-CN home', () => {
915
const result = normalizePages({
@@ -186,7 +192,13 @@ describe('normalize-page', () => {
186192
'page-maps',
187193
'display-hidden-for-mobile'
188194
)
189-
const rawJs = await collectPageMap({ dir })
195+
let rawJs = await collectPageMap({ dir })
196+
// TODO: quick fix, found better approach
197+
rawJs = rawJs.replace(
198+
/.*/,
199+
'import test_fixture_page_maps_display_hidden_for_mobile_meta from "./_meta.ts";'
200+
)
201+
190202
await fs.writeFile(path.join(dir, 'generated-page-map.js'), rawJs)
191203

192204
const { pageMap } = await import(
@@ -327,55 +339,4 @@ describe('normalize-page', () => {
327339
}
328340
`)
329341
})
330-
331-
it('should apply `*` settings even if page is not exist', async () => {
332-
const dir = path.join(
333-
__dirname,
334-
'fixture',
335-
'page-maps',
336-
'*-settings-and-page-dont-exist'
337-
)
338-
const rawJs = (await collectPageMap({ dir })).replace(
339-
"import { resolvePageMap } from 'nextra/setup-page'",
340-
'const resolvePageMap = () => {}'
341-
)
342-
await fs.writeFile(path.join(dir, 'generated-page-map.js'), rawJs)
343-
const { pageMap } = await import(
344-
'./fixture/page-maps/*-settings-and-page-dont-exist/generated-page-map.js'
345-
)
346-
347-
const meta = await import(
348-
'./fixture/page-maps/*-settings-and-page-dont-exist/_meta.js'
349-
)
350-
351-
const result = normalizePages({
352-
list: [{ data: meta.default() }, ...pageMap],
353-
route: '/'
354-
})
355-
356-
expect(result).toMatchInlineSnapshot(`
357-
{
358-
"activeIndex": 0,
359-
"activePath": [],
360-
"activeThemeContext": {
361-
"breadcrumb": false,
362-
"collapsed": false,
363-
"footer": false,
364-
"layout": "default",
365-
"navbar": false,
366-
"pagination": false,
367-
"sidebar": false,
368-
"timestamp": false,
369-
"toc": false,
370-
"typesetting": "default",
371-
},
372-
"activeType": undefined,
373-
"directories": [],
374-
"docsDirectories": [],
375-
"flatDirectories": [],
376-
"flatDocsDirectories": [],
377-
"topLevelNavbarItems": [],
378-
}
379-
`)
380-
})
381342
})

Diff for: ‎packages/nextra/__test__/page-map.test.ts

+16-18
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ import path from 'node:path'
22
import { CWD } from '../src/server/constants.js'
33
import { collectPageMap } from '../src/server/page-map.js'
44

5-
// To fix tests on CI
6-
function clean(content: string): string {
7-
return content.replaceAll(
8-
/import \w+ from "(?<name>.*)"/g,
9-
(matched, capture) => matched.replace(capture, path.relative(CWD, capture))
10-
)
11-
}
5+
vi.mock('next/dist/lib/find-pages-dir.js', () => ({
6+
findPagesDir: () => ({
7+
pagesDir: 'update me in related test'
8+
})
9+
}))
1210

1311
describe('collectPageMap', () => {
1412
it('should work', async () => {
@@ -23,15 +21,15 @@ describe('collectPageMap', () => {
2321
)
2422
const rawJs = await collectPageMap({ dir, route: '/en', locale: 'en' })
2523

26-
expect(clean(rawJs)).toMatchInlineSnapshot(`
27-
"import examples_swr_site_pages_en_meta from \\"../../examples/swr-site/pages/en/_meta.ts\\";
28-
import examples_swr_site_pages_en_about_meta from \\"../../examples/swr-site/pages/en/about/_meta.ts\\";
29-
import examples_swr_site_pages_en_blog_meta from \\"../../examples/swr-site/pages/en/blog/_meta.ts\\";
30-
import examples_swr_site_pages_en_docs_meta from \\"../../examples/swr-site/pages/en/docs/_meta.tsx\\";
31-
import examples_swr_site_pages_en_docs_advanced_meta from \\"../../examples/swr-site/pages/en/docs/advanced/_meta.tsx\\";
32-
import examples_swr_site_pages_en_examples_meta from \\"../../examples/swr-site/pages/en/examples/_meta.ts\\";
33-
import examples_swr_site_pages_en_remote_graphql_eslint_meta from \\"../../examples/swr-site/pages/en/remote/graphql-eslint/_meta.ts\\";
34-
import examples_swr_site_pages_en_remote_graphql_yoga_meta from \\"../../examples/swr-site/pages/en/remote/graphql-yoga/_meta.ts\\";
24+
expect(rawJs).toMatchInlineSnapshot(`
25+
"import examples_swr_site_pages_en_meta from \\"../../../../../examples/swr-site/pages/en/_meta.ts\\";
26+
import examples_swr_site_pages_en_about_meta from \\"../../../../../examples/swr-site/pages/en/about/_meta.ts\\";
27+
import examples_swr_site_pages_en_blog_meta from \\"../../../../../examples/swr-site/pages/en/blog/_meta.ts\\";
28+
import examples_swr_site_pages_en_docs_meta from \\"../../../../../examples/swr-site/pages/en/docs/_meta.tsx\\";
29+
import examples_swr_site_pages_en_docs_advanced_meta from \\"../../../../../examples/swr-site/pages/en/docs/advanced/_meta.tsx\\";
30+
import examples_swr_site_pages_en_examples_meta from \\"../../../../../examples/swr-site/pages/en/examples/_meta.ts\\";
31+
import examples_swr_site_pages_en_remote_graphql_eslint_meta from \\"../../../../../examples/swr-site/pages/en/remote/graphql-eslint/_meta.ts\\";
32+
import examples_swr_site_pages_en_remote_graphql_yoga_meta from \\"../../../../../examples/swr-site/pages/en/remote/graphql-yoga/_meta.ts\\";
3533
export const pageMap = [{
3634
data: examples_swr_site_pages_en_meta
3735
}, {
@@ -472,8 +470,8 @@ describe('Page Process', () => {
472470
const rawJs = await collectPageMap({
473471
dir: path.join(CWD, '__test__', 'fixture', 'page-maps', 'dynamic-route')
474472
})
475-
expect(clean(rawJs)).toMatchInlineSnapshot(`
476-
"import test_fixture_page_maps_dynamic_route_my_dir_meta from \\"__test__/fixture/page-maps/dynamic-route/my-dir/_meta.ts\\";
473+
expect(rawJs).toMatchInlineSnapshot(`
474+
"import test_fixture_page_maps_dynamic_route_my_dir_meta from \\"../../../__test__/fixture/page-maps/dynamic-route/my-dir/_meta.ts\\";
477475
export const pageMap = [{
478476
name: \\"my-dir\\",
479477
route: \\"/my-dir\\",

Diff for: ‎packages/nextra/src/client/normalize-pages.ts

-9
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,6 @@ export function normalizePages({
183183
return items
184184
})
185185

186-
const hasRoute = items.some(item => item.route === route)
187-
// In case route is not exist in page map we still want to merge `*` theme settings
188-
if (!hasRoute) {
189-
activeThemeContext = {
190-
...activeThemeContext,
191-
...fallbackMeta.theme
192-
}
193-
}
194-
195186
// Fill all skipped items in meta.
196187
for (let i = metaKeyIndex + 1; i < metaKeys.length; i++) {
197188
const key = metaKeys[i]

Diff for: ‎packages/nextra/src/client/setup-page.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ export function HOC_MDXWrapper(
137137
const __nextra_internal__ = (globalThis as NextraInternalGlobal)[
138138
NEXTRA_INTERNAL
139139
]
140+
140141
__nextra_internal__.route = route
141142
__nextra_internal__.pageMap = pageOpts.pageMap
142143
__nextra_internal__.context[route] = {

Diff for: ‎packages/nextra/src/server/compile.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ export async function compileMdx(
152152

153153
const format =
154154
_format === 'detect' ? (filePath.endsWith('.mdx') ? 'mdx' : 'md') : _format
155-
155+
const fileCompatible = filePath ? { value: source, path: filePath } : source
156156
if (isPageMapImport) {
157157
const compiler = createProcessor({
158158
format,
@@ -161,9 +161,7 @@ export async function compileMdx(
161161
remarkMdxFrontMatter
162162
]
163163
})
164-
const vFile = await compiler.process(
165-
filePath ? { value: source, path: filePath } : source
166-
)
164+
const vFile = await compiler.process(fileCompatible)
167165
const content = vFile.toString()
168166

169167
const index = content.lastIndexOf('function _createMdxContent(props) {')
@@ -207,7 +205,6 @@ export async function compileMdx(
207205
const processor = compiler()
208206

209207
try {
210-
const fileCompatible = filePath ? { value: source, path: filePath } : source
211208
const vFile = await processor.process(fileCompatible)
212209

213210
const data = vFile.data as {

Diff for: ‎packages/nextra/src/server/file-system.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,4 @@ export function findPagesDirectory(): string {
99
return pagesDir
1010
}
1111

12-
export const PAGES_DIR = process.env.VITEST_WORKER_ID
13-
? ''
14-
: findPagesDirectory()
12+
export const PAGES_DIR = findPagesDirectory()

Diff for: ‎packages/nextra/src/server/index.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-env node */
22
import { createRequire } from 'node:module'
3+
import { sep } from 'node:path'
34
import type { NextConfig } from 'next'
45
import type { ZodError } from 'zod'
56
import { fromZodError } from 'zod-validation-error'
@@ -21,6 +22,8 @@ const DEFAULT_EXTENSIONS = ['js', 'jsx', 'ts', 'tsx']
2122

2223
const require = createRequire(import.meta.url)
2324

25+
const AGNOSTIC_PAGE_MAP_PATH = `.next${sep}static${sep}chunks${sep}nextra-page-map`
26+
2427
const nextra: Nextra = nextraConfig => {
2528
try {
2629
nextraConfigSchema.parse(nextraConfig)
@@ -122,8 +125,7 @@ const nextra: Nextra = nextraConfig => {
122125
if (IMPORT_FRONTMATTER) {
123126
rules.push({
124127
test: MARKDOWN_EXTENSION_REGEX,
125-
issuer: request =>
126-
request.includes('.next/static/chunks/nextra-page-map'),
128+
issuer: request => request.includes(AGNOSTIC_PAGE_MAP_PATH),
127129
use: [
128130
options.defaultLoaders.babel,
129131
{
@@ -142,8 +144,7 @@ const nextra: Nextra = nextraConfig => {
142144
// runtime import call such as `import('...')`.
143145
test: MARKDOWN_EXTENSION_REGEX,
144146
issuer: request =>
145-
(!!request &&
146-
!request.includes('.next/static/chunks/nextra-page-map')) ||
147+
(!!request && !request.includes(AGNOSTIC_PAGE_MAP_PATH)) ||
147148
request === null,
148149
use: [
149150
options.defaultLoaders.babel,
@@ -180,7 +181,8 @@ const nextra: Nextra = nextraConfig => {
180181
]
181182
},
182183
{
183-
test: /pages\/_app\./,
184+
// Use platform separator because /pages\/_app\./ will not work on windows
185+
test: new RegExp(`pages${sep === '/' ? '/' : '\\\\'}_app\\.`),
184186
issuer: request => !request,
185187
use: [
186188
options.defaultLoaders.babel,

Diff for: ‎packages/nextra/src/server/loader.ts

+16-11
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,11 @@ export async function loader(
8181
)
8282
: this.resourcePath
8383

84-
if (mdxPath.includes('/pages/api/')) {
84+
const currentPath = slash(mdxPath)
85+
86+
if (currentPath.includes('/pages/api/')) {
8587
logger.warn(
86-
`Ignoring ${mdxPath} because it is located in the "pages/api" folder.`
88+
`Ignoring ${currentPath} because it is located in the "pages/api" folder.`
8789
)
8890
return ''
8991
}
@@ -94,7 +96,7 @@ export async function loader(
9496
export const getStaticProps = () => ({ notFound: true })`
9597
}
9698

97-
if (mdxPath.includes('/pages/_app.mdx')) {
99+
if (currentPath.includes('/pages/_app.mdx')) {
98100
throw new Error(
99101
'Nextra v3 no longer supports _app.mdx, use _app.{js,jsx} or _app.{ts,tsx} for TypeScript projects instead.'
100102
)
@@ -107,8 +109,8 @@ export const getStaticProps = () => ({ notFound: true })`
107109
${latex ? "import 'katex/dist/katex.min.css'" : ''}
108110
${OFFICIAL_THEMES.includes(theme) ? `import '${theme}/style.css'` : ''}`
109111

110-
if (mdxPath.includes('/pages/_app.')) {
111-
isAppFileFromNodeModules = mdxPath.includes('/node_modules/')
112+
if (currentPath.includes('/pages/_app.')) {
113+
isAppFileFromNodeModules = currentPath.includes('/node_modules/')
112114
// Relative path instead of a package name
113115
const themeConfigImport = themeConfig
114116
? `import __themeConfig from '${slash(path.resolve(themeConfig))}'`
@@ -118,23 +120,26 @@ ${OFFICIAL_THEMES.includes(theme) ? `import '${theme}/style.css'` : ''}`
118120
? 'export default function App({ Component, pageProps }) { return <Component {...pageProps} />}'
119121
: [cssImports, source].join('\n')
120122

121-
return `import __layout from '${layoutPath}'
123+
const appRawJs = `import __layout from '${layoutPath}'
122124
${themeConfigImport}
123125
${content}
124126
125127
const __nextra_internal__ = globalThis[Symbol.for('__nextra_internal__')] ||= Object.create(null)
126128
__nextra_internal__.context ||= Object.create(null)
127129
__nextra_internal__.Layout = __layout
128130
${themeConfigImport && '__nextra_internal__.themeConfig = __themeConfig'}`
131+
return appRawJs
129132
}
130133

131-
const locale =
132-
locales[0] === '' ? '' : mdxPath.replace(PAGES_DIR, '').split('/')[1]
134+
const relativePath = slash(path.relative(PAGES_DIR, mdxPath))
135+
136+
let locale = locales[0] === '' ? '' : relativePath.split('/')[0]
137+
// In case when partial document is placed outside `pages` directory
138+
if (locale === '..') locale = ''
133139

134140
const route =
135141
'/' +
136-
slash(path
137-
.relative(PAGES_DIR, mdxPath))
142+
relativePath
138143
.replace(MARKDOWN_EXTENSION_REGEX, '')
139144
.replace(/(^|\/)index$/, '')
140145

@@ -213,7 +218,7 @@ export default MDXLayout`
213218
const pageMapPath = path.join(CHUNKS_DIR, `nextra-page-map-${locale}.mjs`)
214219

215220
const rawJs = `import { HOC_MDXWrapper } from 'nextra/setup-page'
216-
import { pageMap } from '${pageMapPath}'
221+
import { pageMap } from '${slash(pageMapPath)}'
217222
${isAppFileFromNodeModules ? cssImports : ''}
218223
${finalResult}
219224

Diff for: ‎packages/nextra/src/server/mdx-plugins/remark-static-image.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import path from 'node:path'
2+
import type { ImportDeclaration } from 'estree'
23
import fs from 'graceful-fs'
34
import type { Root } from 'mdast'
45
import slash from 'slash'
@@ -97,7 +98,7 @@ export const remarkStaticImage: Plugin<[], Root> = () => ast => {
9798
local: { type: 'Identifier', name: variableName }
9899
}
99100
]
100-
}
101+
} satisfies ImportDeclaration
101102
]
102103
}
103104
}

Diff for: ‎packages/nextra/src/server/page-map.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import { valueToEstree } from 'estree-util-value-to-estree'
55
import gracefulFs from 'graceful-fs'
66
import grayMatter from 'gray-matter'
77
import pLimit from 'p-limit'
8+
import slash from 'slash'
89
import {
10+
CHUNKS_DIR,
911
CWD,
1012
DEFAULT_PROPERTY_PROPS,
1113
IMPORT_FRONTMATTER,
@@ -172,6 +174,14 @@ async function collectFiles({
172174
}
173175
}
174176

177+
/*
178+
* Use relative path instead of absolute, because it's fails on Windows
179+
* https://github.com/nodejs/node/issues/31710
180+
*/
181+
function getImportPath(filePath: string) {
182+
return slash(path.relative(CHUNKS_DIR, filePath))
183+
}
184+
175185
export async function collectPageMap({
176186
dir,
177187
route = '/',
@@ -192,7 +202,7 @@ export async function collectPageMap({
192202
.sort((a, b) => a.filePath.localeCompare(b.filePath))
193203
.map(({ filePath, importName }) => ({
194204
type: 'ImportDeclaration',
195-
source: { type: 'Literal', value: filePath },
205+
source: { type: 'Literal', value: getImportPath(filePath) },
196206
specifiers: [
197207
{
198208
local: { type: 'Identifier', name: importName },

Diff for: ‎packages/nextra/src/server/webpack-plugins/nextra.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class NextraPlugin {
4242
try {
4343
for (const locale of locales) {
4444
const route = `/${locale}`
45-
const dir = PAGES_DIR + route
45+
const dir = path.join(PAGES_DIR, locale)
4646
const rawJs = await collectPageMap({ dir, route, locale })
4747

4848
await fs.writeFile(

0 commit comments

Comments
 (0)
Please sign in to comment.