/
index.ts
69 lines (60 loc) · 2.15 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { NextraPlugin, pageMapCache } from './plugin'
import { DEFAULT_LOCALE, MARKDOWN_EXTENSION_REGEX } from './constants'
import { LoaderOptions, Nextra } from './types'
const DEFAULT_EXTENSIONS = ['js', 'jsx', 'ts', 'tsx'] as const
const MARKDOWN_EXTENSIONS = ['md', 'mdx'] as const
const nextra: Nextra = (...args) =>
function withNextra(nextConfig = {}) {
const nextraConfig =
typeof args[0] === 'string'
? {
theme: args[0],
themeConfig: args[1] as string
}
: args[0]
const nextraPlugin = new NextraPlugin(nextraConfig)
const { i18n, pageExtensions = DEFAULT_EXTENSIONS } = nextConfig
if (i18n?.locales) {
console.log(
'[nextra] You have Next.js i18n enabled, read here https://nextjs.org/docs/advanced-features/i18n-routing for the docs.'
)
} else if (!i18n?.defaultLocale) {
// If `i18n.locales` and `i18n.defaultLocale` were not specified,
// client will receive error - Text content does not match server-rendered HTML.
// Due to `const { locale } = useRouter()` where `locale` will be `undefined`
// To fix it we need to explicitly specify `i18n.locales` and `i18n.defaultLocale`
nextConfig.i18n = {
...i18n,
locales: [DEFAULT_LOCALE],
defaultLocale: DEFAULT_LOCALE
}
}
return {
...nextConfig,
pageExtensions: [...pageExtensions, ...MARKDOWN_EXTENSIONS],
webpack(config, options) {
config.plugins ||= []
config.plugins.push(nextraPlugin)
config.module.rules.push({
test: MARKDOWN_EXTENSION_REGEX,
use: [
options.defaultLoaders.babel,
{
loader: 'nextra/loader',
options: <LoaderOptions>{
...nextraConfig,
locales: nextConfig.i18n?.locales,
defaultLocale: nextConfig.i18n?.defaultLocale,
pageMapCache
}
}
]
})
if (typeof nextConfig.webpack === 'function') {
return nextConfig.webpack(config, options)
}
return config
}
}
}
module.exports = nextra