Skip to content

Commit

Permalink
Add logger library and print plugin build details in debug mode (#311)
Browse files Browse the repository at this point in the history
* Print plugin info in debug mode

* Sync
  • Loading branch information
fwang committed Nov 7, 2023
1 parent 1d83dab commit 1ed5ffd
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/proud-poets-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"open-next": patch
---

Print plugin info in debug mode
36 changes: 19 additions & 17 deletions packages/open-next/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
buildSync,
} from "esbuild";

import logger from "./logger.js";
import { minifyAll } from "./minimize-js.js";
import openNextPlugin from "./plugin.js";

Expand Down Expand Up @@ -84,6 +85,7 @@ export async function build(opts: BuildOptions = {}) {

// Initialize options
options = normalizeOptions(opts, monorepoRoot);
logger.setLevel(options.debug ? "debug" : "info");

// Pre-build validation
checkRunningInsideNextjsApp();
Expand Down Expand Up @@ -139,7 +141,7 @@ function checkRunningInsideNextjsApp() {
fs.existsSync(path.join(appPath, `next.config.${ext}`)),
);
if (!extension) {
console.error(
logger.error(
"Error: next.config.js not found. Please make sure you are running this command inside a Next.js app.",
);
process.exit(1);
Expand All @@ -157,7 +159,7 @@ function findMonorepoRoot(appPath: string) {

if (found) {
if (currentPath !== appPath) {
console.info("Monorepo detected at", currentPath);
logger.info("Monorepo detected at", currentPath);
}
return { root: currentPath, packager: found.packager };
}
Expand Down Expand Up @@ -197,7 +199,7 @@ function buildNextjsApp(packager: "npm" | "yarn" | "pnpm") {

function printHeader(header: string) {
header = `OpenNext — ${header}`;
console.info(
logger.info(
[
"",
"┌" + "─".repeat(header.length + 2) + "┐",
Expand Down Expand Up @@ -226,7 +228,7 @@ function printNextjsVersion() {

function printOpenNextVersion() {
const { openNextVersion } = options;
console.info(`OpenNext v${openNextVersion}`);
logger.info(`OpenNext v${openNextVersion}`);
}

function initOutputDir() {
Expand All @@ -236,7 +238,7 @@ function initOutputDir() {
}

function createWarmerBundle() {
console.info(`Bundling warmer function...`);
logger.info(`Bundling warmer function...`);

const { outputDir } = options;

Expand Down Expand Up @@ -264,7 +266,7 @@ function createWarmerBundle() {
}

async function minifyServerBundle() {
console.info(`Minimizing server function...`);
logger.info(`Minimizing server function...`);
const { outputDir } = options;
await minifyAll(path.join(outputDir, "server-function"), {
compress_json: true,
Expand All @@ -273,7 +275,7 @@ async function minifyServerBundle() {
}

function createRevalidationBundle() {
console.info(`Bundling revalidation function...`);
logger.info(`Bundling revalidation function...`);

const { appBuildOutputPath, outputDir } = options;

Expand All @@ -296,7 +298,7 @@ function createRevalidationBundle() {
}

function createImageOptimizationBundle() {
console.info(`Bundling image optimization function...`);
logger.info(`Bundling image optimization function...`);

const { appPath, appBuildOutputPath, outputDir } = options;

Expand Down Expand Up @@ -364,7 +366,7 @@ function createImageOptimizationBundle() {
}

function createStaticAssets() {
console.info(`Bundling static assets...`);
logger.info(`Bundling static assets...`);

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

Expand Down Expand Up @@ -403,7 +405,7 @@ function createStaticAssets() {
}

function createCacheAssets(monorepoRoot: string, disableDynamoDBCache = false) {
console.info(`Bundling cache assets...`);
logger.info(`Bundling cache assets...`);

const { appBuildOutputPath, outputDir } = options;
const packagePath = path.relative(monorepoRoot, appBuildOutputPath);
Expand Down Expand Up @@ -464,7 +466,7 @@ function createCacheAssets(monorepoRoot: string, disableDynamoDBCache = false) {
case ".map":
break;
default:
console.warn(`Unknown file extension: ${ext}`);
logger.warn(`Unknown file extension: ${ext}`);
break;
}
},
Expand Down Expand Up @@ -582,7 +584,7 @@ function createCacheAssets(monorepoRoot: string, disableDynamoDBCache = false) {
/***************************/

async function createServerBundle(monorepoRoot: string, streaming = false) {
console.info(`Bundling server function...`);
logger.info(`Bundling server function...`);

const { appPath, appBuildOutputPath, outputDir } = options;

Expand Down Expand Up @@ -670,7 +672,7 @@ async function createServerBundle(monorepoRoot: string, streaming = false) {
}

if (plugins && plugins.length > 0) {
console.log(
logger.debug(
`Applying plugins:: [${plugins
.map(({ name }) => name)
.join(",")}] for Next version: ${options.nextVersion}`,
Expand Down Expand Up @@ -844,14 +846,14 @@ function esbuildSync(esbuildOptions: ESBuildOptions) {
...esbuildOptions.banner,
js: [
esbuildOptions.banner?.js || "",
`globalThis.openNextDebug = ${process.env.OPEN_NEXT_DEBUG ?? false};`,
`globalThis.openNextDebug = ${debug};`,
`globalThis.openNextVersion = "${openNextVersion}";`,
].join(""),
},
});

if (result.errors.length > 0) {
result.errors.forEach((error) => console.error(error));
result.errors.forEach((error) => logger.error(error));
throw new Error(
`There was a problem bundling ${
(esbuildOptions.entryPoints as string[])[0]
Expand All @@ -874,14 +876,14 @@ async function esbuildAsync(esbuildOptions: ESBuildOptions) {
...esbuildOptions.banner,
js: [
esbuildOptions.banner?.js || "",
`globalThis.openNextDebug = ${process.env.OPEN_NEXT_DEBUG ?? false};`,
`globalThis.openNextDebug = ${debug};`,
`globalThis.openNextVersion = "${openNextVersion}";`,
].join(""),
},
});

if (result.errors.length > 0) {
result.errors.forEach((error) => console.error(error));
result.errors.forEach((error) => logger.error(error));
throw new Error(
`There was a problem bundling ${
(esbuildOptions.entryPoints as string[])[0]
Expand Down
14 changes: 14 additions & 0 deletions packages/open-next/src/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
type LEVEL = "info" | "debug";

let logLevel: LEVEL = "info";

export default {
setLevel: (level: LEVEL) => (logLevel = level),
debug: (...args: any[]) => {
if (logLevel !== "debug") return;
console.log("DEBUG", ...args);
},
info: console.log,
warn: console.warn,
error: console.error,
};
4 changes: 3 additions & 1 deletion packages/open-next/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import path from "node:path";

import { Plugin } from "esbuild";

import logger from "./logger.js";

export interface IPluginSettings {
target: RegExp;
replacements: string[];
Expand Down Expand Up @@ -72,7 +74,7 @@ export default function openNextPlugin({
const pattern = new RegExp(
`\/\/#override (${id})\n([\\s\\S]*?)\n\/\/#endOverride`,
);
console.log(
logger.debug(
`Open-next plugin ${name} -- Applying override for ${id} from ${fp}`,
);
contents = contents.replace(pattern, replacement);
Expand Down

1 comment on commit 1ed5ffd

@vercel
Copy link

@vercel vercel bot commented on 1ed5ffd Nov 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

open-next – ./

open-next-git-main-sst-dev.vercel.app
open-next-sst-dev.vercel.app
open-next.vercel.app

Please sign in to comment.