From 45ffbdfa36a606bab08eebf6e0e0ae824d92b4cc Mon Sep 17 00:00:00 2001 From: Jack Hsu Date: Tue, 9 May 2023 12:43:35 -0400 Subject: [PATCH] fix(nextjs): inline dev-only dependencies and add e2e test to catch issues --- e2e/next/src/next.test.ts | 6 ++++++ packages/next/plugins/with-nx.ts | 6 ++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/e2e/next/src/next.test.ts b/e2e/next/src/next.test.ts index 3eb3ed59858dc..7136362c045ac 100644 --- a/e2e/next/src/next.test.ts +++ b/e2e/next/src/next.test.ts @@ -177,6 +177,12 @@ describe('Next.js Applications', () => { `dist/packages/${appName}/public/shared/ui/hello.txt` ); + // Check that compiled next config does not contain bad imports + const nextConfigPath = `dist/packages/${appName}/next.config.js`; + expect(nextConfigPath).not.toContain(`require("../`); // missing relative paths + expect(nextConfigPath).not.toContain(`require("nx/`); // dev-only packages + expect(nextConfigPath).not.toContain(`require("@nx/`); // dev-only packages + // Check that `nx serve --prod` works with previous production build (e.g. `nx build `). const prodServePort = 4000; const prodServeProcess = await runCommandUntil( diff --git a/packages/next/plugins/with-nx.ts b/packages/next/plugins/with-nx.ts index bff1b15255845..72bbe1bfbbb31 100644 --- a/packages/next/plugins/with-nx.ts +++ b/packages/next/plugins/with-nx.ts @@ -8,8 +8,6 @@ import type { NextConfigFn } from '../src/utils/config'; 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'; -import { readTsConfigPaths } from '@nx/js'; -import { findAllProjectNodeDependencies } from 'nx/src/utils/project-graph-utils'; export interface WithNxOptions extends NextConfig { nx?: { @@ -222,6 +220,10 @@ function withNx( forNextVersion('>=13.1.0', () => { if (!graph.dependencies[project]) return; + const { readTsConfigPaths } = require('@nx/js'); + const { + findAllProjectNodeDependencies, + } = require('nx/src/utils/project-graph-utils'); const paths = readTsConfigPaths(); const deps = findAllProjectNodeDependencies(project); nextConfig.transpilePackages ??= [];