-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
ref(nextjs): Extract isBuild
into an exported function
#5444
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* 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 <path>/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; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
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); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.