Skip to content

Commit 326bdc7

Browse files
authored
Merge pull request #207 from mi-examples/feat/next-build-parity
feat: warn when a wrapped Next.js app builds via plain `next build`
2 parents 5272047 + be6d4c2 commit 326bdc7

8 files changed

Lines changed: 43 additions & 6 deletions

File tree

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,11 @@ ZIP archive. `pp-dev next-build` runs `next build` and then applies the same pos
371371
2. Writes `VERSION-*.json` + `BUILD-MANIFEST.json` into the export directory (`build.versionFile`)
372372
3. Zips the export directory into `dist-zip/<name>.zip` (`build.zip`)
373373

374+
If a project using `withPPDev()` still runs plain `next build` (e.g. directly, or via a `build`
375+
script nobody updated), `withPPDev()` prints a console warning during the production build phase
376+
suggesting `pp-dev next-build` instead. The warning is suppressed automatically when the build was
377+
started by `pp-dev next-build` itself.
378+
374379
Use it in place of `next build` in your `package.json`:
375380

376381
```json

src/cli.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
PATH_PAGE_PREFIX,
3434
PATH_TEMPLATE_PREFIX,
3535
PATH_TEMPLATE_LOCAL_PREFIX,
36+
PP_DEV_NEXT_BUILD_ENV_VAR,
3637
} from './constants.js';
3738
import { normalizePPDevConfig, validatePPDevConfig } from './plugin.js';
3839
import { RequestStore } from './lib/request-store.js';
@@ -1149,6 +1150,10 @@ cli
11491150
let exportDistDir: string | undefined = config?.distDir;
11501151

11511152
try {
1153+
// Not a real `next build` — just peeking at the prod config for distDir. Suppress
1154+
// withPPDev()'s "use pp-dev next-build" nudge for this internal lookup.
1155+
process.env[PP_DEV_NEXT_BUILD_ENV_VAR] = '1';
1156+
11521157
const prodConfig = await loadConfig(constants.PHASE_PRODUCTION_BUILD, projectRoot);
11531158

11541159
exportDistDir = prodConfig?.distDir ?? exportDistDir;
@@ -1564,6 +1569,10 @@ cli
15641569
const projectRoot = root ? path.resolve(process.cwd(), root) : process.cwd();
15651570
const cliOverrides = resolveBuildCliOverrides(options);
15661571

1572+
// Also covers the `loadConfig()` call below, which evaluates the Next.js config (and thus
1573+
// `withPPDev()`) in this process before `runNextBuildProcess()` spawns the actual `next build`.
1574+
process.env[PP_DEV_NEXT_BUILD_ENV_VAR] = '1';
1575+
15671576
try {
15681577
const { constants } = await safeNextImport();
15691578

src/constants.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ export const PATH_PAGE_PREFIX = '/p';
3636
export const PATH_TEMPLATE_PREFIX = '/pt';
3737
export const PATH_TEMPLATE_LOCAL_PREFIX = '/pl';
3838

39+
/**
40+
* Set (to '1') in the child `next build` process spawned by `pp-dev next-build` (and the
41+
* WS-triggered sync build in DistService). `withPPDev()` checks this during the production
42+
* build phase to tell a wrapped `next build` apart from a bare one, so it can nudge users
43+
* running `next build` directly toward `pp-dev next-build` (VERSION/BUILD-MANIFEST/zip parity).
44+
*/
45+
export const PP_DEV_NEXT_BUILD_ENV_VAR = 'PP_DEV_NEXT_BUILD';
46+
3947
export const PP_DEV_CONFIG_NAMES = [
4048
'.pp-dev.config.js',
4149
'.pp-dev.config.cjs',

src/index.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@ import type { NextConfig } from 'next';
88
import { safeNextImport } from './lib/next-import.js';
99
import { getConfig, getPkg } from './config.js';
1010
import type { PPDevConfig } from './plugin.js';
11-
import { PATH_PAGE_PREFIX, PATH_TEMPLATE_PREFIX, PATH_TEMPLATE_LOCAL_PREFIX } from './constants.js';
11+
import {
12+
PATH_PAGE_PREFIX,
13+
PATH_TEMPLATE_PREFIX,
14+
PATH_TEMPLATE_LOCAL_PREFIX,
15+
PP_DEV_NEXT_BUILD_ENV_VAR,
16+
} from './constants.js';
1217
import { createLogger } from './lib/logger.js';
18+
import { colors } from './lib/helpers/color.helper.js';
1319
import { applyDistZipOverride, applyVersionManifestOverride, ResolvedBuildCliOverrides } from './lib/build-cli-overrides.js';
1420

1521
export type { ResolvedBuildCliOverrides } from './lib/build-cli-overrides.js';
@@ -323,7 +329,15 @@ export function withPPDev(
323329
return async (phase: string, nextConfig: { defaultConfig?: any } = {}): Promise<NextConfig> => {
324330
try {
325331
const { constants } = await safeNextImport();
326-
const { PHASE_DEVELOPMENT_SERVER } = constants;
332+
const { PHASE_DEVELOPMENT_SERVER, PHASE_PRODUCTION_BUILD } = constants;
333+
334+
if (phase === PHASE_PRODUCTION_BUILD && !process.env[PP_DEV_NEXT_BUILD_ENV_VAR]) {
335+
createLogger().warn(
336+
colors.yellow(
337+
'⚠ Building with plain `next build`. Use `pp-dev next-build` instead to get VERSION/BUILD-MANIFEST/zip build output in the same format as `pp-dev build` (see the "Next.js Build" section in the pp-dev README).',
338+
),
339+
);
340+
}
327341

328342
const config = await getConfig();
329343
const pkg = getPkg();

src/lib/next-build-runner.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as path from 'path';
22
import * as child_process from 'child_process';
33
import * as process from 'process';
44
import { createRequire } from 'module';
5+
import { PP_DEV_NEXT_BUILD_ENV_VAR } from '../constants.js';
56

67
/** Run `next build` in `projectRoot`, resolving the `next` binary from the app itself. */
78
export function runNextBuildProcess(projectRoot: string): Promise<void> {
@@ -21,7 +22,7 @@ export function runNextBuildProcess(projectRoot: string): Promise<void> {
2122

2223
const proc = child_process.spawn(process.execPath, [nextBin, 'build'], {
2324
cwd: projectRoot,
24-
env: Object.assign({}, process.env, { NODE_ENV: 'production' }),
25+
env: Object.assign({}, process.env, { NODE_ENV: 'production', [PP_DEV_NEXT_BUILD_ENV_VAR]: '1' }),
2526
stdio: 'inherit',
2627
});
2728

tests/test-commonjs/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/test-nextjs-cjs/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/test-nextjs/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)