@nx/next
Version:
118 lines (117 loc) • 5.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.customServerGenerator = customServerGenerator;
const devkit_1 = require("@nx/devkit");
const devkit_2 = require("@nx/devkit");
const path_1 = require("path");
const add_swc_to_custom_server_1 = require("../../utils/add-swc-to-custom-server");
const ts_solution_setup_1 = require("@nx/js/src/utils/typescript/ts-solution-setup");
async function customServerGenerator(host, options) {
const project = (0, devkit_2.readProjectConfiguration)(host, options.project);
const swcServerName = '.server.swcrc';
const nxJson = (0, devkit_2.readNxJson)(host);
const hasPlugin = nxJson.plugins?.some((p) => typeof p === 'string'
? p === '@nx/next/plugin'
: p.plugin === '@nx/next/plugin');
if (project.targets?.build?.executor !== '@nx/next:build' && !hasPlugin) {
devkit_2.logger.error(`Project ${options.project} is not a Next.js project. Did you generate it with "nx g @nx/next:app"?`);
return;
}
// In Nx 18 next artifacts are inside the project root .next/ & dist/ (for custom server)
const outputPath = `dist/${project.root}-server`;
const root = project.root;
if ((!root ||
!outputPath ||
!project.targets?.build?.configurations?.development ||
!project.targets?.build?.configurations?.production) &&
!hasPlugin) {
devkit_2.logger.error(`Project ${options.project} has invalid config. Did you generate it with "nx g @nx/next:app"?`);
return;
}
if (project.targets?.['build-custom-server'] ||
project.targets?.['serve-custom-server']) {
devkit_2.logger.warn(`Project ${options.project} has custom server targets already: build-custom-server, serve-custom-server. Remove these targets from project and try again.`);
return;
}
// In Nx 18 next artifacts are inside the project root .next/ & dist/ (for custom server)
// So we need ensure the mapping is correct from dist to the project root
const projectPathFromDist = hasPlugin
? `../../${(0, devkit_2.offsetFromRoot)(project.root)}${project.root}`
: `${(0, devkit_2.offsetFromRoot)(`dist/${project.root}`)}${project.root}`;
const offset = (0, devkit_2.offsetFromRoot)(project.root);
const isTsSolution = (0, ts_solution_setup_1.isUsingTsSolutionSetup)(host);
(0, devkit_2.generateFiles)(host, (0, path_1.join)(__dirname, 'files'), project.root, {
...options,
hasPlugin,
projectPathFromDist,
offsetFromRoot: offset,
projectRoot: project.root,
baseTsConfigPath: isTsSolution
? (0, devkit_1.joinPathFragments)(offset, 'tsconfig.base.json')
: './tsconfig.json',
tmpl: '',
});
if (!hasPlugin) {
project.targets.build.dependsOn = ['build-custom-server'];
project.targets.serve.options.customServerTarget = `${options.project}:serve-custom-server`;
project.targets.serve.configurations.development.customServerTarget = `${options.project}:serve-custom-server:development`;
project.targets.serve.configurations.production.customServerTarget = `${options.project}:serve-custom-server:production`;
}
else {
project.targets['build'] = {
dependsOn: ['^build', 'build-custom-server'],
};
}
project.targets['build-custom-server'] = {
executor: options.compiler === 'tsc' ? '@nx/js:tsc' : '@nx/js:swc',
defaultConfiguration: 'production',
options: {
outputPath,
main: `${root}/server/main.ts`,
tsConfig: `${root}/tsconfig.server.json`,
clean: false,
assets: [],
...(options.compiler === 'tsc'
? {}
: { swcrc: `${root}/${swcServerName}` }),
},
configurations: {
development: {},
production: {},
},
};
project.targets['serve-custom-server'] = {
executor: '@nx/js:node',
defaultConfiguration: 'production',
options: {
buildTarget: `${options.project}:build-custom-server`,
},
configurations: {
development: {
buildTarget: `${options.project}:build-custom-server:development`,
},
production: {
buildTarget: `${options.project}:build-custom-server:production`,
},
},
};
(0, devkit_2.updateProjectConfiguration)(host, options.project, project);
(0, devkit_2.updateJson)(host, 'nx.json', (json) => {
if (!json.tasksRunnerOptions?.default?.options?.cacheableOperations?.includes('build-custom-server') &&
json.tasksRunnerOptions?.default?.options?.cacheableOperations) {
json.tasksRunnerOptions.default.options.cacheableOperations.push('build-custom-server');
}
json.targetDefaults ??= {};
json.targetDefaults['build-custom-server'] ??= {};
json.targetDefaults['build-custom-server'].cache ??= true;
return json;
});
if (options.compiler === 'swc') {
// Update app swc to exlude server files
(0, devkit_2.updateJson)(host, (0, path_1.join)(project.root, '.swcrc'), (json) => {
json.exclude = [...(json.exclude ?? []), 'server/**'];
return json;
});
return (0, add_swc_to_custom_server_1.configureForSwc)(host, project.root, swcServerName, ['src/**/*']);
}
}