1/* eslint-disable import/order */ 2const { copySync, removeSync } = require('fs-extra'); 3const merge = require('lodash/merge'); 4const { join } = require('path'); 5const semver = require('semver'); 6const { ESBuildPlugin } = require('esbuild-loader'); 7 8const navigation = require('./constants/navigation-data'); 9const versions = require('./constants/versions'); 10const { version, betaVersion } = require('./package.json'); 11 12// To generate a sitemap, we need context about the supported versions and navigational data 13const createSitemap = require('./scripts/create-sitemap'); 14 15// copy versions/v(latest version) to versions/latest 16// (Next.js only half-handles symlinks) 17const vLatest = join('pages', 'versions', `v${version}/`); 18const latest = join('pages', 'versions', 'latest/'); 19removeSync(latest); 20copySync(vLatest, latest); 21 22// Determine if we are using esbuild for MDX transpiling 23const enableEsbuild = !!process.env.USE_ESBUILD; 24 25console.log(enableEsbuild ? 'Using esbuild for MDX files' : 'Using babel for MDX files'); 26 27module.exports = { 28 // future: { 29 // webpack5: true, 30 // }, 31 trailingSlash: true, 32 // Rather than use `@zeit/next-mdx`, we replicate it 33 pageExtensions: ['js', 'jsx', 'ts', 'tsx', 'md', 'mdx'], 34 webpack: (config, options) => { 35 // Add preval support for `constants/*` only and move it to the `.next/preval` cache. 36 // It's to prevent over-usage and separate the cache to allow manually invalidation. 37 // See: https://github.com/kentcdodds/babel-plugin-preval/issues/19 38 config.module.rules.push({ 39 test: /.jsx?$/, 40 include: [join(__dirname, 'constants')], 41 use: merge({}, options.defaultLoaders.babel, { 42 options: { 43 // Keep this path in sync with package.json and other scripts that clear the cache 44 cacheDirectory: '.next/preval', 45 plugins: ['preval'], 46 }, 47 }), 48 }); 49 50 // Add support for MDX with our custom loader and esbuild 51 config.module.rules.push({ 52 test: /.mdx?$/, // load both .md and .mdx files 53 use: [ 54 !enableEsbuild 55 ? options.defaultLoaders.babel 56 : { 57 loader: 'esbuild-loader', 58 options: { 59 loader: 'tsx', 60 target: 'es2017', 61 }, 62 }, 63 { 64 loader: '@mdx-js/loader', 65 options: { 66 remarkPlugins: [ 67 require('./mdx-plugins/remark-heading-meta'), 68 require('./mdx-plugins/remark-link-rewrite'), 69 ], 70 }, 71 }, 72 join(__dirname, './common/md-loader'), 73 ], 74 }); 75 76 // Fix inline or browser MDX usage: https://mdxjs.com/getting-started/webpack#running-mdx-in-the-browser 77 // Webpack 4 78 config.node = { fs: 'empty' }; 79 // Webpack 5 80 // config.resolve.fallback = { fs: false, path: require.resolve('path-browserify') }; 81 82 // Add the esbuild plugin only when using esbuild 83 if (enableEsbuild) { 84 config.plugins.push(new ESBuildPlugin()); 85 } 86 87 return config; 88 }, 89 // Create a map of all pages to export 90 async exportPathMap(defaultPathMap, { dev, outDir }) { 91 if (dev) { 92 return defaultPathMap; 93 } 94 const pathMap = Object.assign( 95 ...Object.entries(defaultPathMap).map(([pathname, page]) => { 96 if (pathname.match(/\/v[1-9][^/]*$/)) { 97 // ends in "/v<version>" 98 pathname += '/index.html'; // TODO: find out why we need to do this 99 } 100 if (pathname.match(/unversioned/)) { 101 return {}; 102 } else { 103 // hide versions greater than the package.json version number 104 const versionMatch = pathname.match(/\/v(\d\d\.\d\.\d)\//); 105 if ( 106 versionMatch && 107 versionMatch[1] && 108 semver.gt(versionMatch[1], betaVersion || version) 109 ) { 110 return {}; 111 } 112 return { [pathname]: page }; 113 } 114 }) 115 ); 116 // Create a sitemap for crawlers like Google and Algolia 117 createSitemap({ 118 pathMap, 119 domain: 'https://docs.expo.io', 120 output: join(outDir, 'sitemap.xml'), 121 // Some of the search engines only track the first N items from the sitemap, 122 // this makes sure our starting and general guides are first, and API index last (in order from new to old) 123 pathsPriority: [ 124 ...navigation.startingDirectories, 125 ...navigation.generalDirectories, 126 ...versions.VERSIONS.map(version => `versions/${version}`), 127 ], 128 // Some of our pages are "hidden" and should not be added to the sitemap 129 pathsHidden: navigation.previewDirectories, 130 }); 131 return pathMap; 132 }, 133 async headers() { 134 const cacheHeaders = [{ key: 'Cache-Control', value: 'public, max-age=31536000, immutable' }]; 135 return [{ source: '/_next/static/:static*', headers: cacheHeaders }]; 136 }, 137}; 138