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 ,
@@ -84,6 +88,94 @@ const config = {
8488 config . plugins . push ( new ProvidePlugin ( { React : "react" } ) ) ;
8589 return config ;
8690 } ,
91+ async redirects ( ) {
92+ const redirects = [
93+ {
94+ source : "/community" ,
95+ destination : "/community/overview" ,
96+ permanent : true ,
97+ } ,
98+ {
99+ source : "/bucklescript-rebranding" ,
100+ destination : "/blog/bucklescript-is-rebranding" ,
101+ permanent : true ,
102+ } ,
103+ {
104+ source : "/docs/manual/latest/migrate-from-bucklescript-reason" ,
105+ destination : "/docs/manual/v10.0.0/migrate-from-bucklescript-reason" ,
106+ permanent : true ,
107+ } ,
108+ {
109+ source : "/docs/manual/latest/unboxed" ,
110+ destination : "/docs/manual/v10.0.0/unboxed" ,
111+ permanent : true ,
112+ } ,
113+ {
114+ source : "/docs/gentype/latest/introduction" ,
115+ destination : "/docs/manual/latest/typescript-integration" ,
116+ permanent : true ,
117+ } ,
118+ {
119+ source : "/docs/gentype/latest/getting-started" ,
120+ destination : "/docs/manual/latest/typescript-integration" ,
121+ permanent : true ,
122+ } ,
123+ {
124+ source : "/docs/gentype/latest/usage" ,
125+ destination : "/docs/manual/latest/typescript-integration" ,
126+ permanent : true ,
127+ } ,
128+ {
129+ source : "/docs/gentype/latest/supported-types" ,
130+ destination : "/docs/manual/latest/typescript-integration" ,
131+ permanent : true ,
132+ } ,
133+ ] ;
134+ const splatRedirects = [
135+ {
136+ source : "/docs/manual/latest/:slug*" ,
137+ destination : `/docs/manual/${ process . env . VERSION_LATEST } /:slug*` ,
138+ permanent : false ,
139+ } ,
140+ {
141+ source : "/docs/manual/next/:slug*" ,
142+ destination : `/docs/manual/${ process . env . VERSION_NEXT } /:slug*` ,
143+ permanent : false ,
144+ } ,
145+ {
146+ source : "/llms/manual/latest/:file*" ,
147+ destination : `/llms/manual/${ process . env . VERSION_LATEST } /:file*` ,
148+ permanent : false ,
149+ } ,
150+ {
151+ source : "/llms/manual/next/:file*" ,
152+ destination : `/llms/manual/${ process . env . VERSION_NEXT } /:file*` ,
153+ permanent : false ,
154+ } ,
155+ ] ;
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 ] ;
178+ } ,
87179} ;
88180
89181export default {
0 commit comments