1
1
import fs from "node:fs" ;
2
2
import path from "node:path" ;
3
3
4
+ import { loadConfig } from "config/util.js" ;
4
5
import logger from "../logger.js" ;
5
6
import type { TagCacheMetaFile } from "../types/cache.js" ;
6
7
import { isBinaryContentType } from "../utils/binary.js" ;
7
8
import * as buildHelper from "./helper.js" ;
8
9
9
- export function createStaticAssets ( options : buildHelper . BuildOptions ) {
10
+ /**
11
+ * Copy the static assets to the output folder
12
+ *
13
+ * WARNING: `useBasePath` should be set to `false` when the output file is used.
14
+ *
15
+ * @param options OpenNext build options
16
+ * @param useBasePath whether to copy files into the to Next.js configured basePath
17
+ */
18
+ export function createStaticAssets (
19
+ options : buildHelper . BuildOptions ,
20
+ { useBasePath = false } = { } ,
21
+ ) {
10
22
logger . info ( "Bundling static assets..." ) ;
11
23
12
24
const { appBuildOutputPath, appPublicPath, outputDir, appPath } = options ;
13
25
26
+ const NextConfig = loadConfig ( path . join ( appBuildOutputPath , ".next" ) ) ;
27
+ const basePath = useBasePath ? ( NextConfig . basePath ?? "" ) : "" ;
28
+
14
29
// Create output folder
15
- const outputPath = path . join ( outputDir , "assets" ) ;
30
+ const outputPath = path . join ( outputDir , "assets" , basePath ) ;
16
31
fs . mkdirSync ( outputPath , { recursive : true } ) ;
17
32
18
33
/**
19
- * Next.js outputs assets into multiple files. Copy into the same directory.
34
+ * Next.js outputs assets into multiple files.
35
+ *
36
+ * Copy into the same directory:
37
+ * - `.open-next/assets` when `useBasePath` is `false`
38
+ * - `.open-next/assets/basePath` when `useBasePath` is `true`
20
39
*
21
40
* Copy over:
22
41
* - .next/BUILD_ID => BUILD_ID
@@ -30,6 +49,7 @@ export function createStaticAssets(options: buildHelper.BuildOptions) {
30
49
path . join ( appBuildOutputPath , ".next/BUILD_ID" ) ,
31
50
path . join ( outputPath , "BUILD_ID" ) ,
32
51
) ;
52
+
33
53
fs . cpSync (
34
54
path . join ( appBuildOutputPath , ".next/static" ) ,
35
55
path . join ( outputPath , "_next" , "static" ) ,
0 commit comments