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" ;
3
6
import webpack from "webpack" ;
4
7
import rehypeSlug from "rehype-slug" ;
5
8
import remarkGfm from "remark-gfm" ;
@@ -8,14 +11,15 @@ import remarkFrontmatter from "remark-frontmatter";
8
11
import remarkMdxFrontmatter from "remark-mdx-frontmatter" ;
9
12
import { createLoader } from "simple-functional-loader" ;
10
13
11
- const bsconfig = JSON . parse ( fs . readFileSync ( "./rescript.json" ) . toString ( ) ) ;
14
+ const bsconfig = JSON . parse ( ( await fs . readFile ( "./rescript.json" , "utf8" ) ) . toString ( ) ) ;
12
15
13
16
const { ProvidePlugin } = webpack ;
14
17
15
18
const transpileModules = [ "rescript" ] . concat ( bsconfig [ "bs-dependencies" ] ) ;
16
19
20
+ /** @type {import("next").NextConfig } */
17
21
const config = {
18
- output : " export",
22
+ output : process . env . NODE_ENV === "production" ? " export" : undefined ,
19
23
pageExtensions : [ "jsx" , "js" , "bs.js" , "mdx" , "mjs" ] ,
20
24
env : {
21
25
ENV : process . env . NODE_ENV ,
@@ -85,7 +89,7 @@ const config = {
85
89
return config ;
86
90
} ,
87
91
async redirects ( ) {
88
- return [
92
+ const redirects = [
89
93
{
90
94
source : "/community" ,
91
95
destination : "/community/overview" ,
@@ -126,6 +130,8 @@ const config = {
126
130
destination : "/docs/manual/latest/typescript-integration" ,
127
131
permanent : true ,
128
132
} ,
133
+ ] ;
134
+ const splatRedirects = [
129
135
{
130
136
source : "/docs/manual/latest/:slug*" ,
131
137
destination : `/docs/manual/${ process . env . VERSION_LATEST } /:slug*` ,
@@ -147,6 +153,33 @@ const config = {
147
153
permanent : false ,
148
154
} ,
149
155
] ;
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 ] ;
150
183
} ,
151
184
} ;
152
185
0 commit comments