Skip to content

Commit 1ed5ffd

Browse files
authored
Add logger library and print plugin build details in debug mode (#311)
* Print plugin info in debug mode * Sync
1 parent 1d83dab commit 1ed5ffd

File tree

4 files changed

+41
-18
lines changed

4 files changed

+41
-18
lines changed

.changeset/proud-poets-bow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"open-next": patch
3+
---
4+
5+
Print plugin info in debug mode

packages/open-next/src/build.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
buildSync,
1111
} from "esbuild";
1212

13+
import logger from "./logger.js";
1314
import { minifyAll } from "./minimize-js.js";
1415
import openNextPlugin from "./plugin.js";
1516

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

8586
// Initialize options
8687
options = normalizeOptions(opts, monorepoRoot);
88+
logger.setLevel(options.debug ? "debug" : "info");
8789

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

158160
if (found) {
159161
if (currentPath !== appPath) {
160-
console.info("Monorepo detected at", currentPath);
162+
logger.info("Monorepo detected at", currentPath);
161163
}
162164
return { root: currentPath, packager: found.packager };
163165
}
@@ -197,7 +199,7 @@ function buildNextjsApp(packager: "npm" | "yarn" | "pnpm") {
197199

198200
function printHeader(header: string) {
199201
header = `OpenNext — ${header}`;
200-
console.info(
202+
logger.info(
201203
[
202204
"",
203205
"┌" + "─".repeat(header.length + 2) + "┐",
@@ -226,7 +228,7 @@ function printNextjsVersion() {
226228

227229
function printOpenNextVersion() {
228230
const { openNextVersion } = options;
229-
console.info(`OpenNext v${openNextVersion}`);
231+
logger.info(`OpenNext v${openNextVersion}`);
230232
}
231233

232234
function initOutputDir() {
@@ -236,7 +238,7 @@ function initOutputDir() {
236238
}
237239

238240
function createWarmerBundle() {
239-
console.info(`Bundling warmer function...`);
241+
logger.info(`Bundling warmer function...`);
240242

241243
const { outputDir } = options;
242244

@@ -264,7 +266,7 @@ function createWarmerBundle() {
264266
}
265267

266268
async function minifyServerBundle() {
267-
console.info(`Minimizing server function...`);
269+
logger.info(`Minimizing server function...`);
268270
const { outputDir } = options;
269271
await minifyAll(path.join(outputDir, "server-function"), {
270272
compress_json: true,
@@ -273,7 +275,7 @@ async function minifyServerBundle() {
273275
}
274276

275277
function createRevalidationBundle() {
276-
console.info(`Bundling revalidation function...`);
278+
logger.info(`Bundling revalidation function...`);
277279

278280
const { appBuildOutputPath, outputDir } = options;
279281

@@ -296,7 +298,7 @@ function createRevalidationBundle() {
296298
}
297299

298300
function createImageOptimizationBundle() {
299-
console.info(`Bundling image optimization function...`);
301+
logger.info(`Bundling image optimization function...`);
300302

301303
const { appPath, appBuildOutputPath, outputDir } = options;
302304

@@ -364,7 +366,7 @@ function createImageOptimizationBundle() {
364366
}
365367

366368
function createStaticAssets() {
367-
console.info(`Bundling static assets...`);
369+
logger.info(`Bundling static assets...`);
368370

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

@@ -403,7 +405,7 @@ function createStaticAssets() {
403405
}
404406

405407
function createCacheAssets(monorepoRoot: string, disableDynamoDBCache = false) {
406-
console.info(`Bundling cache assets...`);
408+
logger.info(`Bundling cache assets...`);
407409

408410
const { appBuildOutputPath, outputDir } = options;
409411
const packagePath = path.relative(monorepoRoot, appBuildOutputPath);
@@ -464,7 +466,7 @@ function createCacheAssets(monorepoRoot: string, disableDynamoDBCache = false) {
464466
case ".map":
465467
break;
466468
default:
467-
console.warn(`Unknown file extension: ${ext}`);
469+
logger.warn(`Unknown file extension: ${ext}`);
468470
break;
469471
}
470472
},
@@ -582,7 +584,7 @@ function createCacheAssets(monorepoRoot: string, disableDynamoDBCache = false) {
582584
/***************************/
583585

584586
async function createServerBundle(monorepoRoot: string, streaming = false) {
585-
console.info(`Bundling server function...`);
587+
logger.info(`Bundling server function...`);
586588

587589
const { appPath, appBuildOutputPath, outputDir } = options;
588590

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

672674
if (plugins && plugins.length > 0) {
673-
console.log(
675+
logger.debug(
674676
`Applying plugins:: [${plugins
675677
.map(({ name }) => name)
676678
.join(",")}] for Next version: ${options.nextVersion}`,
@@ -844,14 +846,14 @@ function esbuildSync(esbuildOptions: ESBuildOptions) {
844846
...esbuildOptions.banner,
845847
js: [
846848
esbuildOptions.banner?.js || "",
847-
`globalThis.openNextDebug = ${process.env.OPEN_NEXT_DEBUG ?? false};`,
849+
`globalThis.openNextDebug = ${debug};`,
848850
`globalThis.openNextVersion = "${openNextVersion}";`,
849851
].join(""),
850852
},
851853
});
852854

853855
if (result.errors.length > 0) {
854-
result.errors.forEach((error) => console.error(error));
856+
result.errors.forEach((error) => logger.error(error));
855857
throw new Error(
856858
`There was a problem bundling ${
857859
(esbuildOptions.entryPoints as string[])[0]
@@ -874,14 +876,14 @@ async function esbuildAsync(esbuildOptions: ESBuildOptions) {
874876
...esbuildOptions.banner,
875877
js: [
876878
esbuildOptions.banner?.js || "",
877-
`globalThis.openNextDebug = ${process.env.OPEN_NEXT_DEBUG ?? false};`,
879+
`globalThis.openNextDebug = ${debug};`,
878880
`globalThis.openNextVersion = "${openNextVersion}";`,
879881
].join(""),
880882
},
881883
});
882884

883885
if (result.errors.length > 0) {
884-
result.errors.forEach((error) => console.error(error));
886+
result.errors.forEach((error) => logger.error(error));
885887
throw new Error(
886888
`There was a problem bundling ${
887889
(esbuildOptions.entryPoints as string[])[0]

packages/open-next/src/logger.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
type LEVEL = "info" | "debug";
2+
3+
let logLevel: LEVEL = "info";
4+
5+
export default {
6+
setLevel: (level: LEVEL) => (logLevel = level),
7+
debug: (...args: any[]) => {
8+
if (logLevel !== "debug") return;
9+
console.log("DEBUG", ...args);
10+
},
11+
info: console.log,
12+
warn: console.warn,
13+
error: console.error,
14+
};

packages/open-next/src/plugin.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import path from "node:path";
33

44
import { Plugin } from "esbuild";
55

6+
import logger from "./logger.js";
7+
68
export interface IPluginSettings {
79
target: RegExp;
810
replacements: string[];
@@ -72,7 +74,7 @@ export default function openNextPlugin({
7274
const pattern = new RegExp(
7375
`\/\/#override (${id})\n([\\s\\S]*?)\n\/\/#endOverride`,
7476
);
75-
console.log(
77+
logger.debug(
7678
`Open-next plugin ${name} -- Applying override for ${id} from ${fp}`,
7779
);
7880
contents = contents.replace(pattern, replacement);

0 commit comments

Comments
 (0)