Skip to content

Commit 40849a5

Browse files
committed
feat(nf): Add esmsInitOptions to Angular builder to inject additional sms-module-shims options into index.html
1 parent 16630ae commit 40849a5

File tree

5 files changed

+47
-17
lines changed

5 files changed

+47
-17
lines changed

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ export async function* runBuilder(
207207
nfOptions.skipHtmlTransform
208208
? {}
209209
: {
210-
indexHtml: transformIndexHtml,
210+
indexHtml: transformIndexHtml(nfOptions),
211211
},
212212
{
213213
buildPlugins: plugins,
@@ -239,7 +239,7 @@ export async function* runBuilder(
239239
}
240240

241241
if (write && !nfOptions.dev && !nfOptions.skipHtmlTransform) {
242-
updateIndexHtml(fedOptions);
242+
updateIndexHtml(fedOptions, nfOptions);
243243
}
244244

245245
if (first && runServer) {
@@ -283,8 +283,13 @@ function infereConfigPath(tsConfig: string): string {
283283
return relConfigPath;
284284
}
285285

286-
function transformIndexHtml(content: string): Promise<string> {
287-
return Promise.resolve(updateScriptTags(content, 'main.js', 'polyfills.js'));
286+
function transformIndexHtml(
287+
nfOptions: NfBuilderSchema
288+
): (content: string) => Promise<string> {
289+
return (content: string): Promise<string> =>
290+
Promise.resolve(
291+
updateScriptTags(content, 'main.js', 'polyfills.js', nfOptions)
292+
);
288293
}
289294

290295
function addDebugInformation(fileName: string, rawBody: string): string {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { JsonObject } from '@angular-devkit/core';
2+
import type { ESMSInitOptions } from 'es-module-shims';
23

34
export interface NfBuilderSchema extends JsonObject {
45
target: string;
@@ -9,4 +10,5 @@ export interface NfBuilderSchema extends JsonObject {
910
shell: string;
1011
watch: boolean;
1112
skipHtmlTransform: boolean;
13+
esmsInitOptions: ESMSInitOptions;
1214
} // eslint-disable-line

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@
4242
"skipHtmlTransform": {
4343
"type": "boolean",
4444
"default": false
45+
},
46+
"esmsInitOptions": {
47+
"type": "object",
48+
"description": "Options for esms-module-shims https://github.com/guybedford/es-module-shims?tab=readme-ov-file#init-options",
49+
"default": {
50+
"shimMode": true
51+
}
4552
}
4653
}
4754
}

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export function startServer(
3434

3535
if (result) {
3636
const mimeType = lookup(extname(key)) || 'text/javascript';
37-
const body = getBody(result, memResults);
37+
const body = getBody(result, memResults, options);
3838
res.writeHead(200, {
3939
'Content-Type': mimeType,
4040
});
@@ -77,7 +77,11 @@ export function reloadShell(shellProjectName: string): void {
7777
}
7878
}
7979

80-
function modifyIndexHtml(content: string, fileNames: string[]): string {
80+
function modifyIndexHtml(
81+
content: string,
82+
fileNames: string[],
83+
nfOptions: NfBuilderSchema
84+
): string {
8185
if (buildError) {
8286
const errorHtml = `
8387
<div style="position: absolute; filter: opacity(80%); top:0; bottom:0; left:0; right:0; padding:20px; background-color:black; color:white; ">
@@ -94,18 +98,19 @@ function modifyIndexHtml(content: string, fileNames: string[]): string {
9498
(f) => f.startsWith('polyfills.') && f.endsWith('.js')
9599
);
96100

97-
const index = updateScriptTags(content, mainName, polyfillsName);
101+
const index = updateScriptTags(content, mainName, polyfillsName, nfOptions);
98102
return index;
99103
}
100104

101105
function getBody(
102106
result: BuildResult,
103-
memResults: MemResults
107+
memResults: MemResults,
108+
nfOptions: NfBuilderSchema
104109
): Uint8Array | Buffer | string {
105110
const body = result.get();
106111
if (result.fileName === 'index.html') {
107112
const content = new TextDecoder().decode(body);
108-
return modifyIndexHtml(content, memResults.getFileNames());
113+
return modifyIndexHtml(content, memResults.getFileNames(), nfOptions);
109114
} else {
110115
return body;
111116
}

libs/native-federation/src/utils/updateIndexHtml.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import * as path from 'path';
22
import * as fs from 'fs';
33
import { FederationOptions } from '@softarc/native-federation/build';
4+
import { NfBuilderSchema } from '../builders/build/schema';
45

5-
export function updateIndexHtml(fedOptions: FederationOptions) {
6+
export function updateIndexHtml(
7+
fedOptions: FederationOptions,
8+
nfOptions: NfBuilderSchema
9+
) {
610
const outputPath = path.join(fedOptions.workspaceRoot, fedOptions.outputPath);
711
const indexPath = path.join(outputPath, 'index.html');
812
const mainName = fs
@@ -14,21 +18,28 @@ export function updateIndexHtml(fedOptions: FederationOptions) {
1418

1519
let indexContent = fs.readFileSync(indexPath, 'utf-8');
1620

17-
indexContent = updateScriptTags(indexContent, mainName, polyfillsName);
21+
indexContent = updateScriptTags(
22+
indexContent,
23+
mainName,
24+
polyfillsName,
25+
nfOptions
26+
);
1827
fs.writeFileSync(indexPath, indexContent, 'utf-8');
1928
}
2029

2130
export function updateScriptTags(
2231
indexContent: string,
2332
mainName: string,
24-
polyfillsName: string
33+
polyfillsName: string,
34+
nfOptions: NfBuilderSchema
2535
) {
36+
const esmsOptions = {
37+
shimMode: true,
38+
...nfOptions.esmsInitOptions,
39+
};
40+
2641
const htmlFragment = `
27-
<script type="esms-options">
28-
{
29-
"shimMode": true
30-
}
31-
</script>
42+
<script type="esms-options">${JSON.stringify(esmsOptions)}</script>
3243
3344
<script type="module" src="${polyfillsName}"></script>
3445
<script type="module-shim" src="${mainName}"></script>

0 commit comments

Comments
 (0)