/
index.js
92 lines (81 loc) · 2.57 KB
/
index.js
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import next from 'next/package.json'
import { NextraPlugin, pageMapCache } from './plugin'
import {
DEFAULT_LOCALE,
MARKDOWN_EXTENSION_REGEX,
MARKDOWN_EXTENSIONS
} from './constants'
const DEFAULT_EXTENSIONS = ['js', 'jsx', 'ts', 'tsx']
const [major, minor] = next.version.split('.').map(Number)
const nextVersion = major + (0.1 * minor)
const hasFutureImage = nextVersion >= 12.3
const nextra = (...config) =>
function withNextra(nextConfig = {}) {
const nextraConfig =
typeof config[0] === 'string'
? {
theme: config[0],
themeConfig: config[1]
}
: config[0]
const nextraPlugin = new NextraPlugin(nextraConfig)
if (nextConfig.i18n?.locales) {
console.log(
'[nextra] You have Next.js i18n enabled, read here https://nextjs.org/docs/advanced-features/i18n-routing for the docs.'
)
}
return {
...nextConfig,
pageExtensions: [
...(nextConfig.pageExtensions || DEFAULT_EXTENSIONS),
...MARKDOWN_EXTENSIONS
],
webpack(config, options) {
config.plugins ||= []
config.plugins.push(nextraPlugin)
const nextraLoaderOptions = {
...nextraConfig,
locales: nextConfig.i18n?.locales || [DEFAULT_LOCALE],
defaultLocale: nextConfig.i18n?.defaultLocale || DEFAULT_LOCALE,
pageMapCache,
newNextLinkBehavior: nextConfig.experimental?.newNextLinkBehavior,
allowFutureImage: hasFutureImage
}
config.module.rules.push(
{
// Match Markdown imports from non-pages. These imports have an
// issuer, which can be anything as long as it's not empty.
test: MARKDOWN_EXTENSION_REGEX,
issuer: request => !!request,
use: [
options.defaultLoaders.babel,
{
loader: 'nextra/loader',
options: nextraLoaderOptions
}
]
},
{
// Match pages (imports without an issuer).
test: MARKDOWN_EXTENSION_REGEX,
issuer: request => !request,
use: [
options.defaultLoaders.babel,
{
loader: 'nextra/loader',
options: {
...nextraLoaderOptions,
pageImport: true
}
}
]
}
)
if (typeof nextConfig.webpack === 'function') {
return nextConfig.webpack(config, options)
}
return config
}
}
}
module.exports = nextra