forked from expo/expo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
next.config.js
133 lines (120 loc) · 4.76 KB
/
next.config.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import fsExtra from 'fs-extra';
import NextLog from 'next/dist/build/output/log.js';
import { join } from 'path';
import rehypeSlug from 'rehype-slug';
import remarkFrontmatter from 'remark-frontmatter';
import remarkGFM from 'remark-gfm';
import remarkMDX from 'remark-mdx';
import remarkMdxDisableExplicitJsx from 'remark-mdx-disable-explicit-jsx';
import remarkMDXFrontmatter from 'remark-mdx-frontmatter';
import semver from 'semver';
import remarkCodeTitle from './mdx-plugins/remark-code-title.js';
import remarkCreateStaticProps from './mdx-plugins/remark-create-static-props.js';
import remarkExportHeadings from './mdx-plugins/remark-export-headings.js';
import remarkLinkRewrite from './mdx-plugins/remark-link-rewrite.js';
import { copyAsLatest } from './scripts/copy-latest.js';
import createSitemap from './scripts/create-sitemap.js';
const { readJsonSync } = fsExtra;
// note(simek): We cannot use direct JSON import because ESLint do not support `assert { type: 'json' }` syntax yet:
// * https://github.com/eslint/eslint/discussions/15305
const { version, betaVersion } = readJsonSync('./package.json');
const { VERSIONS } = readJsonSync('./public/static/constants/versions.json');
const navigation = readJsonSync('./public/static/constants/navigation.json');
copyAsLatest(version);
NextLog.info(`Copied latest Expo SDK version from v${version}`);
const removeConsole =
process.env.NODE_ENV !== 'development'
? {
exclude: ['error'],
}
: false;
/** @type {import('next').NextConfig} */
export default {
trailingSlash: true,
experimental: {
esmExternals: true,
// note(simek): would be nice enhancement, but it breaks the `@next/font` styles currently,
// and results in font face swap on every page reload
optimizeCss: false,
},
pageExtensions: ['js', 'jsx', 'ts', 'tsx', 'md', 'mdx'],
compiler: {
reactRemoveProperties: true,
removeConsole,
},
output: 'export',
poweredByHeader: false,
webpack: (config, { defaultLoaders }) => {
// Add support for MDX with our custom loader
config.module.rules.push({
test: /.mdx?$/,
use: [
defaultLoaders.babel,
{
loader: '@mdx-js/loader',
/** @type {import('@mdx-js/loader').Options} */
options: {
providerImportSource: '@mdx-js/react',
remarkPlugins: [
remarkMDX,
remarkGFM,
[remarkMdxDisableExplicitJsx, { whiteList: ['kbd'] }],
remarkFrontmatter,
[remarkMDXFrontmatter, { name: 'meta' }],
remarkCodeTitle,
remarkExportHeadings,
remarkLinkRewrite,
[remarkCreateStaticProps, `{ meta: meta || {}, headings: headings || [] }`],
],
rehypePlugins: [rehypeSlug],
},
},
],
});
// Fix inline or browser MDX usage
config.resolve.fallback = { fs: false, path: 'path-browserify' };
config.output.environment = { ...config.output.environment, asyncFunction: true };
config.experiments = { ...config.experiments, topLevelAwait: true };
return config;
},
// Create a map of all pages to export
// https://nextjs.org/docs/api-reference/next.config.js/exportPathMap
async exportPathMap(defaultPathMap, { dev, outDir }) {
if (dev) {
return defaultPathMap;
}
const pathMap = Object.assign(
...Object.entries(defaultPathMap).map(([pathname, page]) => {
if (pathname.match(/unversioned/)) {
// Remove unversioned pages from the exported site
return {};
} else {
// Remove newer unreleased versions from the exported side
const versionMatch = pathname.match(/\/v(\d\d\.\d\.\d)\//);
if (versionMatch?.[1] && semver.gt(versionMatch[1], betaVersion || version, false)) {
return {};
}
}
return { [pathname]: page };
})
);
const sitemapEntries = createSitemap({
pathMap,
domain: `https://docs.expo.dev`,
output: join(outDir, `sitemap.xml`),
// Some of the search engines only track the first N items from the sitemap,
// this makes sure our starting and general guides are first, and API index last (in order from new to old)
pathsPriority: [
...navigation.homeDirectories,
...navigation.learnDirectories,
...navigation.generalDirectories,
...navigation.referenceDirectories.filter(dir => dir === 'versions'),
...VERSIONS.map(version => `versions/${version}`),
],
// Some of our pages are "hidden" and should not be added to the sitemap
pathsHidden: [...navigation.previewDirectories, ...navigation.archiveDirectories],
});
NextLog.info(`📝 Generated sitemap with ${sitemapEntries.length} entries`);
return pathMap;
},
};