Skip to content

Commit c57bfab

Browse files
committed
Step 2 - Use esbuild plugin to create import-map-config entryPoints; provide config URL in dev-server and generate file during build.
1 parent 663cf11 commit c57bfab

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { Plugin, PluginBuild } from 'esbuild';
2+
import { sep } from 'path';
3+
import { ConfigMf } from '../types';
4+
import { getDataForImportMap, IMPORT_MAP_CONFIG_NAME } from '../helpers';
5+
6+
export function importMapConfigPlugin(config: ConfigMf): Plugin {
7+
return {
8+
name: 'importMapConfig',
9+
setup(build: PluginBuild) {
10+
if (build.initialOptions.platform === 'node') return;
11+
const name = IMPORT_MAP_CONFIG_NAME.split('.').at(0);
12+
build.initialOptions.entryPoints = {
13+
...build.initialOptions.entryPoints,
14+
[name]: name,
15+
};
16+
17+
build.onResolve({ filter: new RegExp('^' + name) }, ({ kind, path }) => {
18+
if (kind !== 'entry-point') {
19+
return null;
20+
}
21+
return {
22+
path: path,
23+
namespace: name,
24+
};
25+
});
26+
27+
build.onLoad({ filter: /./, namespace: name }, () => {
28+
return {
29+
contents: `{}`,
30+
loader: 'json',
31+
};
32+
});
33+
34+
build.onEnd((result) => {
35+
const importMapResult = result.outputFiles.find(
36+
(i) => i.path.includes(name) && !i.path.endsWith('.map')
37+
);
38+
importMapResult.contents = new TextEncoder().encode(
39+
JSON.stringify(getDataForImportMap(config))
40+
);
41+
42+
const pathsArray = importMapResult.path.split(sep);
43+
pathsArray[pathsArray.length - 1] = `${name}.json`;
44+
importMapResult.path = pathsArray.join(sep);
45+
});
46+
},
47+
};
48+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './entry-point-for-extend-dependencies'
2+
export * from './import-map-config'

libs/nx-angular-mf/src/builders/helpers/init-import-map-utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
export const IMPORT_MAP_CONFIG_NAME = 'import-map-config.json';
2+
13
export function addToPathName(currentPathname, addPathname) {
24
return [
35
...currentPathname.split('/').filter((i) => !!i),
@@ -7,7 +9,7 @@ export function addToPathName(currentPathname, addPathname) {
79

810
export async function fetchImportMap(host: string) {
911
const urlHost = new URL(host);
10-
const pathName = addToPathName(urlHost.pathname, 'importmap.json');
12+
const pathName = addToPathName(urlHost.pathname, IMPORT_MAP_CONFIG_NAME);
1113
const url = new URL(pathName, urlHost.origin).toString();
1214
try {
1315
const r = await fetch(url);

libs/nx-angular-mf/src/builders/serve/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { Plugin } from 'esbuild';
1313
import { ServeExecutorSchema } from './schema';
1414
import { BuildExecutorSchema } from '../build/schema';
1515
import { deepMergeObject, getMapName, indexHtml, loadModule, patchBuilderContext, prepareConfig } from '../helpers';
16-
import { entryPointForExtendDependencies } from '../es-plugin';
16+
import { entryPointForExtendDependencies, importMapConfigPlugin } from '../es-plugin';
1717

1818

1919
function getBuilderAction() {
@@ -86,6 +86,7 @@ export async function* runBuilder(
8686

8787
const resultEsBuild = [
8888
...esPlugins,
89+
importMapConfigPlugin(optionsMfe),
8990
entryPointForExtendDependencies(optionsMfe)
9091
]
9192

0 commit comments

Comments
 (0)