Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions packages/open-next/src/build/createAssets.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,41 @@
import fs from "node:fs";
import path from "node:path";

import { loadConfig } from "config/util.js";
import logger from "../logger.js";
import type { TagCacheMetaFile } from "../types/cache.js";
import { isBinaryContentType } from "../utils/binary.js";
import * as buildHelper from "./helper.js";

export function createStaticAssets(options: buildHelper.BuildOptions) {
/**
* Copy the static assets to the output folder
*
* WARNING: `useBasePath` should be set to `false` when the output file is used.
*
* @param options OpenNext build options
* @param useBasePath whether to copy files into the to Next.js configured basePath
*/
export function createStaticAssets(
options: buildHelper.BuildOptions,
{ useBasePath = false } = {},
) {
logger.info("Bundling static assets...");

const { appBuildOutputPath, appPublicPath, outputDir, appPath } = options;

const NextConfig = loadConfig(path.join(appBuildOutputPath, ".next"));
const basePath = useBasePath ? (NextConfig.basePath ?? "") : "";

// Create output folder
const outputPath = path.join(outputDir, "assets");
const outputPath = path.join(outputDir, "assets", basePath);
fs.mkdirSync(outputPath, { recursive: true });

/**
* Next.js outputs assets into multiple files. Copy into the same directory.
* Next.js outputs assets into multiple files.
*
* Copy into the same directory:
* - `.open-next/assets` when `useBasePath` is `false`
* - `.open-next/assets/basePath` when `useBasePath` is `true`
*
* Copy over:
* - .next/BUILD_ID => BUILD_ID
Expand All @@ -30,6 +49,7 @@ export function createStaticAssets(options: buildHelper.BuildOptions) {
path.join(appBuildOutputPath, ".next/BUILD_ID"),
path.join(outputPath, "BUILD_ID"),
);

fs.cpSync(
path.join(appBuildOutputPath, ".next/static"),
path.join(outputPath, "_next", "static"),
Expand Down