Skip to content

Commit

Permalink
fix(angular): support module syntax for mfes (nrwl#8020)
Browse files Browse the repository at this point in the history
* fix(angular): support module syntax for mfes

* fix(angular): add publich host to prevent infinte reload
  • Loading branch information
Coly010 authored Dec 9, 2021
1 parent 366c2f0 commit 6e40032
Show file tree
Hide file tree
Showing 9 changed files with 932 additions and 9 deletions.
6 changes: 6 additions & 0 deletions packages/angular/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@
"version": "13.2.0",
"description": "In Angular version 13, the `teardown` flag in `TestBed` will be enabled by default. This migration automatically opts out existing apps from the new teardown behavior.",
"factory": "./src/migrations/update-13-2-0/opt-out-testbed-teardown"
},
"update-mfe-config-to-module-syntax": {
"cli": "nx",
"version": "13.3.0-beta.0",
"description": "In Angular version 13, the ESM became a first class citizen. This means the webpack config generated must be modified to support modules.",
"factory": "./src/migrations/update-13-3-0/update-mfe-webpack-config"
}
},
"packageJsonUpdates": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ module.exports = {
runtimeChunk: false,
minimize: false,
},
experiments: {
outputModule: true
},
resolve: {
alias: {
...sharedMappings.getAliases(),
Expand All @@ -40,8 +43,8 @@ module.exports = {
plugins: [
new ModuleFederationPlugin({
remotes: {
\\"remote1\\": 'remote1@http://localhost:4201/remoteEntry.js',
\\"remote2\\": 'remote2@http://localhost:4202/remoteEntry.js',
\\"remote1\\": 'http://localhost:4201/remoteEntry.js',
\\"remote2\\": 'http://localhost:4202/remoteEntry.js',
},
shared: {
Expand All @@ -51,6 +54,9 @@ module.exports = {
\\"@angular/router\\": { singleton: true, strictVersion: true },
...sharedMappings.getDescriptors(),
},
library: {
type: 'module'
},
}),
sharedMappings.getPlugin(),
],
Expand Down Expand Up @@ -90,6 +96,9 @@ module.exports = {
runtimeChunk: false,
minimize: false,
},
experiments: {
outputModule: true
},
resolve: {
alias: {
...sharedMappings.getAliases(),
Expand All @@ -98,7 +107,7 @@ module.exports = {
plugins: [
new ModuleFederationPlugin({
remotes: {
\\"remote1\\": 'remote1@http://localhost:4200/remoteEntry.js',
\\"remote1\\": 'http://localhost:4200/remoteEntry.js',
},
shared: {
Expand All @@ -108,6 +117,9 @@ module.exports = {
\\"@angular/router\\": { singleton: true, strictVersion: true },
...sharedMappings.getDescriptors(),
},
library: {
type: 'module'
},
}),
sharedMappings.getPlugin(),
],
Expand Down Expand Up @@ -147,6 +159,9 @@ module.exports = {
runtimeChunk: false,
minimize: false,
},
experiments: {
outputModule: true
},
resolve: {
alias: {
...sharedMappings.getAliases(),
Expand All @@ -164,6 +179,9 @@ module.exports = {
\\"@angular/router\\": { singleton: true, strictVersion: true },
...sharedMappings.getDescriptors(),
},
library: {
type: 'module'
},
}),
sharedMappings.getPlugin(),
],
Expand Down Expand Up @@ -203,6 +221,9 @@ module.exports = {
runtimeChunk: false,
minimize: false,
},
experiments: {
outputModule: true
},
resolve: {
alias: {
...sharedMappings.getAliases(),
Expand All @@ -222,6 +243,9 @@ module.exports = {
\\"@angular/router\\": { singleton: true, strictVersion: true },
...sharedMappings.getDescriptors(),
},
library: {
type: 'module'
},
}),
sharedMappings.getPlugin(),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ module.exports = {
runtimeChunk: false,
minimize: false,
},
experiments: {
outputModule: true
},
resolve: {
alias: {
...sharedMappings.getAliases(),
Expand All @@ -70,8 +73,8 @@ module.exports = {
plugins: [
new ModuleFederationPlugin({
remotes: {
\\"remote1\\": 'remote1@http://localhost:4201/remoteEntry.js',
\\"remote2\\": 'remote2@http://localhost:4202/remoteEntry.js',
\\"remote1\\": 'http://localhost:4201/remoteEntry.js',
\\"remote2\\": 'http://localhost:4202/remoteEntry.js',
},
shared: {
Expand All @@ -81,6 +84,9 @@ module.exports = {
\\"@angular/router\\": { singleton: true, strictVersion: true },
...sharedMappings.getDescriptors(),
},
library: {
type: 'module'
},
}),
sharedMappings.getPlugin(),
],
Expand Down Expand Up @@ -120,6 +126,9 @@ module.exports = {
runtimeChunk: false,
minimize: false,
},
experiments: {
outputModule: true
},
resolve: {
alias: {
...sharedMappings.getAliases(),
Expand All @@ -128,7 +137,7 @@ module.exports = {
plugins: [
new ModuleFederationPlugin({
remotes: {
\\"remote1\\": 'remote1@http://localhost:4200/remoteEntry.js',
\\"remote1\\": 'http://localhost:4200/remoteEntry.js',
},
shared: {
Expand All @@ -138,6 +147,9 @@ module.exports = {
\\"@angular/router\\": { singleton: true, strictVersion: true },
...sharedMappings.getDescriptors(),
},
library: {
type: 'module'
},
}),
sharedMappings.getPlugin(),
],
Expand Down Expand Up @@ -177,6 +189,9 @@ module.exports = {
runtimeChunk: false,
minimize: false,
},
experiments: {
outputModule: true
},
resolve: {
alias: {
...sharedMappings.getAliases(),
Expand All @@ -194,6 +209,9 @@ module.exports = {
\\"@angular/router\\": { singleton: true, strictVersion: true },
...sharedMappings.getDescriptors(),
},
library: {
type: 'module'
},
}),
sharedMappings.getPlugin(),
],
Expand Down Expand Up @@ -233,6 +251,9 @@ module.exports = {
runtimeChunk: false,
minimize: false,
},
experiments: {
outputModule: true
},
resolve: {
alias: {
...sharedMappings.getAliases(),
Expand All @@ -252,6 +273,9 @@ module.exports = {
\\"@angular/router\\": { singleton: true, strictVersion: true },
...sharedMappings.getDescriptors(),
},
library: {
type: 'module'
},
}),
sharedMappings.getPlugin(),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ module.exports = {
runtimeChunk: false,
minimize: false,
},
experiments: {
outputModule: true
},
resolve: {
alias: {
...sharedMappings.getAliases(),
Expand All @@ -42,12 +45,15 @@ module.exports = {
'./Module': '<%= sourceRoot %>/src/app/remote-entry/entry.module.ts',
},<% } %><% if(type === 'host') { %>
remotes: {
<% remotes.forEach(function(remote) { %>"<%= remote.remoteName %>": "<%= remote.remoteName %>@http://localhost:<%= remote.port %>/remoteEntry.js",<% }); %>
<% remotes.forEach(function(remote) { %>"<%= remote.remoteName %>": "http://localhost:<%= remote.port %>/remoteEntry.js",<% }); %>
},<% } %>
shared: {<% sharedLibraries.forEach(function (lib) { %>
"<%= lib %>": { singleton: true, strictVersion: true },<% }); %>
...sharedMappings.getDescriptors(),
},
library: {
type: 'module'
},
}),
sharedMappings.getPlugin(),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function addRemoteToHost(host: Tree, options: Schema) {
const endOfPropertiesPos = mfRemotesNode.properties.end;

const updatedConfig = `${hostWebpackConfig.slice(0, endOfPropertiesPos)}
\t\t"${options.appName}": '${options.appName}@http://localhost:${
\t\t"${options.appName}": 'http://localhost:${
options.port ?? 4200
}/remoteEntry.js',${hostWebpackConfig.slice(endOfPropertiesPos)}`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ export function setupServeTarget(host: Tree, options: Schema) {
options: {
...appConfig.targets['serve'].options,
port: options.port ?? undefined,
publicHost: options.host
? undefined
: `http://localhost:${options.port ?? 4200}`,
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ describe('Init MFE', () => {
const webpackContents = host.read(`apps/app1/webpack.config.js`, 'utf-8');

expect(webpackContents).toContain(
'"remote1": "remote1@http://localhost:4200/remoteEntry.js"'
'"remote1": "http://localhost:4200/remoteEntry.js"'
);
});
it('should update the implicit dependencies of the host when --remotes flag supplied', async () => {
Expand Down
Loading

0 comments on commit 6e40032

Please sign in to comment.