Skip to content

Commit 4b88da8

Browse files
committed
feat(nf): prepare to be called via ssr
1 parent 7a1e828 commit 4b88da8

File tree

6 files changed

+72
-36
lines changed

6 files changed

+72
-36
lines changed

libs/native-federation/src/builders/build/builder.ts

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ import {
3333
import { RebuildHubs } from '../../utils/rebuild-events';
3434
import { updateIndexHtml } from '../../utils/updateIndexHtml';
3535
import { appendFileSync, existsSync, mkdirSync } from 'fs';
36-
import { EsBuildResult, MemResults } from '../../utils/mem-resuts';
36+
import {
37+
EsBuildResult,
38+
MemResults,
39+
NgCliAssetResult,
40+
} from '../../utils/mem-resuts';
3741
import { JsonObject } from '@angular-devkit/core';
3842

3943
function log(...args) {
@@ -42,10 +46,10 @@ function log(...args) {
4246
console.log(args);
4347
}
4448

45-
export async function runBuilder(
49+
export async function* runBuilder(
4650
nfOptions: NfBuilderSchema,
4751
context: BuilderContext
48-
): Promise<BuilderOutput> {
52+
): AsyncIterable<BuilderOutput> {
4953
const target = targetFromTargetString(nfOptions.target);
5054
const _options = (await context.getTargetOptions(
5155
target
@@ -57,15 +61,18 @@ export async function runBuilder(
5761
builder
5862
)) as JsonObject & Schema;
5963

64+
const runServer = !!nfOptions.port;
65+
const write = !runServer;
66+
const watch = !!runServer || nfOptions.watch;
67+
68+
options.watch = watch;
6069
const rebuildEvents = new RebuildHubs();
6170

6271
const adapter = createAngularBuildAdapter(options, context, rebuildEvents);
6372
setBuildAdapter(adapter);
6473

6574
setLogLevel(options.verbose ? 'verbose' : 'info');
6675

67-
options.watch = !!nfOptions.dev;
68-
6976
const fedOptions: FederationOptions = {
7077
workspaceRoot: context.workspaceRoot,
7178
outputPath: options.outputPath,
@@ -97,12 +104,12 @@ export async function runBuilder(
97104
// );
98105

99106
const memResults = new MemResults();
100-
const write = !nfOptions.dev;
107+
101108
let first = true;
102109
let lastResult: { success: boolean } | undefined;
103110

104111
if (!existsSync(options.outputPath)) {
105-
mkdirSync(options.outputPath);
112+
mkdirSync(options.outputPath, { recursive: true });
106113
}
107114

108115
if (!write) {
@@ -116,6 +123,7 @@ export async function runBuilder(
116123
write,
117124
})) {
118125
lastResult = output;
126+
yield output;
119127

120128
if (!output.success) {
121129
setError('Compilation Error');
@@ -125,23 +133,14 @@ export async function runBuilder(
125133
setError(null);
126134
}
127135

128-
if (!write) {
136+
if (!write && output.outputFiles) {
129137
memResults.add(output.outputFiles.map((file) => new EsBuildResult(file)));
138+
}
130139

131-
// TODO!
132-
// '{\n' +
133-
// ' "success": true,\n' +
134-
// ' "assetFiles": [\n' +
135-
// ' {\n' +
136-
// ' "source": "C:\\\\temp\\\\native\\\\src\\\\favicon.ico",\n' +
137-
// ' "destination": "favicon.ico"\n' +
138-
// ' },\n' +
139-
// ' {\n' +
140-
// ' "source": "C:\\\\temp\\\\native\\\\src\\\\assets\\\\shutterstock_1835092750.jpg",\n' +
141-
// ' "destination": "assets\\\\shutterstock_1835092750.jpg"\n' +
142-
// ' }\n' +
143-
// ' ]\n' +
144-
// '}'
140+
if (!write && output.assetFiles) {
141+
memResults.add(
142+
output.assetFiles.map((file) => new NgCliAssetResult(file))
143+
);
145144
}
146145

147146
if (write) {
@@ -152,17 +151,23 @@ export async function runBuilder(
152151
await buildForFederation(config, fedOptions, externals);
153152
}
154153

155-
if (first && nfOptions.dev) {
154+
if (first && runServer) {
156155
startServer(nfOptions, options.outputPath, memResults);
157-
} else if (!first && nfOptions.dev) {
156+
}
157+
158+
if (!first && runServer) {
158159
reloadBrowser();
160+
}
159161

162+
if (!first && watch) {
160163
setTimeout(async () => {
161164
logger.info('Rebuilding federation artefacts ...');
162165
await Promise.all([rebuildEvents.rebuild.emit()]);
163166
logger.info('Done!');
164167

165-
setTimeout(() => reloadShell(nfOptions.shell), 0);
168+
if (runServer) {
169+
setTimeout(() => reloadShell(nfOptions.shell), 0);
170+
}
166171
}, nfOptions.rebuildDelay);
167172
}
168173

@@ -171,7 +176,7 @@ export async function runBuilder(
171176

172177
// updateIndexHtml(fedOptions);
173178
// const output = await lastValueFrom(builderRun.output as any);
174-
return lastResult || { success: false };
179+
yield lastResult || { success: false };
175180
}
176181

177182
export default createBuilder(runBuilder) as any;

libs/native-federation/src/builders/build/schema.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ export interface NfBuilderSchema extends JsonObject {
77
open: boolean;
88
rebuildDelay: number;
99
shell: string;
10+
watch: boolean;
1011
} // eslint-disable-line

libs/native-federation/src/builders/build/schema.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@
1515
"description": "Set this to true to start the builder in dev mode",
1616
"default": false
1717
},
18+
"watch": {
19+
"type": "boolean",
20+
"default": false
21+
},
1822
"port": {
1923
"type": "number",
20-
"default": 4200
24+
"default": 0
2125
},
2226
"open": {
2327
"type": "boolean",

libs/native-federation/src/utils/dev-server.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,14 @@ function modifyIndexHtml(content: string, fileNames: string[]): string {
102102
return index;
103103
}
104104

105-
function getBody(result: BuildResult, memResults: MemResults): string {
105+
function getBody(
106+
result: BuildResult,
107+
memResults: MemResults
108+
): Uint8Array | Buffer | string {
106109
const body = result.get();
107110
if (result.fileName === 'index.html') {
108-
return modifyIndexHtml(body, memResults.getFileNames());
111+
const content = new TextDecoder().decode(body);
112+
return modifyIndexHtml(content, memResults.getFileNames());
109113
} else {
110114
return body;
111115
}

libs/native-federation/src/utils/mem-resuts.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { OutputFile } from 'esbuild';
22
import { basename } from 'path';
3+
import * as fs from 'fs';
34

45
export interface BuildResult {
56
fileName: string;
6-
get(): string;
7+
get(): Uint8Array | Buffer;
78
}
89

910
export class EsBuildResult implements BuildResult {
@@ -13,8 +14,29 @@ export class EsBuildResult implements BuildResult {
1314
this.fileName = outputFile.path;
1415
}
1516

16-
get(): string {
17-
return this.outputFile.text;
17+
get(): Uint8Array {
18+
return this.outputFile.contents;
19+
}
20+
}
21+
22+
export interface NgCliAssetFile {
23+
source: string;
24+
destination: string;
25+
}
26+
27+
export class NgCliAssetResult implements BuildResult {
28+
public get fileName(): string {
29+
return this.file.source;
30+
}
31+
32+
private file: NgCliAssetFile;
33+
34+
constructor(private assetFile: NgCliAssetFile) {
35+
this.file = assetFile;
36+
}
37+
38+
get(): Buffer {
39+
return fs.readFileSync(this.file.source);
1840
}
1941
}
2042

update-local.bat

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ call npm unpublish @softarc/[email protected] --registry http://lo
33
call npm unpublish @softarc/native-federation-esbuild@2.0.0 --registry http://localhost:4873
44
call npm unpublish @angular-architects/native-federation@16.3.0 --registry http://localhost:4873
55

6-
call nx build native-federation
7-
call nx build native-federation-core
8-
call nx build native-federation-runtime
9-
call nx build native-federation-esbuild
6+
call npx nx build native-federation
7+
call npx nx build native-federation-core
8+
call npx nx build native-federation-runtime
9+
call npx nx build native-federation-esbuild
1010

1111
call node post-build.js
1212

0 commit comments

Comments
 (0)