Skip to content

Commit

Permalink
fix(nextjs): Deprecate adding NX_ vars to Webpack Define (#18892)
Browse files Browse the repository at this point in the history
  • Loading branch information
ndcunningham authored Aug 29, 2023
1 parent 74e5879 commit da2a08e
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion packages/next/plugins/with-nx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,36 @@ import type { NextBuildBuilderOptions } from '../src/utils/types';
import type { DependentBuildableProjectNode } from '@nx/js/src/utils/buildable-libs-utils';
import type { ProjectGraph, ProjectGraphProjectNode, Target } from '@nx/devkit';

const baseNXEnvironmentVariables = [
'NX_BASE',
'NX_CACHE_DIRECTORY',
'NX_CACHE_PROJECT_GRAPH',
'NX_DAEMON',
'NX_DEFAULT_PROJECT',
'NX_HEAD',
'NX_PERF_LOGGING',
'NX_PROFILE',
'NX_PROJECT_GRAPH_CACHE_DIRECTORY',
'NX_PROJECT_GRAPH_MAX_WORKERS',
'NX_RUNNER',
'NX_SKIP_NX_CACHE',
'NX_TASKS_RUNNER',
'NX_TASKS_RUNNER_DYNAMIC_OUTPUT',
'NX_VERBOSE_LOGGING',
'NX_DRY_RUN',
'NX_INTERACTIVE',
'NX_GENERATE_QUIET',
'NX_PREFER_TS_NODE',
'NX_TASK_TARGET_PROJECT',
'NX_TASK_TARGET_TARGET',
'NX_TASK_TARGET_CONFIGURATION',
'NX_CLI_SET',
'NX_LOAD_DOT_ENV_FILES',
'NX_WORKSPACE_ROOT',
'NX_TASK_HASH',
'NX_NEXT_DIR',
];

export interface WithNxOptions extends NextConfig {
nx?: {
svgr?: boolean;
Expand Down Expand Up @@ -364,6 +394,12 @@ export function getNextConfig(
};
}

function getNonBaseVariables(oldEnv) {
return Object.keys(oldEnv).filter(
(env) => !baseNXEnvironmentVariables.includes(env)
);
}

function getNxEnvironmentVariables() {
return Object.keys(process.env)
.filter((env) => /^NX_/i.test(env))
Expand All @@ -372,7 +408,10 @@ function getNxEnvironmentVariables() {
return env;
}, {});
}

/**
* TODO(v18)
* @deprecated Use Next.js 9.4+ built-in support for environment variables. Reference https://nextjs.org/docs/pages/api-reference/next-config-js/env
*/
function addNxEnvVariables(config: any) {
const maybeDefinePlugin = config.plugins?.find((plugin) => {
return plugin.definitions?.['process.env.NODE_ENV'];
Expand All @@ -387,6 +426,15 @@ function addNxEnvVariables(config: any) {
.forEach(
([name, value]) => (maybeDefinePlugin.definitions[name] = value)
);

const vars = getNonBaseVariables(env);
if (vars.length > 0) {
console.warn(
`Warning, in Nx 18 environment variables starting with NX_ will not be available in the browser, and currently will not work with @nx/next:server executor.\nPlease rename the following environment variables: ${vars.join(
', '
)} using Next.js' built-in support for environment variables. Reference https://nextjs.org/docs/pages/api-reference/next-config-js/env`
);
}
}
}

Expand Down

0 comments on commit da2a08e

Please sign in to comment.