@@ -15,6 +15,7 @@ import {
15
15
ProjectGraph ,
16
16
readCachedProjectGraph ,
17
17
} from '@nx/devkit' ;
18
+ import { readCachedProjectConfiguration } from 'nx/src/project-graph/project-graph' ;
18
19
19
20
export function applyDefaultEagerPackages (
20
21
sharedConfig : Record < string , SharedLibraryConfig >
@@ -42,10 +43,35 @@ export const DEFAULT_ANGULAR_PACKAGES_TO_SHARE = [
42
43
'@angular/common' ,
43
44
] ;
44
45
46
+ export function getFunctionDeterminateRemoteUrl ( isServer : boolean = false ) {
47
+ const target = 'serve' ;
48
+ const remoteEntry = isServer ? 'server/remoteEntry.js' : 'remoteEntry.mjs' ;
49
+
50
+ return function ( remote : string ) {
51
+ const remoteConfiguration = readCachedProjectConfiguration ( remote ) ;
52
+ const serveTarget = remoteConfiguration ?. targets ?. [ target ] ;
53
+
54
+ if ( ! serveTarget ) {
55
+ throw new Error (
56
+ `Cannot automatically determine URL of remote (${ remote } ). Looked for property "host" in the project's "serve" target.\n
57
+ You can also use the tuple syntax in your webpack config to configure your remotes. e.g. \`remotes: [['remote1', 'http://localhost:4201']]\``
58
+ ) ;
59
+ }
60
+
61
+ const host = serveTarget . options ?. host ?? 'http://localhost' ;
62
+ const port = serveTarget . options ?. port ?? 4201 ;
63
+ return `${
64
+ host . endsWith ( '/' ) ? host . slice ( 0 , - 1 ) : host
65
+ } :${ port } /${ remoteEntry } `;
66
+ } ;
67
+ }
68
+
45
69
export async function getModuleFederationConfig (
46
70
mfConfig : ModuleFederationConfig ,
47
- determineRemoteUrl : ( remote : string ) => string ,
48
- options : { isServer : boolean } = { isServer : false }
71
+ options : {
72
+ isServer : boolean ;
73
+ determineRemoteUrl ?: ( remote : string ) => string ;
74
+ } = { isServer : false }
49
75
) {
50
76
let projectGraph : ProjectGraph ;
51
77
try {
@@ -107,11 +133,14 @@ export async function getModuleFederationConfig(
107
133
mfConfig . additionalShared ,
108
134
projectGraph
109
135
) ;
136
+ const determineRemoteUrlFn =
137
+ options . determineRemoteUrl ||
138
+ getFunctionDeterminateRemoteUrl ( options . isServer ) ;
110
139
111
140
const mapRemotesFunction = options . isServer ? mapRemotesForSSR : mapRemotes ;
112
141
const mappedRemotes =
113
142
! mfConfig . remotes || mfConfig . remotes . length === 0
114
143
? { }
115
- : mapRemotesFunction ( mfConfig . remotes , 'mjs' , determineRemoteUrl ) ;
144
+ : mapRemotesFunction ( mfConfig . remotes , 'mjs' , determineRemoteUrlFn ) ;
116
145
return { sharedLibraries, sharedDependencies, mappedRemotes } ;
117
146
}
1 commit comments
vercel[bot] commentedon Apr 27, 2023
Successfully deployed to the following URLs:
nx-dev – ./
nx-dev-nrwl.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx.dev
nx-five.vercel.app