Skip to content

Commit

Permalink
fix(nextjs): move webpack config to withNx from build.impl
Browse files Browse the repository at this point in the history
  • Loading branch information
ndcunningham committed Mar 13, 2023
1 parent 7450e72 commit 6b98ff0
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions packages/next/plugins/with-nx.ts
Original file line number Diff line number Diff line change
@@ -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;
};
}

Expand Down Expand Up @@ -34,6 +64,67 @@ function getWithNxContext(): WithNxContext {
};
}

// return () => Promise<NextConfig>
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()
Expand Down Expand Up @@ -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;

0 comments on commit 6b98ff0

Please sign in to comment.