-
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(react): add SSR suppoprt to host generator for module federation (…
- Loading branch information
Showing
26 changed files
with
499 additions
and
62 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
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
13 changes: 13 additions & 0 deletions
13
...rc/generators/host/files/module-federation-ssr/module-federation.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,13 @@ | ||
// @ts-check | ||
|
||
/** | ||
* @type {import('@nrwl/devkit').ModuleFederationConfig} | ||
**/ | ||
const moduleFederationConfig = { | ||
name: '<%= projectName %>', | ||
remotes: [ | ||
<% remotes.forEach(function(r) {%> "<%= r.fileName %>", <% }); %> | ||
], | ||
}; | ||
|
||
module.exports = moduleFederationConfig; |
8 changes: 8 additions & 0 deletions
8
...es/react/src/generators/host/files/module-federation-ssr/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,8 @@ | ||
const { withModuleFederationForSSR } = require('@nrwl/react/module-federation'); | ||
const baseConfig = require("./module-federation.server.config"); | ||
|
||
const defaultConfig = { | ||
...baseConfig | ||
}; | ||
|
||
module.exports = withModuleFederationForSSR(defaultConfig); |
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,60 @@ | ||
import type { Tree } from '@nrwl/devkit'; | ||
import { readJson } from '@nrwl/devkit'; | ||
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; | ||
import hostGenerator from './host'; | ||
import { Linter } from '@nrwl/linter'; | ||
|
||
describe('hostGenerator', () => { | ||
let tree: Tree; | ||
|
||
beforeEach(() => { | ||
tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' }); | ||
}); | ||
|
||
it('should generate host files and configs', async () => { | ||
await hostGenerator(tree, { | ||
name: 'test', | ||
style: 'css', | ||
linter: Linter.None, | ||
unitTestRunner: 'none', | ||
e2eTestRunner: 'none', | ||
}); | ||
|
||
expect(tree.exists('apps/test/tsconfig.json')); | ||
expect(tree.exists('apps/test/webpack.config.prod.js')); | ||
expect(tree.exists('apps/test/webpack.config.js')); | ||
expect(tree.exists('apps/test/src/bootstrap.tsx')); | ||
expect(tree.exists('apps/test/src/main.ts')); | ||
expect(tree.exists('apps/test/src/remotes.d.ts')); | ||
}); | ||
|
||
it('should generate host files and configs for SSR', async () => { | ||
await hostGenerator(tree, { | ||
name: 'test', | ||
ssr: true, | ||
style: 'css', | ||
linter: Linter.None, | ||
unitTestRunner: 'none', | ||
e2eTestRunner: 'none', | ||
}); | ||
|
||
expect(tree.exists('apps/test/tsconfig.json')); | ||
expect(tree.exists('apps/test/webpack.config.prod.js')); | ||
expect(tree.exists('apps/test/webpack.config.server.js')); | ||
expect(tree.exists('apps/test/webpack.config.js')); | ||
expect(tree.exists('apps/test/src/main.server.tsx')); | ||
expect(tree.exists('apps/test/src/bootstrap.tsx')); | ||
expect(tree.exists('apps/test/src/main.ts')); | ||
expect(tree.exists('apps/test/src/remotes.d.ts')); | ||
|
||
expect(readJson(tree, 'apps/test/tsconfig.server.json')).toEqual({ | ||
compilerOptions: { | ||
outDir: '../../out-tsc/server', | ||
target: 'es2019', | ||
types: ['node'], | ||
}, | ||
extends: './tsconfig.app.json', | ||
include: ['src/main.server.tsx', 'server.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
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
52 changes: 52 additions & 0 deletions
52
packages/react/src/generators/host/lib/setup-ssr-for-host.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,52 @@ | ||
import type { GeneratorCallback, Tree } from '@nrwl/devkit'; | ||
import { | ||
addDependenciesToPackageJson, | ||
generateFiles, | ||
joinPathFragments, | ||
names, | ||
readProjectConfiguration, | ||
} from '@nrwl/devkit'; | ||
import { runTasksInSerial } from '@nrwl/workspace/src/utilities/run-tasks-in-serial'; | ||
|
||
import type { Schema } from '../schema'; | ||
import { moduleFederationNodeVersion } from '../../../utils/versions'; | ||
import { normalizeProjectName } from '../../application/lib/normalize-options'; | ||
|
||
export async function setupSsrForHost( | ||
tree: Tree, | ||
options: Schema, | ||
appName: string, | ||
defaultRemoteManifest: { name: string; port: number }[] | ||
) { | ||
const tasks: GeneratorCallback[] = []; | ||
const project = readProjectConfiguration(tree, appName); | ||
|
||
generateFiles( | ||
tree, | ||
joinPathFragments(__dirname, '../files/module-federation-ssr'), | ||
project.root, | ||
{ | ||
...options, | ||
remotes: defaultRemoteManifest.map(({ name, port }) => { | ||
const remote = normalizeProjectName({ ...options, name }); | ||
return { | ||
...names(remote), | ||
port, | ||
}; | ||
}), | ||
appName, | ||
tmpl: '', | ||
} | ||
); | ||
|
||
const installTask = addDependenciesToPackageJson( | ||
tree, | ||
{ | ||
'@module-federation/node': moduleFederationNodeVersion, | ||
}, | ||
{} | ||
); | ||
tasks.push(installTask); | ||
|
||
return runTasksInSerial(...tasks); | ||
} |
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
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
13 changes: 13 additions & 0 deletions
13
.../generators/remote/files/module-federation-ssr/module-federation.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,13 @@ | ||
// @ts-check | ||
|
||
/** | ||
* @type {import('@nrwl/devkit').ModuleFederationConfig} | ||
**/ | ||
const moduleFederationConfig = { | ||
name: '<%= projectName %>', | ||
exposes: { | ||
'./Module': '<%= appProjectRoot %>/src/remote-entry.ts', | ||
}, | ||
}; | ||
|
||
module.exports = moduleFederationConfig; |
8 changes: 8 additions & 0 deletions
8
.../react/src/generators/remote/files/module-federation-ssr/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,8 @@ | ||
const { withModuleFederationForSSR } = require('@nrwl/react/module-federation'); | ||
const baseConfig = require("./module-federation.server.config"); | ||
|
||
const defaultConfig = { | ||
...baseConfig | ||
}; | ||
|
||
module.exports = withModuleFederationForSSR(defaultConfig); |
Oops, something went wrong.
6c59cbb
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