UNPKG

@nx/react

Version:

The React plugin for Nx contains executors and generators for managing React applications and libraries within an Nx workspace. It provides: - Integration with libraries such as Jest, Vitest, Playwright, Cypress, and Storybook. - Generators for applica

102 lines (101 loc) 3.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.addRemoteToConfig = addRemoteToConfig; exports.addRemoteDefinition = addRemoteDefinition; exports.addRemoteRoute = addRemoteRoute; const devkit_1 = require("@nx/devkit"); const js_1 = require("@nx/js"); const ast_utils_1 = require("../utils/ast-utils"); const ensure_typescript_1 = require("@nx/js/src/utils/typescript/ensure-typescript"); let tsModule; function addRemoteToConfig(source, app) { if (!tsModule) { tsModule = (0, ensure_typescript_1.ensureTypescript)(); } const assignments = (0, js_1.findNodes)(source, tsModule.SyntaxKind.PropertyAssignment); const remotesAssignment = assignments.find((s) => s.name.getText() === 'remotes'); if (remotesAssignment) { const arrayExpression = remotesAssignment.initializer; if (!arrayExpression.elements) return []; const lastElement = arrayExpression.elements[arrayExpression.elements.length - 1]; return [ lastElement && !arrayExpression.elements.hasTrailingComma ? { type: devkit_1.ChangeType.Insert, index: lastElement.end, text: `,`, } : null, { type: devkit_1.ChangeType.Insert, index: remotesAssignment.end - 1, text: `'${app}',\n`, }, ].filter(Boolean); } const binaryExpressions = (0, js_1.findNodes)(source, tsModule.SyntaxKind.BinaryExpression); const exportExpression = binaryExpressions.find((b) => { if (b.left.kind === tsModule.SyntaxKind.PropertyAccessExpression) { const pae = b.left; return (pae.expression.getText() === 'module' && pae.name.getText() === 'exports'); } }); if (exportExpression?.right.kind === tsModule.SyntaxKind.ObjectLiteralExpression) { const ole = exportExpression.right; return [ { type: devkit_1.ChangeType.Insert, index: ole.end - 1, text: `remotes: ['${app}']\n`, }, ]; } return []; } function addRemoteDefinition(source, app) { return [ { type: devkit_1.ChangeType.Insert, index: source.end, text: `\ndeclare module '${app}/Module';`, }, ]; } function addRemoteRoute(source, names) { const routes = (0, ast_utils_1.findElements)(source, 'Route'); const links = (0, ast_utils_1.findElements)(source, 'Link'); if (routes.length === 0) { return []; } else { const changes = []; const firstRoute = routes[0]; const firstLink = links[0]; changes.push(...(0, ast_utils_1.addImport)(source, `const ${names.className} = React.lazy(() => import('${names.fileName}/Module'));`)); changes.push({ type: devkit_1.ChangeType.Insert, index: firstRoute.end, text: `\n<Route path="/${names.fileName}" element={<${names.className} />} />`, }); if (firstLink) { const parentLi = (0, ast_utils_1.findClosestOpening)('li', firstLink); if (parentLi) { changes.push({ type: devkit_1.ChangeType.Insert, index: parentLi.end, text: `\n<li><Link to="/${names.fileName}">${names.className}</Link></li>`, }); } else { changes.push({ type: devkit_1.ChangeType.Insert, index: firstLink.parent.end, text: `\n<Link to="/${names.fileName}">${names.className}</Link>`, }); } } return changes; } }