Skip to content

Commit 0cfc676

Browse files
committed
feature: ng add uses nx builders for nx workspaces
1 parent 606cec2 commit 0cfc676

File tree

8 files changed

+77
-15
lines changed

8 files changed

+77
-15
lines changed

.vscode/settings.json

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,22 @@
66
"markdown",
77
"latex",
88
"plaintext"
9-
]
9+
],
10+
"workbench.colorCustomizations": {
11+
"activityBar.activeBackground": "#1f6fd0",
12+
"activityBar.activeBorder": "#ee90bb",
13+
"activityBar.background": "#1f6fd0",
14+
"activityBar.foreground": "#e7e7e7",
15+
"activityBar.inactiveForeground": "#e7e7e799",
16+
"activityBarBadge.background": "#ee90bb",
17+
"activityBarBadge.foreground": "#15202b",
18+
"statusBar.background": "#1857a4",
19+
"statusBar.foreground": "#e7e7e7",
20+
"statusBarItem.hoverBackground": "#1f6fd0",
21+
"titleBar.activeBackground": "#1857a4",
22+
"titleBar.activeForeground": "#e7e7e7",
23+
"titleBar.inactiveBackground": "#1857a499",
24+
"titleBar.inactiveForeground": "#e7e7e799"
25+
},
26+
"peacock.color": "#1857a4"
1027
}

libs/mf-runtime/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
"name": "@angular-architects/module-federation-runtime",
33

44
"license": "MIT",
5-
"version": "12.4.0",
5+
"version": "12.5.0",
66
"peerDependencies": {
7-
"@angular/common": ">=12.4.0",
8-
"@angular/core": ">=12.4.0"
7+
"@angular/common": ">=12.0.0",
8+
"@angular/core": ">=12.0.0"
99
},
1010
"dependencies": {
1111
"tslib": "^2.0.0"

libs/mf-tools/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@angular-architects/module-federation-tools",
3-
"version": "12.4.0",
3+
"version": "12.5.0",
44
"license": "MIT",
55
"peerDependencies": {
66
"@angular/common": ">=11.0.0",

libs/mf/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,10 @@ The options passed to shareAll are applied to all dependencies found in your ``p
278278

279279
This might come in handy in an mono repo scenario and when doing some experiments/ trouble shooting.
280280

281+
### Nx Integration
282+
283+
If the plugin detects that you are using Nx (it basically looks for a ``nx.json``), it uses the builders provided by Nx.
284+
281285
### Angular Universal (Server Side Rendering)
282286

283287
Since Version 12.4.0 of this plugin, we support the new _jsdom_-based Angular Universal API for Server Side Rendering (SSR). Please note that SSR *only* makes sense in specific scenarios, e. g. for customer-facing apps that need SEO.
@@ -354,6 +358,8 @@ Please find an [example](https://github.com/manfredsteyer/module-federation-plug
354358

355359
To try it out, you can checkout the ``main`` branch of our [example](https://github.com/manfredsteyer/module-federation-plugin-example). After installing the dependencies (``npm i``), you can repeat the steps for adding Angular Universal to an existing Module Federation project described above twice: Once for the _project shell and the port 5000_ and one more time for the _project mfe1 and port 3000_.
356360

361+
Please find a [brain dump](https://github.com/angular-architects/module-federation-plugin/blob/main/libs/mf/tutorial/braindump-ssr.md) for this [here](https://github.com/angular-architects/module-federation-plugin/blob/main/libs/mf/tutorial/braindump-ssr.md).
362+
357363
### Pitfalls when sharing libraries of a Monorepo
358364

359365
#### Warning: No required version specified

libs/mf/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@angular-architects/module-federation",
3-
"version": "12.4.0",
3+
"version": "12.5.0",
44
"license": "MIT",
55
"repository": {
66
"type": "GitHub",
@@ -18,7 +18,7 @@
1818
"builders": "./builders.json",
1919
"dependencies": {
2020
"ngx-build-plus": "^12.2.0",
21-
"@angular-architects/module-federation-runtime": "^12.4.0",
21+
"@angular-architects/module-federation-runtime": "^12.5.0",
2222
"word-wrap": "^1.2.3",
2323
"callsite": "^1.0.0",
2424
"node-fetch": "^2.6.1"

libs/mf/src/schematics/mf/schema.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export interface MfSchematicSchema {
22
project: string;
33
port: string;
4+
nxBuilders: boolean | undefined
45
}

libs/mf/src/schematics/mf/schema.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
"$source": "argv",
2222
"index": 1
2323
}
24+
},
25+
"nxBuilders": {
26+
"type": "boolean",
27+
"description": "Use builders provided by Nx instead of ngx-build-plus? Defaults to true for Nx workspaces and false for CLI workspaces.",
2428
}
2529
},
2630
"required": [

libs/mf/src/schematics/mf/schematic.ts

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,14 @@ function updatePackageJson(tree: Tree): void {
133133
tree.overwrite('package.json', JSON.stringify(packageJson, null, 2));
134134
}
135135

136+
function getWebpackConfigValue(nx: boolean, path: string) {
137+
if (!nx) {
138+
return path;
139+
}
140+
141+
return { path };
142+
}
143+
136144
export default function config (options: MfSchematicSchema): Rule {
137145

138146
return async function (tree) {
@@ -183,6 +191,18 @@ export default function config (options: MfSchematicSchema): Rule {
183191
tree.create(configPath, webpackConfig);
184192
tree.create(configProdPath, prodConfig);
185193

194+
if (typeof options.nxBuilders === 'undefined') {
195+
options.nxBuilders = tree.exists('nx.json');
196+
}
197+
198+
if (options.nxBuilders) {
199+
console.log('Using Nx builders!');
200+
}
201+
202+
const webpackProperty = options.nxBuilders ? 'customWebpackConfig' : 'extraWebpackConfig';
203+
const buildBuilder = options.nxBuilders ? '@nrwl/angular:webpack-browser' : 'ngx-build-plus:browser';
204+
const serveBuilder = options.nxBuilders ? '@nrwl/angular:webpack-server' : 'ngx-build-plus:dev-server';
205+
186206
if (!projectConfig?.architect?.build ||
187207
!projectConfig?.architect?.serve) {
188208
throw new Error(`The project doen't have a build or serve target in angular.json!`);
@@ -196,18 +216,31 @@ export default function config (options: MfSchematicSchema): Rule {
196216
projectConfig.architect.serve.options = {};
197217
}
198218

199-
projectConfig.architect.build.options.extraWebpackConfig = configPath;
219+
projectConfig.architect.build.builder = buildBuilder;
220+
projectConfig.architect.build.options[webpackProperty] = getWebpackConfigValue(options.nxBuilders, configPath);
200221
projectConfig.architect.build.options.commonChunk = false;
201-
projectConfig.architect.build.configurations.production.extraWebpackConfig = configProdPath;
202-
projectConfig.architect.serve.options.extraWebpackConfig = configPath;
203-
projectConfig.architect.serve.options.port = port;
204-
projectConfig.architect.serve.configurations.production.extraWebpackConfig = configProdPath;
222+
projectConfig.architect.build.configurations.production[webpackProperty] = getWebpackConfigValue(options.nxBuilders, configProdPath);
205223

206-
if (projectConfig?.architect?.test?.options) {
207-
projectConfig.architect.test.options.extraWebpackConfig = configPath;
224+
projectConfig.architect.serve.builder = serveBuilder;
225+
projectConfig.architect.serve.options.port = port;
226+
227+
// Only needed for ngx-build-plus
228+
if (!options.nxBuilders) {
229+
projectConfig.architect.serve.options[webpackProperty] = getWebpackConfigValue(options.nxBuilders, configPath);
230+
projectConfig.architect.serve.configurations.production[webpackProperty] = getWebpackConfigValue(options.nxBuilders, configProdPath);
208231
}
232+
233+
// We don't change the config for testing anymore to prevent
234+
// issues with eager bundles and webpack
235+
// Consequence:
236+
// Remotes: No issue
237+
// Hosts: Should be tested using an E2E test
238+
// if (projectConfig?.architect?.test?.options) {
239+
// projectConfig.architect.test.options.extraWebpackConfig = configPath;
240+
// }
209241

210242
if (projectConfig?.architect?.['extract-i18n']?.options) {
243+
projectConfig.architect['extract-i18n'].builder = 'ngx-build-plus:extract-i18n';
211244
projectConfig.architect['extract-i18n'].options.extraWebpackConfig = configPath;
212245
}
213246

@@ -220,12 +253,13 @@ export default function config (options: MfSchematicSchema): Rule {
220253
return chain([
221254
makeMainAsync(main),
222255
adjustSSR(projectSourceRoot, ssrMappings),
223-
externalSchematic('ngx-build-plus', 'ng-add', { project: options.project }),
256+
// externalSchematic('ngx-build-plus', 'ng-add', { project: options.project }),
224257
]);
225258

226259
}
227260
}
228261

262+
229263
function generateRemoteConfig(workspace: any, projectName: string) {
230264
let remotes = '';
231265
for (const p in workspace.projects) {

0 commit comments

Comments
 (0)