Skip to content

Commit

Permalink
feat(nextjs): use global NX_GRAPH_CREATION in withNx plugin to guard …
Browse files Browse the repository at this point in the history
…against graph creation during create nodes
  • Loading branch information
jaysoo committed Feb 27, 2024
1 parent e732e71 commit cdc3a66
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 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 @@ -132,15 +132,14 @@ function withNx(
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

0 comments on commit cdc3a66

Please sign in to comment.