From 6b98ff0b9ceaacdc9c59ae5b28567131924a2ed9 Mon Sep 17 00:00:00 2001 From: Nicholas Cunningham Date: Mon, 13 Mar 2023 17:47:08 -0600 Subject: [PATCH] fix(nextjs): move webpack config to withNx from build.impl --- packages/next/plugins/with-nx.ts | 92 ++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/packages/next/plugins/with-nx.ts b/packages/next/plugins/with-nx.ts index d750a6f13f1172..e80b8d7a69ab05 100644 --- a/packages/next/plugins/with-nx.ts +++ b/packages/next/plugins/with-nx.ts @@ -1,8 +1,38 @@ +import { + createProjectGraphAsync, + joinPathFragments, + offsetFromRoot, + workspaceRoot, +} from '@nrwl/devkit'; +import { + calculateProjectDependencies, + DependentBuildableProjectNode, +} from '@nrwl/js/src/utils/buildable-libs-utils'; import type { NextConfig } from 'next'; +import { + PHASE_DEVELOPMENT_SERVER, + PHASE_EXPORT, + PHASE_PRODUCTION_BUILD, + PHASE_PRODUCTION_SERVER, + PHASE_TEST, +} from 'next/constants'; +import { + createProjectRootMappings, + findProjectForPath, +} from 'nx/src/project-graph/utils/find-project-for-path'; +import path = require('path'); +import { createWebpackConfig } from '../src/utils/config'; +export type PHASE = + | typeof PHASE_EXPORT + | typeof PHASE_PRODUCTION_BUILD + | typeof PHASE_DEVELOPMENT_SERVER + | typeof PHASE_PRODUCTION_SERVER + | typeof PHASE_TEST; export interface WithNxOptions extends NextConfig { nx?: { svgr?: boolean; + projectRoot: string; }; } @@ -34,6 +64,67 @@ function getWithNxContext(): WithNxContext { }; } +// return () => Promise +export function withNxnew( + _nextConfig = {} as WithNxOptions, + context: WithNxContext = getWithNxContext() +) { + const projectDirectory = path.relative( + workspaceRoot, + _nextConfig.nx.projectRoot + ); + return async (phase: PHASE, defaultConfig: NextConfig) => { + let dependencies: DependentBuildableProjectNode[] = []; + + const graph = await createProjectGraphAsync(); + const rootMap = createProjectRootMappings(graph.nodes); + const project = findProjectForPath(projectDirectory, rootMap); + const projectNode = graph.nodes[project]; + + // Assuming we know the target else we have to look it + const targetName = 'build-base'; + const configurationName = null; + + const options = projectNode.data.targets[targetName].options; + + if (!options.buildLibsFromSource && targetName) { + const result = calculateProjectDependencies( + graph, + workspaceRoot, + project, + targetName, + configurationName + ); + dependencies = result.dependencies; + } + + // Get next config + const nextConfig = withNx(_nextConfig, context); + + const outputDir = `${offsetFromRoot(projectDirectory)}${ + options.outputPath + }`; + nextConfig.distDir = + nextConfig.distDir && nextConfig.distDir !== '.next' + ? joinPathFragments(outputDir, nextConfig.distDir) + : joinPathFragments(outputDir, '.next'); + + const userWebpackConfig = nextConfig.webpack; + + nextConfig.webpack = (a, b) => + createWebpackConfig( + projectDirectory, + options.root, + options.fileReplacements, + options.assets, + dependencies, + path.join(workspaceRoot, context.libsDir) + )(userWebpackConfig ? userWebpackConfig(a, b) : a, b); + + return nextConfig; + }; +} + export function withNx( nextConfig = {} as WithNxOptions, context: WithNxContext = getWithNxContext() @@ -223,3 +314,4 @@ function addNxEnvVariables(config: any) { module.exports = withNx; // Support for newer generated code: `const { withNx } = require(...);` module.exports.withNx = withNx; +module.exports.withNxnew = withNxnew;