11import fs from "node:fs" ;
22import path from "node:path" ;
33
4+ import { loadConfig } from "config/util.js" ;
45import logger from "../logger.js" ;
56import type { TagCacheMetaFile } from "../types/cache.js" ;
67import { isBinaryContentType } from "../utils/binary.js" ;
78import * as buildHelper from "./helper.js" ;
89
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+ ) {
1022 logger . info ( "Bundling static assets..." ) ;
1123
1224 const { appBuildOutputPath, appPublicPath, outputDir, appPath } = options ;
1325
26+ const NextConfig = loadConfig ( path . join ( appBuildOutputPath , ".next" ) ) ;
27+ const basePath = useBasePath ? ( NextConfig . basePath ?? "" ) : "" ;
28+
1429 // Create output folder
15- const outputPath = path . join ( outputDir , "assets" ) ;
30+ const outputPath = path . join ( outputDir , "assets" , basePath ) ;
1631 fs . mkdirSync ( outputPath , { recursive : true } ) ;
1732
1833 /**
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`
2039 *
2140 * Copy over:
2241 * - .next/BUILD_ID => BUILD_ID
@@ -30,6 +49,7 @@ export function createStaticAssets(options: buildHelper.BuildOptions) {
3049 path . join ( appBuildOutputPath , ".next/BUILD_ID" ) ,
3150 path . join ( outputPath , "BUILD_ID" ) ,
3251 ) ;
52+
3353 fs . cpSync (
3454 path . join ( appBuildOutputPath , ".next/static" ) ,
3555 path . join ( outputPath , "_next" , "static" ) ,
0 commit comments