1- // @ts -check
2- import fs from "fs" ;
1+ // @ ts-check
2+
3+ import * as assert from "node:assert/strict" ;
4+ import * as path from "node:path" ;
5+ import * as fs from "node:fs/promises" ;
36import webpack from "webpack" ;
47import rehypeSlug from "rehype-slug" ;
58import remarkGfm from "remark-gfm" ;
@@ -8,14 +11,15 @@ import remarkFrontmatter from "remark-frontmatter";
811import remarkMdxFrontmatter from "remark-mdx-frontmatter" ;
912import { createLoader } from "simple-functional-loader" ;
1013
11- const bsconfig = JSON . parse ( fs . readFileSync ( "./rescript.json" ) . toString ( ) ) ;
14+ const bsconfig = JSON . parse ( ( await fs . readFile ( "./rescript.json" , "utf8" ) ) . toString ( ) ) ;
1215
1316const { ProvidePlugin } = webpack ;
1417
1518const transpileModules = [ "rescript" ] . concat ( bsconfig [ "bs-dependencies" ] ) ;
1619
20+ /** @type {import("next").NextConfig } */
1721const config = {
18- output : " export",
22+ output : process . env . NODE_ENV === "production" ? " export" : undefined ,
1923 pageExtensions : [ "jsx" , "js" , "bs.js" , "mdx" , "mjs" ] ,
2024 env : {
2125 ENV : process . env . NODE_ENV ,
@@ -85,7 +89,7 @@ const config = {
8589 return config ;
8690 } ,
8791 async redirects ( ) {
88- return [
92+ const redirects = [
8993 {
9094 source : "/community" ,
9195 destination : "/community/overview" ,
@@ -126,6 +130,8 @@ const config = {
126130 destination : "/docs/manual/latest/typescript-integration" ,
127131 permanent : true ,
128132 } ,
133+ ] ;
134+ const splatRedirects = [
129135 {
130136 source : "/docs/manual/latest/:slug*" ,
131137 destination : `/docs/manual/${ process . env . VERSION_LATEST } /:slug*` ,
@@ -147,6 +153,28 @@ const config = {
147153 permanent : false ,
148154 } ,
149155 ] ;
156+
157+ const redirectsFile = path . join ( import . meta. dirname , "public/_redirects" ) ;
158+ await fs . writeFile (
159+ redirectsFile ,
160+ redirects
161+ . map ( ( { source, destination, permanent } ) => {
162+ return `${ source } ${ destination } ${ permanent ? 308 : 307 } ` ;
163+ } )
164+ . join ( "\n" ) +
165+ "\n" +
166+ splatRedirects
167+ . map ( ( { source, destination, permanent } ) => {
168+ const splatPattern = / : ( \w + ) \* $ / ;
169+ assert . match ( source , splatPattern ) ;
170+ assert . match ( destination , splatPattern ) ;
171+ return `${ source . replace ( splatPattern , "*" ) } ${ destination . replace ( splatPattern , ":splat" ) } ${ permanent ? 308 : 307 } ` ;
172+ } )
173+ . join ( "\n" ) ,
174+ "utf8" ,
175+ ) ;
176+
177+ return [ ...redirects , ...splatRedirects ] ;
150178 } ,
151179} ;
152180
0 commit comments