xref: /expo/docs/next.config.js (revision a033cb8a)
1const withCSS = require('@zeit/next-css');
2const { copySync, removeSync } = require('fs-extra');
3const { join } = require('path');
4const semver = require('semver');
5
6const { version } = require('./package.json');
7
8// copy versions/v(latest version) to versions/latest
9// (Next.js only half-handles symlinks)
10const vLatest = join('pages', 'versions', `v${version}/`);
11const latest = join('pages', 'versions', 'latest/');
12removeSync(latest);
13copySync(vLatest, latest);
14
15module.exports = withCSS({
16  trailingSlash: true,
17  // Rather than use `@zeit/next-mdx`, we replicate it
18  pageExtensions: ['js', 'jsx', 'md', 'mdx'],
19  webpack: (config, options) => {
20    // Create a copy of the babel loader, to separate MDX and Next/Preval caches
21    const babelMdxLoader = {
22      ...options.defaultLoaders.babel,
23      options: {
24        ...options.defaultLoaders.babel.options,
25        cacheDirectory: 'node_modules/.cache/babel-mdx-loader',
26      },
27    };
28    config.module.rules.push({
29      test: /.mdx?$/, // load both .md and .mdx files
30      use: [babelMdxLoader, '@mdx-js/loader', join(__dirname, './common/md-loader')],
31    });
32    config.node = {
33      fs: 'empty',
34    };
35    return config;
36  },
37  async exportPathMap(defaultPathMap, { dev, dir, outDir }) {
38    if (dev) {
39      return defaultPathMap;
40    }
41    return Object.assign(
42      ...Object.entries(defaultPathMap).map(([pathname, page]) => {
43        if (pathname.match(/\/v[1-9][^\/]*$/)) {
44          // ends in "/v<version>"
45          pathname += '/index.html'; // TODO: find out why we need to do this
46        }
47        if (pathname.match(/unversioned/)) {
48          return {};
49        } else {
50          // hide versions greater than the package.json version number
51          const versionMatch = pathname.match(/\/v(\d\d\.\d\.\d)\//);
52          if (versionMatch && versionMatch[1] && semver.gt(versionMatch[1], version)) {
53            return {};
54          }
55          return { [pathname]: page };
56        }
57      })
58    );
59  },
60});
61