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
+ * @param options
14
+ * @param useBasePath whether to adjust paths according to Next.js configure basePath
15
+ */
16
+ export function createStaticAssets (
17
+ options : buildHelper . BuildOptions ,
18
+ { useBasePath = false } = { } ,
19
+ ) {
10
20
logger . info ( "Bundling static assets..." ) ;
11
21
12
22
const { appBuildOutputPath, appPublicPath, outputDir, appPath } = options ;
@@ -20,7 +30,7 @@ export function createStaticAssets(options: buildHelper.BuildOptions) {
20
30
*
21
31
* Copy over:
22
32
* - .next/BUILD_ID => BUILD_ID
23
- * - .next/static => _next/static
33
+ * - .next/static => _next/static or basePath/_next/static when useBasePath is true
24
34
* - public/* => *
25
35
* - app/favicon.ico or src/app/favicon.ico => favicon.ico
26
36
*
@@ -30,9 +40,13 @@ export function createStaticAssets(options: buildHelper.BuildOptions) {
30
40
path . join ( appBuildOutputPath , ".next/BUILD_ID" ) ,
31
41
path . join ( outputPath , "BUILD_ID" ) ,
32
42
) ;
43
+
44
+ const NextConfig = loadConfig ( path . join ( appBuildOutputPath , ".next" ) ) ;
45
+ const basePath = useBasePath ? ( NextConfig . basePath ?? "" ) : "" ;
46
+
33
47
fs . cpSync (
34
48
path . join ( appBuildOutputPath , ".next/static" ) ,
35
- path . join ( outputPath , "_next" , "static" ) ,
49
+ path . join ( outputPath , basePath , "_next" , "static" ) ,
36
50
{ recursive : true } ,
37
51
) ;
38
52
if ( fs . existsSync ( appPublicPath ) ) {
0 commit comments