-
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 withModuleFederationForSSR function (#13276)
- Loading branch information
Showing
5 changed files
with
138 additions
and
53 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
import { withModuleFederation } from './src/module-federation/with-module-federation'; | ||
import { withModuleFederationForSSR } from './src/module-federation/with-module-federation-ssr'; | ||
|
||
export { withModuleFederation }; | ||
export { withModuleFederationForSSR }; | ||
|
||
// Support for older generated code: `const withModuleFederation = require('@nrwl/react/module-federation')` | ||
module.exports = withModuleFederation; | ||
|
||
// Allow newer generated code to work: `const { withModuleFederation } = require(...)`; | ||
module.exports.withModuleFederation = withModuleFederation; | ||
module.exports.withModuleFederationForSSR = withModuleFederationForSSR; |
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,65 @@ | ||
import { | ||
applyAdditionalShared, | ||
applySharedFunction, | ||
createProjectGraphAsync, | ||
getDependentPackagesForProject, | ||
mapRemotes, | ||
mapRemotesForSSR, | ||
ModuleFederationConfig, | ||
ProjectConfiguration, | ||
ProjectGraph, | ||
readCachedProjectGraph, | ||
sharePackages, | ||
shareWorkspaceLibraries, | ||
} from '@nrwl/devkit'; | ||
|
||
export async function getModuleFederationConfig( | ||
mfConfig: ModuleFederationConfig, | ||
determineRemoteUrl: (remote: string) => string, | ||
options: { isServer: boolean } = { isServer: false } | ||
) { | ||
let projectGraph: ProjectGraph<ProjectConfiguration>; | ||
try { | ||
projectGraph = readCachedProjectGraph(); | ||
} catch (e) { | ||
projectGraph = await createProjectGraphAsync(); | ||
} | ||
|
||
const project = projectGraph.nodes[mfConfig.name]?.data; | ||
|
||
if (!project) { | ||
throw Error( | ||
`Cannot find project "${mfConfig.name}". Check that the name is correct in module-federation.config.js` | ||
); | ||
} | ||
|
||
const dependencies = getDependentPackagesForProject( | ||
projectGraph, | ||
mfConfig.name | ||
); | ||
const sharedLibraries = shareWorkspaceLibraries( | ||
dependencies.workspaceLibraries | ||
); | ||
|
||
const npmPackages = sharePackages(dependencies.npmPackages); | ||
|
||
const sharedDependencies = { | ||
...sharedLibraries.getLibraries(), | ||
...npmPackages, | ||
}; | ||
|
||
applySharedFunction(sharedDependencies, mfConfig.shared); | ||
applyAdditionalShared( | ||
sharedDependencies, | ||
mfConfig.additionalShared, | ||
projectGraph | ||
); | ||
|
||
const mapRemotesFunction = options.isServer ? mapRemotesForSSR : mapRemotes; | ||
const mappedRemotes = | ||
!mfConfig.remotes || mfConfig.remotes.length === 0 | ||
? {} | ||
: mapRemotesFunction(mfConfig.remotes, 'js', determineRemoteUrl); | ||
|
||
return { sharedLibraries, sharedDependencies, mappedRemotes }; | ||
} |
64 changes: 64 additions & 0 deletions
64
packages/react/src/module-federation/with-module-federation-ssr.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,64 @@ | ||
import { ModuleFederationConfig } from '@nrwl/devkit'; | ||
import { readCachedProjectConfiguration } from 'nx/src/project-graph/project-graph'; | ||
import { getModuleFederationConfig } from './utils'; | ||
|
||
function determineRemoteUrl(remote: string) { | ||
const remoteConfiguration = readCachedProjectConfiguration(remote); | ||
const serveTarget = remoteConfiguration?.targets?.serve; | ||
|
||
if (!serveTarget) { | ||
throw new Error( | ||
`Cannot automatically determine URL of remote (${remote}). Looked for property "host" in the project's "serve" target.\n | ||
You can also use the tuple syntax in your webpack config to configure your remotes. e.g. \`remotes: [['remote1', '//localhost:4201']]\`` | ||
); | ||
} | ||
|
||
const host = serveTarget.options?.host ?? '//localhost'; | ||
const port = serveTarget.options?.port ?? 4201; | ||
return `${ | ||
host.endsWith('/') ? host.slice(0, -1) : host | ||
}:${port}/server/remoteEntry.js`; | ||
} | ||
|
||
export async function withModuleFederationForSSR( | ||
options: ModuleFederationConfig | ||
) { | ||
const reactWebpackConfig = require('../../plugins/webpack'); | ||
|
||
const { sharedLibraries, sharedDependencies, mappedRemotes } = | ||
await getModuleFederationConfig(options, determineRemoteUrl, { | ||
isServer: true, | ||
}); | ||
|
||
return (config) => { | ||
config = reactWebpackConfig(config); | ||
|
||
config.target = false; | ||
config.output.uniqueName = options.name; | ||
config.optimization = { | ||
runtimeChunk: false, | ||
}; | ||
|
||
config.plugins.push( | ||
new (require('@module-federation/node').UniversalFederationPlugin)( | ||
{ | ||
name: options.name, | ||
filename: 'remoteEntry.js', | ||
exposes: options.exposes, | ||
remotes: mappedRemotes, | ||
shared: { | ||
...sharedDependencies, | ||
}, | ||
library: { | ||
type: 'commonjs-module', | ||
}, | ||
isServer: true, | ||
}, | ||
{} | ||
), | ||
sharedLibraries.getReplacementPlugin() | ||
); | ||
|
||
return 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
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