-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(angular): add --ssr flag to remote generator (#13370)
- Loading branch information
Showing
15 changed files
with
438 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
182 changes: 182 additions & 0 deletions
182
packages/angular/src/generators/remote/__snapshots__/remote.spec.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
packages/angular/src/generators/remote/files/src/main.server.ts__tmpl__
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import 'zone.js/dist/zone-node'; | ||
|
||
import { APP_BASE_HREF } from '@angular/common'; | ||
import { ngExpressEngine } from '@nguniversal/express-engine'; | ||
import * as express from 'express'; | ||
import * as cors from 'cors'; | ||
import { existsSync } from 'fs'; | ||
import { join } from 'path'; | ||
|
||
import { AppServerModule } from './bootstrap.server'; | ||
|
||
// The Express app is exported so that it can be used by serverless Functions. | ||
export function app(): express.Express { | ||
const server = express(); | ||
const browserBundles = join(process.cwd(), 'dist/apps/<%= appName %>/browser'); | ||
const serverBundles = join(process.cwd(), 'dist/apps/<%= appName %>/server'); | ||
|
||
server.use(cors()); | ||
const indexHtml = existsSync(join(browserBundles, 'index.original.html')) | ||
? 'index.original.html' | ||
: 'index'; | ||
|
||
// Our Universal express-engine (found @ https://github.com/angular/universal/tree/main/modules/express-engine) | ||
server.engine( | ||
'html', | ||
ngExpressEngine({ | ||
bootstrap: AppServerModule, | ||
}) | ||
); | ||
|
||
server.set('view engine', 'html'); | ||
server.set('views', browserBundles); | ||
|
||
|
||
// Example Express Rest API endpoints | ||
// server.get('/api/**', (req, res) => { }); | ||
// Serve static files from /browser | ||
// serve static files | ||
server.use('/', express.static(browserBundles, { maxAge: '1y' })); | ||
server.use('/server', express.static(serverBundles, { maxAge: '1y' })); | ||
|
||
// All regular routes use the Universal engine | ||
server.get('*', (req, res) => { | ||
|
||
res.render(indexHtml, { | ||
req, | ||
providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }], | ||
}); | ||
}); | ||
|
||
return server; | ||
} | ||
|
||
function run(): void { | ||
const port = process.env['PORT'] || 4000; | ||
|
||
// Start up the Node server | ||
const server = app(); | ||
server.listen(port, () => { | ||
console.log(`Node Express server listening on http://localhost:${port}`); | ||
}); | ||
} | ||
|
||
run(); | ||
|
||
export * from './bootstrap.server'; |
3 changes: 3 additions & 0 deletions
3
packages/angular/src/generators/remote/files/webpack.server.config.js__tmpl__
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const { withModuleFederationForSSR } = require('@nrwl/angular/module-federation'); | ||
const config = require('./module-federation.config'); | ||
module.exports = withModuleFederationForSSR(config) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import type { Tree } from '@nrwl/devkit'; | ||
import { | ||
addDependenciesToPackageJson, | ||
generateFiles, | ||
joinPathFragments, | ||
readProjectConfiguration, | ||
updateProjectConfiguration, | ||
} from '@nrwl/devkit'; | ||
import type { Schema } from '../schema'; | ||
|
||
import setupSsr from '../../setup-ssr/setup-ssr'; | ||
import { | ||
corsVersion, | ||
moduleFederationNodeVersion, | ||
} from '../../../utils/versions'; | ||
|
||
export async function addSsr(tree: Tree, options: Schema, appName: string) { | ||
let project = readProjectConfiguration(tree, appName); | ||
|
||
await setupSsr(tree, { | ||
project: appName, | ||
}); | ||
|
||
tree.rename( | ||
joinPathFragments(project.sourceRoot, 'main.server.ts'), | ||
joinPathFragments(project.sourceRoot, 'bootstrap.server.ts') | ||
); | ||
tree.write( | ||
joinPathFragments(project.root, 'server.ts'), | ||
"import('./src/main.server');" | ||
); | ||
|
||
generateFiles(tree, joinPathFragments(__dirname, '../files'), project.root, { | ||
appName, | ||
tmpl: '', | ||
}); | ||
|
||
// update project.json | ||
project = readProjectConfiguration(tree, appName); | ||
|
||
project.targets.server.executor = '@nrwl/angular:webpack-server'; | ||
project.targets.server.options.customWebpackConfig = { | ||
path: joinPathFragments(project.root, 'webpack.server.config.js'), | ||
}; | ||
|
||
updateProjectConfiguration(tree, appName, project); | ||
|
||
const installTask = addDependenciesToPackageJson( | ||
tree, | ||
{ | ||
cors: corsVersion, | ||
'@module-federation/node': moduleFederationNodeVersion, | ||
}, | ||
{} | ||
); | ||
|
||
return installTask; | ||
} |
18 changes: 18 additions & 0 deletions
18
packages/angular/src/generators/remote/lib/find-next-available-port.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import type { Tree } from '@nrwl/devkit'; | ||
import { readProjectConfiguration } from '@nrwl/devkit'; | ||
import { getMFProjects } from '../../../utils/get-mf-projects'; | ||
|
||
export function findNextAvailablePort(tree: Tree) { | ||
const mfProjects = getMFProjects(tree); | ||
|
||
const ports = new Set<number>([4200]); | ||
for (const mfProject of mfProjects) { | ||
const { targets } = readProjectConfiguration(tree, mfProject); | ||
const port = targets?.serve?.options?.port ?? 4200; | ||
ports.add(port); | ||
} | ||
|
||
const nextAvailablePort = Math.max(...ports) + 1; | ||
|
||
return nextAvailablePort; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './find-next-available-port'; | ||
export * from './add-ssr'; |
Oops, something went wrong.
2471768
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
nx-dev – ./
nx-dev-nrwl.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx-five.vercel.app
nx.dev