Skip to content

Commit

Permalink
fix(nextjs): handle buildable libs correctly (#15795)
Browse files Browse the repository at this point in the history
(cherry picked from commit 63cdddf)
  • Loading branch information
jaysoo authored and FrozenPandaz committed Mar 23, 2023
1 parent 53ba54f commit 7c4f043
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 40 deletions.
33 changes: 24 additions & 9 deletions e2e/next/src/next.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,14 @@ describe('Next.js Applications', () => {
const appName = uniq('app');
const nextLib = uniq('nextlib');
const jsLib = uniq('tslib');
const buildableLib = uniq('buildablelib');

runCLI(`generate @nrwl/next:app ${appName} --no-interactive --style=css`);
runCLI(`generate @nrwl/next:lib ${nextLib} --no-interactive`);
runCLI(`generate @nrwl/js:lib ${jsLib} --no-interactive`);
runCLI(
`generate @nrwl/js:lib ${buildableLib} --no-interactive --bundler=vite`
);

// Create file in public that should be copied to dist
updateFile(`apps/${appName}/public/a/b.txt`, `Hello World!`);
Expand Down Expand Up @@ -77,27 +81,36 @@ describe('Next.js Applications', () => {
updateFile(
`libs/${jsLib}/src/lib/${jsLib}.ts`,
`
export function testFn(): string {
export function jsLib(): string {
return 'Hello Nx';
};
// testing whether async-await code in Node / Next.js api routes works as expected
export async function testAsyncFn() {
export async function jsLibAsync() {
return await Promise.resolve('hell0');
}
`
);

updateFile(
`libs/${buildableLib}/src/lib/${buildableLib}.ts`,
`
export function buildableLib(): string {
return 'Hello Buildable';
};
`
);

const mainPath = `apps/${appName}/pages/index.tsx`;
const content = readFile(mainPath);

updateFile(
`apps/${appName}/pages/api/hello.ts`,
`
import { testAsyncFn } from '@${proj}/${jsLib}';
import { jsLibAsync } from '@${proj}/${jsLib}';
export default async function handler(_, res) {
const value = await testAsyncFn();
const value = await jsLibAsync();
res.send(value);
}
`
Expand All @@ -106,7 +119,8 @@ describe('Next.js Applications', () => {
updateFile(
mainPath,
`
import { testFn } from '@${proj}/${jsLib}';
import { jsLib } from '@${proj}/${jsLib}';
import { buildableLib } from '@${proj}/${buildableLib}';
/* eslint-disable */
import dynamic from 'next/dynamic';
Expand All @@ -119,7 +133,8 @@ describe('Next.js Applications', () => {
`</h2>`,
`</h2>
<div>
{testFn()}
{jsLib()}
{buildableLib()}
<TestComponent />
</div>
`
Expand Down Expand Up @@ -194,7 +209,7 @@ describe('Next.js Applications', () => {
updateFile(
`libs/${jsLib}/src/lib/${jsLib}.ts`,
`
export function testFn(): string {
export function jsLib(): string {
return process.env.NX_CUSTOM_VAR;
};
`
Expand All @@ -204,11 +219,11 @@ describe('Next.js Applications', () => {
`apps/${appName}/pages/index.tsx`,
`
import React from 'react';
import { testFn } from '@${proj}/${jsLib}';
import { jsLib } from '@${proj}/${jsLib}';
export const Index = ({ greeting }: any) => {
return (
<p>{testFn()}</p>
<p>{jsLib()}</p>
);
};
export default Index;
Expand Down
2 changes: 1 addition & 1 deletion packages/next/plugins/with-nx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export function withNx(
} = getNxContext(graph, originalTarget);
const projectDirectory = projectNode.data.root;

if (!options.buildLibsFromSource && targetName) {
if (options.buildLibsFromSource === false && targetName) {
const result = calculateProjectDependencies(
graph,
workspaceRoot,
Expand Down
30 changes: 0 additions & 30 deletions packages/next/src/utils/buildable-libs.ts
Original file line number Diff line number Diff line change
@@ -1,30 +0,0 @@
import {
DependentBuildableProjectNode,
findMissingBuildDependencies,
} from '@nrwl/workspace/src/utilities/buildable-libs-utils';
import * as chalk from 'chalk';
import { ExecutorContext, stripIndents } from '@nrwl/devkit';

export function assertDependentProjectsHaveBeenBuilt(
dependencies: DependentBuildableProjectNode[],
context: ExecutorContext
) {
const missing = findMissingBuildDependencies(
context.root,
context.projectName,
context.targetName,
dependencies
);
if (missing.length > 0) {
throw new Error(
chalk.red(stripIndents`
Some of the project ${
context.projectName
}'s dependencies have not been built yet.
Please build these libraries first:
${missing.map((x) => ` - ${x.node.name}`).join('\n')}
`)
);
}
}

0 comments on commit 7c4f043

Please sign in to comment.