From e7a90581c2e7dba2047e38b9108462e0f5f4fbdd Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Tue, 27 Sep 2022 09:26:15 +0000 Subject: [PATCH] ref(nextjs): Make `isBuild` rely on `NEXT_PHASE` environment variable --- packages/nextjs/src/utils/isBuild.ts | 25 ++++------- packages/nextjs/test/utils/isBuild.test.ts | 51 ---------------------- 2 files changed, 8 insertions(+), 68 deletions(-) delete mode 100644 packages/nextjs/test/utils/isBuild.test.ts diff --git a/packages/nextjs/src/utils/isBuild.ts b/packages/nextjs/src/utils/isBuild.ts index 2e849807d8dd..609c6fbb41a8 100644 --- a/packages/nextjs/src/utils/isBuild.ts +++ b/packages/nextjs/src/utils/isBuild.ts @@ -2,21 +2,12 @@ * Decide if the currently running process is part of the build phase or happening at runtime. */ export function isBuild(): boolean { - // During build, the main process is invoked by - // `node next build` - // and child processes are invoked as - // `node /node_modules/.../jest-worker/processChild.js`. - // The former is (obviously) easy to recognize, but the latter could happen at runtime as well. Fortunately, the main - // process hits this file before any of the child processes do, so we're able to set an env variable which the child - // processes can then check. During runtime, the main process is invoked as - // `node next start` - // or - // `node /var/runtime/index.js`, - // so we never drop into the `if` in the first place. - if (process.argv.includes('build') || process.env.SENTRY_BUILD_PHASE) { - process.env.SENTRY_BUILD_PHASE = 'true'; - return true; - } - - return false; + // Next.js sets the `NEXT_PHASE` env var depending on what phase/environment we're in. + // These phases are constants within the Next.js codebase but sadly they're not exported in a way that we can easily + // import them with our rollup setup so we're simply vendoring the relevant constant ourselves. + // This constant hasn't changed in the Next.js codebase since next@10: + // Most recent: https://github.com/vercel/next.js/blob/406d69d4d9f7d14b9bf497a134f0151914b13964/packages/next/shared/lib/constants.ts#L20 + // v10: https://github.com/vercel/next.js/blob/118ab7992bc8f7a7e5a7bb996510d9b56ffe4f68/packages/next/next-server/lib/constants.ts#L2 + const PHASE_PRODUCTION_BUILD = 'phase-production-build'; + return process.env.NEXT_PHASE === PHASE_PRODUCTION_BUILD; } diff --git a/packages/nextjs/test/utils/isBuild.test.ts b/packages/nextjs/test/utils/isBuild.test.ts deleted file mode 100644 index 0bc1523b041a..000000000000 --- a/packages/nextjs/test/utils/isBuild.test.ts +++ /dev/null @@ -1,51 +0,0 @@ -import { isBuild } from '../../src/utils/isBuild'; - -let originalEnv: typeof process.env; -let originalArgv: typeof process.argv; - -function assertNoMagicValues(): void { - if (Object.keys(process.env).includes('SENTRY_BUILD_PHASE') || process.argv.includes('build')) { - throw new Error('Not starting test with a clean setup'); - } -} - -describe('isBuild()', () => { - beforeEach(() => { - assertNoMagicValues(); - originalEnv = { ...process.env }; - originalArgv = [...process.argv]; - }); - - afterEach(() => { - process.env = originalEnv; - process.argv = originalArgv; - assertNoMagicValues(); - }); - - it("detects 'build' in argv", () => { - // the result of calling `next build` - process.argv = ['/abs/path/to/node', '/abs/path/to/nextjs/excecutable', 'build']; - expect(isBuild()).toBe(true); - }); - - it("sets env var when 'build' in argv", () => { - // the result of calling `next build` - process.argv = ['/abs/path/to/node', '/abs/path/to/nextjs/excecutable', 'build']; - isBuild(); - expect(Object.keys(process.env).includes('SENTRY_BUILD_PHASE')).toBe(true); - }); - - it("does not set env var when 'build' not in argv", () => { - isBuild(); - expect(Object.keys(process.env).includes('SENTRY_BUILD_PHASE')).toBe(false); - }); - - it('detects env var', () => { - process.env.SENTRY_BUILD_PHASE = 'true'; - expect(isBuild()).toBe(true); - }); - - it("returns false when 'build' not in `argv` and env var not present", () => { - expect(isBuild()).toBe(false); - }); -});