Skip to content

Commit ce80105

Browse files
fix: next-on-pages executor (#31)
1 parent 2018689 commit ce80105

File tree

4 files changed

+63
-44
lines changed

4 files changed

+63
-44
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import {
2+
uniq,
3+
runNxCommand,
4+
ensureNxProject,
5+
cleanup,
6+
readJson,
7+
updateFile,
8+
} from '@nx/plugin/testing';
9+
10+
const workerapp = uniq('workerapp');
11+
12+
describe('Next on Pages', () => {
13+
beforeEach(() => {
14+
ensureNxProject(
15+
'@naxodev/nx-cloudflare',
16+
'dist/packages/plugins/nx-cloudflare'
17+
);
18+
19+
runNxCommand(
20+
`generate @nx/next:app --name ${workerapp} --directory="apps" --appDir`
21+
);
22+
});
23+
24+
afterEach(() => cleanup());
25+
26+
it('should build a NextJS application with the next-on-pages script', async () => {
27+
const projectJson = readJson(`apps/${workerapp}/project.json`);
28+
projectJson.targets.build.executor = '@naxodev/nx-cloudflare:next-build';
29+
updateFile(`apps/${workerapp}/project.json`, JSON.stringify(projectJson));
30+
31+
const buildResults = runNxCommand(`build ${workerapp}`);
32+
expect(buildResults).toContain(
33+
`NX Successfully ran target build for project ${workerapp}`
34+
);
35+
}, 120_000);
36+
});

packages/plugins/nx-cloudflare/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@
4141
"webpack": "^5.88.2",
4242
"nx": "^17.0.0",
4343
"@nx/workspace": "^17.0.0",
44-
"@nx/web": "^17.0.0",
45-
"@cloudflare/next-on-pages": "^1.6.3"
44+
"@nx/web": "^17.0.0"
4645
},
4746
"dependencies": {
4847
"@nx/devkit": "^17.0.0",

packages/plugins/nx-cloudflare/src/executors/next-build/build.impl.ts

Lines changed: 25 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,20 @@ import 'dotenv/config';
33
import {
44
detectPackageManager,
55
ExecutorContext,
6+
getPackageManagerCommand,
67
logger,
78
readJsonFile,
8-
workspaceRoot,
99
writeJsonFile,
1010
} from '@nx/devkit';
1111
import { createLockFile, createPackageJson, getLockFileName } from '@nx/js';
12-
import { join, resolve as pathResolve } from 'path';
13-
import { copySync, existsSync, mkdir, writeFileSync } from 'fs-extra';
12+
import { join } from 'path';
13+
import {
14+
copySync,
15+
ensureDirSync,
16+
existsSync,
17+
mkdir,
18+
writeFileSync,
19+
} from 'fs-extra';
1420
import { gte } from 'semver';
1521
import { directoryExists } from '@nx/workspace/src/utilities/fileutils';
1622
import { checkAndCleanWithSemver } from '@nx/devkit/src/utils/semver';
@@ -19,15 +25,15 @@ import { updatePackageJson } from './lib/update-package-json';
1925
import { createNextConfigFile } from './lib/create-next-config-file';
2026
import { checkPublicDirectory } from './lib/check-project';
2127
import { NextBuildBuilderOptions } from '../../utils/types';
22-
import { ChildProcess, fork } from 'child_process';
28+
import { execSync, ExecSyncOptions } from 'child_process';
2329
import { createCliOptions } from '../../utils/create-cli-options';
2430

25-
let childProcess: ChildProcess;
26-
2731
// This executor is a modified version of the original `@nrwl/next:build` executor.
2832
// It's main modification is to use the cloudflare next-on-pages package.
2933
// Because the Cloudflare builder doesn't allow us to locate the build output outside the project root directory
3034
// we need to change the output path config options to the project root directory and then copy the build output to the desired output path.
35+
// We also move from invoking the bin script to executing the command with the `execSync` function. This is because the bin script
36+
// is not exported on the package.json
3137
export default async function buildExecutor(
3238
options: NextBuildBuilderOptions,
3339
context: ExecutorContext
@@ -54,21 +60,18 @@ export default async function buildExecutor(
5460
if (hasReact18) {
5561
process.env['__NEXT_REACT_ROOT'] ||= 'true';
5662
}
63+
ensureDirSync(join(projectRoot, '.vercel'));
5764

5865
const { outputPath: originalOutputPath } = options;
5966
// Set the outputPath to the projectRoot to bypass cloudflare builded limitations.
6067
options.outputPath = projectRoot;
6168

6269
try {
63-
await runCliBuild(workspaceRoot, projectRoot, options);
70+
runCliBuild(projectRoot, options);
6471
} catch (error) {
6572
logger.error(`Error occurred while trying to run the build command`);
6673
logger.error(error);
6774
return { success: false };
68-
} finally {
69-
if (childProcess) {
70-
childProcess.kill();
71-
}
7275
}
7376

7477
if (!directoryExists(options.outputPath)) {
@@ -130,43 +133,24 @@ export default async function buildExecutor(
130133
return { success: true };
131134
}
132135

133-
function runCliBuild(
134-
workspaceRoot: string,
135-
projectRoot: string,
136-
options: NextBuildBuilderOptions
137-
) {
136+
function runCliBuild(projectRoot: string, options: NextBuildBuilderOptions) {
138137
const { experimentalAppOnly, profile, debug, outputPath } = options;
139138

140139
// Set output path here since it can also be set via CLI
141140
// We can retrieve it inside plugins/with-nx
142141
process.env.NX_NEXT_OUTPUT_PATH ??= outputPath;
143142

144143
const args = createCliOptions({ experimentalAppOnly, profile, debug });
145-
return new Promise((resolve, reject) => {
146-
childProcess = fork(
147-
require.resolve('@cloudflare/next-on-pages/bin'),
148-
[...args],
149-
{
150-
cwd: pathResolve(workspaceRoot, projectRoot),
151-
stdio: 'inherit',
152-
env: process.env,
153-
}
154-
);
155144

156-
// Ensure the child process is killed when the parent exits
157-
process.on('exit', () => childProcess.kill());
158-
process.on('SIGTERM', () => childProcess.kill());
159-
160-
childProcess.on('error', (err) => {
161-
reject(err);
162-
});
145+
const pmc = getPackageManagerCommand('npm');
146+
const execSyncOptions: ExecSyncOptions = {
147+
stdio: ['inherit', 'inherit', 'inherit'],
148+
encoding: 'utf-8',
149+
cwd: projectRoot,
150+
};
163151

164-
childProcess.on('exit', (code) => {
165-
if (code === 0) {
166-
resolve(code);
167-
} else {
168-
reject(code);
169-
}
170-
});
171-
});
152+
execSync(
153+
`${pmc.exec} @cloudflare/next-on-pages ${args.join(' ')}`,
154+
execSyncOptions
155+
);
172156
}

tools/scripts/start-local-registry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export default async () => {
1919
const nx = require.resolve('nx');
2020
execFileSync(
2121
nx,
22-
['run-many', '--targets', 'publish', '--ver', '999.0.0', '--tag', 'e2e'],
22+
['run-many', '--targets', 'publish', '--ver', '0.0.0-e2e', '--tag', 'e2e'],
2323
{ env: process.env, stdio: 'inherit' }
2424
);
2525
};

0 commit comments

Comments
 (0)