Skip to content

Commit

Permalink
feat(nf): update for Angular 18
Browse files Browse the repository at this point in the history
  • Loading branch information
manfredsteyer committed Jun 4, 2024
1 parent c932c5e commit f0e8ea3
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 24 deletions.
1 change: 1 addition & 0 deletions libs/native-federation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ We will at least provide a new version of this package per Angular major. If nec
- Use version 16.2.x for Angular 16.2.x
- Use version 17.x for Angular 17.x
- Use version 17.1.x for Angular 17.1+
- Use version 18.x for Angular 18+

## Angular Integration

Expand Down
34 changes: 34 additions & 0 deletions libs/native-federation/docs/update18.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Update to Native Federation for Angular 18

The package ``@angular-architects/native-federation`` version 18 was successfully tested with Angular 18.

## Option 1

Just use ``ng update``:

```
ng update @angular-architects/native-federation
```

## Option 2

Use npm install:

```
npm i @angular-architects/native-federation@^18
```

Make sure you have the following ``postinstall`` script in your ``package.json``:

```json
"scripts": {
[...]
"postinstall": "node node_modules/@angular-architects/native-federation/src/patch-angular-build.js"
},
```

Run the ``postinstall`` script once manually for initialization:

```
npm run postinstall
```
13 changes: 13 additions & 0 deletions libs/native-federation/migration-collection.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"$schema": "../../node_modules/@angular-devkit/schematics/collection-schema.json",
"name": "native-federation",
"version": "0.0.1",
"schematics": {
"update18": {
"version": "18",
"factory": "./src/schematics/update18/schematic",
"schema": "./src/schematics/update18/schema.json",
"description": "migrating to v18"
}
}
}
5 changes: 4 additions & 1 deletion libs/native-federation/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@angular-architects/native-federation",
"version": "17.1.8",
"version": "18.0.1",
"main": "src/index.js",
"generators": "./collection.json",
"builders": "./builders.json",
Expand All @@ -15,6 +15,9 @@
"type": "git",
"url": "https://github.com/angular-architects/module-federation-plugin"
},
"ng-update": {
"migrations": "./migration-collection.json"
},
"dependencies": {
"@babel/core": "^7.19.0",
"@softarc/native-federation": "2.0.9",
Expand Down
5 changes: 5 additions & 0 deletions libs/native-federation/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
"glob": "builders.json",
"output": "."
},
{
"input": "./libs/native-federation",
"glob": "migration-collection.json",
"output": "."
},
{
"input": "./libs/native-federation",
"glob": "executors.json",
Expand Down
58 changes: 37 additions & 21 deletions libs/native-federation/src/builders/build/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import {
createBuilder,
} from '@angular-devkit/architect';

import { buildApplication } from '@angular-devkit/build-angular';
import { buildApplication, buildApplicationInternal } from '@angular/build/src/builders/application';
import { serveWithVite } from '@angular/build/src/builders/dev-server/vite-server';

import {
executeDevServerBuilder,
DevServerBuilderOptions,
} from '@angular-devkit/build-angular';
import { normalizeOptions } from '@angular-devkit/build-angular/src/builders/dev-server/options';
Expand Down Expand Up @@ -53,6 +53,19 @@ import { Connect } from 'vite';
import { PluginBuild } from 'esbuild';
import { FederationInfo } from '@softarc/native-federation-runtime';

function _buildApplication(options, context, pluginsOrExtensions) {
let extensions;
if (pluginsOrExtensions && Array.isArray(pluginsOrExtensions)) {
extensions = {
codePlugins: pluginsOrExtensions,
};
}
else {
extensions = pluginsOrExtensions;
}
return buildApplicationInternal(options, context, { write: false }, extensions);
}

export async function* runBuilder(
nfOptions: NfBuilderSchema,
context: BuilderContext
Expand Down Expand Up @@ -217,20 +230,23 @@ export async function* runBuilder(

// TODO: Clarify how DevServer needs to be executed. Not sure if its right.
// TODO: Clarify if buildApplication is needed `executeDevServerBuilder` seems to choose the correct DevServer

const appBuilderName = '@angular-devkit/build-angular:application';

const builderRun = nfOptions.dev
? executeDevServerBuilder(
options,
context,
{
indexHtml: nfOptions.skipHtmlTransform
? {}
: { indexHtml: transformIndexHtml(nfOptions) },
},
{
buildPlugins: plugins,
middleware,
}
)
? serveWithVite(
normOuterOptions,
appBuilderName,
_buildApplication,
context,
nfOptions.skipHtmlTransform
? {}
: { indexHtml: transformIndexHtml(nfOptions) },
{
buildPlugins: plugins,
middleware,
}
)
: buildApplication(options, context, plugins);

// builderRun.output.subscribe(async (output) => {
Expand Down Expand Up @@ -259,13 +275,13 @@ export async function* runBuilder(
updateIndexHtml(fedOptions, nfOptions);
}

if (first && runServer) {
startServer(nfOptions, fedOptions.outputPath, memResults);
}
// if (first && runServer) {
// startServer(nfOptions, fedOptions.outputPath, memResults);
// }

if (!first && runServer) {
reloadBrowser();
}
// if (!first && runServer) {
// reloadBrowser();
// }

if (!runServer) {
yield output;
Expand Down
4 changes: 4 additions & 0 deletions libs/native-federation/src/patch-angular-build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { patchAngularBuild } from './utils/patch-angular-build';

const workspaceRoot = process.cwd();
patchAngularBuild(workspaceRoot);
42 changes: 40 additions & 2 deletions libs/native-federation/src/schematics/init/schematic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
import { strings } from '@angular-devkit/core';
import { MfSchematicSchema } from './schema';

import { patchAngularBuildPackageJson } from '../../utils/patch-angular-build';

import {
addPackageJsonDependency,
NodeDependencyType,
Expand All @@ -32,6 +34,32 @@ type NormalizedOptions = {
port: number;
};

export function updatePackageJson(tree: Tree): void {
const packageJson = tree.readJson('package.json');

const scriptCall = 'node node_modules/@angular-architects/native-federation/src/patch-angular-build.js';

if (!packageJson['scripts']) {
packageJson['scripts'] = {};
}

let postInstall = (packageJson['scripts']?.['postinstall'] || '') as string;
if (postInstall?.includes(scriptCall)) {
return;
}

if (postInstall) {
postInstall += ' && ';
}

postInstall += scriptCall;

packageJson['scripts']['postinstall'] = postInstall;

tree.overwrite('package.json', JSON.stringify(packageJson, null, 2));

}

export default function config(options: MfSchematicSchema): Rule {
return async function (tree, context) {
const workspaceFileName = getWorkspaceFileName(tree);
Expand Down Expand Up @@ -71,6 +99,10 @@ export default function config(options: MfSchematicSchema): Rule {

updateWorkspaceConfig(tree, normalized, workspace, workspaceFileName);

updatePackageJson(tree);

patchAngularBuild(tree);

addPackageJsonDependency(tree, {
name: 'es-module-shims',
type: NodeDependencyType.Default,
Expand All @@ -84,6 +116,12 @@ export default function config(options: MfSchematicSchema): Rule {
};
}

export function patchAngularBuild(tree) {
const packageJson = JSON.parse(tree.read('node_modules/@angular/build/package.json'));
patchAngularBuildPackageJson(packageJson);
tree.overwrite('node_modules/@angular/build/package.json', JSON.stringify(packageJson, null, 2));
}

function updateWorkspaceConfig(
tree: Tree,
options: NormalizedOptions,
Expand Down Expand Up @@ -217,7 +255,7 @@ function normalizeOptions(
);

const manifestPath = path
.join(projectRoot, 'src/assets/federation.manifest.json')
.join(projectRoot, 'public/federation.manifest.json')
.replace(/\\/g, '/');

const main =
Expand Down Expand Up @@ -316,7 +354,7 @@ function makeMainAsync(
if (options.type === 'dynamic-host') {
newMainContent = `import { initFederation } from '@angular-architects/native-federation';
initFederation('/assets/federation.manifest.json')
initFederation('federation.manifest.json')
.catch(err => console.error(err))
.then(_ => import('./bootstrap'))
.catch(err => console.error(err));
Expand Down
8 changes: 8 additions & 0 deletions libs/native-federation/src/schematics/update18/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"$schema": "http://json-schema.org/schema",
"$id": "mf",
"title": "",
"type": "object",
"properties": {
}
}
10 changes: 10 additions & 0 deletions libs/native-federation/src/schematics/update18/schematic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Rule, Tree } from '@angular-devkit/schematics';

import { patchAngularBuild, updatePackageJson } from '../init/schematic';

export default function update18(): Rule {
return async function (tree: Tree) {
updatePackageJson(tree);
patchAngularBuild(tree);
};
}
30 changes: 30 additions & 0 deletions libs/native-federation/src/utils/patch-angular-build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import * as path from 'path';
import * as fs from 'fs';

export function patchAngularBuildPackageJson(packageJson: unknown): void {
const exportsMap = packageJson['exports'];

if (!exportsMap) {
console.log('No need to patch @angular/build/package.json')
return;
}

packageJson['_exports'] = exportsMap;
delete packageJson['exports'];

packageJson['types'] = './src/index.d.ts';
packageJson['main'] = './src/index.js';
packageJson['module'] = './src/index.js';
}

export function patchAngularBuild(workspaceRoot: string): void {
const packagePath = path.join(workspaceRoot, 'node_modules/@angular/build/package.json');

const packageJson = JSON.parse(fs.readFileSync(packagePath, 'utf-8'));

patchAngularBuildPackageJson(packageJson);

fs.writeFileSync(packagePath, JSON.stringify(packageJson, null, 2));

console.log('@angular/build/package.json patched');
}

0 comments on commit f0e8ea3

Please sign in to comment.