Skip to content
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

feat(nextjs): use global NX_GRAPH_CREATION in withNx plugin to guard against graph creation during create nodes #22026

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions packages/next/plugins/with-nx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ function withNx(
const { PHASE_PRODUCTION_SERVER, PHASE_DEVELOPMENT_SERVER } = await import(
'next/constants'
);
if (
PHASE_PRODUCTION_SERVER === phase ||
!process.env.NX_TASK_TARGET_TARGET
) {
// If we are running an already built production server, just return the configuration.
// NOTE: Avoid any `require(...)` or `import(...)` statements here. Development dependencies are not available at production runtime.
// Two scenarios where we want to skip graph creation:
// 1. Running production server means the build is already done so we just need to start the Next.js server.
// 2. During graph creation (i.e. create nodes), we won't have a graph to read, and it is not needed anyway since it's a build-time concern.
//
// NOTE: Avoid any `require(...)` or `import(...)` statements here. Development dependencies are not available at production runtime.
if (PHASE_PRODUCTION_SERVER === phase || global.NX_GRAPH_CREATION) {
const { nx, ...validNextConfig } = _nextConfig;
return {
distDir: '.next',
Expand All @@ -126,21 +126,19 @@ function withNx(
} else {
const {
createProjectGraphAsync,
readCachedProjectGraph,
joinPathFragments,
offsetFromRoot,
workspaceRoot,
} = require('@nx/devkit');

let graph = readCachedProjectGraph();
if (!graph) {
try {
graph = await createProjectGraphAsync();
} catch (e) {
throw new Error(
'Could not create project graph. Please ensure that your workspace is valid.'
);
}
let graph: ProjectGraph;
try {
graph = await createProjectGraphAsync();
} catch (e) {
throw new Error(
'Could not create project graph. Please ensure that your workspace is valid.',
{ cause: e }
);
}

const originalTarget = {
Expand Down