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,33 @@ const config = {
147153 permanent : false ,
148154 } ,
149155 ] ;
156+
157+ /**
158+ * @typedef {{
159+ * source: string,
160+ * destination: string,
161+ * permanent: boolean,
162+ * }} Redirect
163+ */
164+
165+ const redirectsFile = path . join ( import . meta. dirname , "public/_redirects" ) ;
166+ await fs . writeFile (
167+ redirectsFile ,
168+ redirects
169+ . map ( ( { source, destination, permanent } ) => {
170+ return `${ source } ${ destination } ${ permanent ? 308 : 307 } ` ;
171+ } ) . join ( "\n" ) +
172+ "\n" +
173+ splatRedirects . map ( ( { source, destination, permanent } ) => {
174+ const splatPattern = / : ( \w + ) \* $ / ;
175+ assert . match ( source , splatPattern ) ;
176+ assert . match ( destination , splatPattern ) ;
177+ return `${ source . replace ( splatPattern , "*" ) } ${ destination . replace ( splatPattern , ":splat" ) } ${ permanent ? 308 : 307 } ` ;
178+ } ) . join ( "\n" ) ,
179+ "utf8" ,
180+ ) ;
181+
182+ return [ ...redirects , ...splatRedirects ] ;
150183 } ,
151184} ;
152185
0 commit comments