|
| 1 | +import { readProjectConfiguration, type Tree } from '@nx/devkit'; |
| 2 | +import { createTreeWithEmptyWorkspace } from 'nx/src/devkit-testing-exports'; |
| 3 | +import hostGenerator from '../../generators/host/host'; |
| 4 | +import { Linter } from '@nx/eslint'; |
| 5 | +import updateSsrServerPort from './update-ssr-server-port'; |
| 6 | +describe('update-19-6-0 update-ssr-server-port migration', () => { |
| 7 | + let tree: Tree; |
| 8 | + |
| 9 | + beforeEach(() => { |
| 10 | + tree = createTreeWithEmptyWorkspace(); |
| 11 | + }); |
| 12 | + |
| 13 | + it('should update host and remote port server files', async () => { |
| 14 | + await hostGenerator(tree, { |
| 15 | + name: 'shell', |
| 16 | + e2eTestRunner: 'none', |
| 17 | + unitTestRunner: 'none', |
| 18 | + ssr: true, |
| 19 | + linter: Linter.EsLint, |
| 20 | + projectNameAndRootFormat: 'as-provided', |
| 21 | + style: 'css', |
| 22 | + remotes: ['product'], |
| 23 | + }); |
| 24 | + const remotePort = readProjectConfiguration(tree, 'product').targets.serve |
| 25 | + .options.port; |
| 26 | + |
| 27 | + const shellPort = readProjectConfiguration(tree, 'shell').targets.serve |
| 28 | + .options.port; |
| 29 | + |
| 30 | + // This should already exists in the generated project |
| 31 | + tree.write( |
| 32 | + 'product/server.ts', |
| 33 | + tree |
| 34 | + .read('product/server.ts', 'utf-8') |
| 35 | + .replace( |
| 36 | + 'const port = 4201;', |
| 37 | + `const port = process.env['PORT'] || 4200;` |
| 38 | + ) |
| 39 | + ); |
| 40 | + |
| 41 | + updateSsrServerPort(tree); |
| 42 | + expect(tree.read('product/server.ts', 'utf-8')).toContain( |
| 43 | + `port = process.env['PORT'] || ${remotePort}` |
| 44 | + ); |
| 45 | + expect(tree.read('product/server.ts', 'utf-8')).toMatchInlineSnapshot(` |
| 46 | + "import * as path from 'path'; |
| 47 | + import express from 'express'; |
| 48 | + import cors from 'cors'; |
| 49 | +
|
| 50 | + import { handleRequest } from './src/main.server'; |
| 51 | +
|
| 52 | + const port = process.env['PORT'] || 4201; |
| 53 | + const app = express(); |
| 54 | +
|
| 55 | + const browserDist = path.join(process.cwd(), 'dist/product/browser'); |
| 56 | + const serverDist = path.join(process.cwd(), 'dist/product/server'); |
| 57 | + const indexPath = path.join(browserDist, 'index.html'); |
| 58 | +
|
| 59 | + app.use(cors()); |
| 60 | +
|
| 61 | + // Client-side static bundles |
| 62 | + app.get( |
| 63 | + '*.*', |
| 64 | + express.static(browserDist, { |
| 65 | + maxAge: '1y', |
| 66 | + }) |
| 67 | + ); |
| 68 | +
|
| 69 | + // Static bundles for server-side module federation |
| 70 | + app.use( |
| 71 | + '/server', |
| 72 | + express.static(serverDist, { |
| 73 | + maxAge: '1y', |
| 74 | + }) |
| 75 | + ); |
| 76 | +
|
| 77 | + app.use('*', handleRequest(indexPath)); |
| 78 | +
|
| 79 | + const server = app.listen(port, () => { |
| 80 | + console.log(\`Express server listening on http://localhost:\${port}\`); |
| 81 | +
|
| 82 | + /** |
| 83 | + * DO NOT REMOVE IF USING @nx/react:module-federation-dev-ssr executor |
| 84 | + * to serve your Host application with this Remote application. |
| 85 | + * This message allows Nx to determine when the Remote is ready to be |
| 86 | + * consumed by the Host. |
| 87 | + */ |
| 88 | + process.send?.('nx.server.ready'); |
| 89 | + }); |
| 90 | +
|
| 91 | + server.on('error', console.error); |
| 92 | + " |
| 93 | + `); |
| 94 | + |
| 95 | + tree.write( |
| 96 | + 'shell/server.ts', |
| 97 | + tree |
| 98 | + .read('shell/server.ts', 'utf-8') |
| 99 | + .replace( |
| 100 | + 'const port = 4200;', |
| 101 | + `const port = process.env['PORT'] || 4200;` |
| 102 | + ) |
| 103 | + ); |
| 104 | + |
| 105 | + updateSsrServerPort(tree); |
| 106 | + expect(tree.read('shell/server.ts', 'utf-8')).toContain( |
| 107 | + `port = process.env.PORT || ${shellPort}` |
| 108 | + ); |
| 109 | + expect(tree.read('shell/server.ts', 'utf-8')).toMatchInlineSnapshot(` |
| 110 | + "import * as path from 'path'; |
| 111 | + import express from 'express'; |
| 112 | + import cors from 'cors'; |
| 113 | + import { handleRequest } from './src/main.server'; |
| 114 | + const port = process.env.PORT || 4200; |
| 115 | + const app = express(); |
| 116 | + const browserDist = path.join(process.cwd(), 'dist/shell/browser'); |
| 117 | + const indexPath = path.join(browserDist, 'index.html'); |
| 118 | + app.use(cors()); |
| 119 | + app.get('*.*', express.static(browserDist, { |
| 120 | + maxAge: '1y', |
| 121 | + })); |
| 122 | + app.use('*', handleRequest(indexPath)); |
| 123 | + const server = app.listen(port, () => { |
| 124 | + console.log(\`Express server listening on http://localhost:\${port}\`); |
| 125 | + }); |
| 126 | + server.on('error', console.error); |
| 127 | + " |
| 128 | + `); |
| 129 | + }); |
| 130 | + |
| 131 | + it('should update a host project server file', async () => { |
| 132 | + await hostGenerator(tree, { |
| 133 | + name: 'host', |
| 134 | + e2eTestRunner: 'none', |
| 135 | + unitTestRunner: 'none', |
| 136 | + ssr: true, |
| 137 | + linter: Linter.EsLint, |
| 138 | + projectNameAndRootFormat: 'as-provided', |
| 139 | + style: 'css', |
| 140 | + }); |
| 141 | + |
| 142 | + const hostPort = readProjectConfiguration(tree, 'host').targets.serve |
| 143 | + .options.port; |
| 144 | + |
| 145 | + tree.write( |
| 146 | + 'host/server.ts', |
| 147 | + tree |
| 148 | + .read('host/server.ts', 'utf-8') |
| 149 | + .replace( |
| 150 | + 'const port = 4200;', |
| 151 | + `const port = process.env['PORT'] || 4200;` |
| 152 | + ) |
| 153 | + ); |
| 154 | + |
| 155 | + updateSsrServerPort(tree); |
| 156 | + |
| 157 | + expect(tree.read('host/server.ts', 'utf-8')).toContain( |
| 158 | + `port = process.env.PORT || ${hostPort}` |
| 159 | + ); |
| 160 | + expect(tree.read('host/server.ts', 'utf-8')).toMatchInlineSnapshot(` |
| 161 | + "import * as path from 'path'; |
| 162 | + import express from 'express'; |
| 163 | + import cors from 'cors'; |
| 164 | + import { handleRequest } from './src/main.server'; |
| 165 | + const port = process.env.PORT || 4200; |
| 166 | + const app = express(); |
| 167 | + const browserDist = path.join(process.cwd(), 'dist/host/browser'); |
| 168 | + const indexPath = path.join(browserDist, 'index.html'); |
| 169 | + app.use(cors()); |
| 170 | + app.get('*.*', express.static(browserDist, { |
| 171 | + maxAge: '1y', |
| 172 | + })); |
| 173 | + app.use('*', handleRequest(indexPath)); |
| 174 | + const server = app.listen(port, () => { |
| 175 | + console.log(\`Express server listening on http://localhost:\${port}\`); |
| 176 | + }); |
| 177 | + server.on('error', console.error); |
| 178 | + " |
| 179 | + `); |
| 180 | + }); |
| 181 | + |
| 182 | + it('should not update a mfe project that is not ssr', async () => { |
| 183 | + await hostGenerator(tree, { |
| 184 | + name: 'shell-not-ssr', |
| 185 | + e2eTestRunner: 'none', |
| 186 | + unitTestRunner: 'none', |
| 187 | + ssr: false, |
| 188 | + linter: Linter.EsLint, |
| 189 | + projectNameAndRootFormat: 'as-provided', |
| 190 | + style: 'css', |
| 191 | + }); |
| 192 | + |
| 193 | + tree.write('shell-not-ssr/server.ts', 'const port = 9999;'); |
| 194 | + const shellPort = readProjectConfiguration(tree, 'shell-not-ssr').targets |
| 195 | + .serve.options.port; |
| 196 | + |
| 197 | + updateSsrServerPort(tree); |
| 198 | + |
| 199 | + expect(tree.read('shell-not-ssr/server.ts', 'utf-8')).not.toContain( |
| 200 | + `port = ${shellPort}` |
| 201 | + ); |
| 202 | + expect(tree.read('shell-not-ssr/server.ts', 'utf-8')).toMatchInlineSnapshot( |
| 203 | + `"const port = 9999;"` |
| 204 | + ); |
| 205 | + }); |
| 206 | +}); |
0 commit comments