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

This commit is contained in:
Jack Hsu 2024-02-27 14:57:39 -05:00 committed by GitHub
parent e732e7116f
commit 207b4926d1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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.
// 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',
@ -126,22 +126,20 @@ function withNx(
} else {
const {
createProjectGraphAsync,
readCachedProjectGraph,
joinPathFragments,
offsetFromRoot,
workspaceRoot,
} = require('@nx/devkit');
let graph = readCachedProjectGraph();
if (!graph) {
let graph: ProjectGraph;
try {
graph = await createProjectGraphAsync();
} catch (e) {
throw new Error(
'Could not create project graph. Please ensure that your workspace is valid.'
'Could not create project graph. Please ensure that your workspace is valid.',
{ cause: e }
);
}
}
const originalTarget = {
project: process.env.NX_TASK_TARGET_PROJECT,