Skip to content

Commit 67252ea

Browse files
committed
feat(nf-core): don't create tsconfig.federation.ts when ignoreUnusedDeps is activated
1 parent 4c297e9 commit 67252ea

File tree

14 files changed

+184
-29
lines changed

14 files changed

+184
-29
lines changed

libs/native-federation-core/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
{
22
"name": "@softarc/native-federation",
3-
"version": "3.1.1",
3+
"version": "3.2.0",
44
"type": "commonjs",
55
"license": "MIT",
66
"dependencies": {
77
"json5": "^2.2.0",
88
"chalk": "^4.1.2",
9-
"@softarc/native-federation-runtime": "3.1.1"
9+
"@softarc/native-federation-runtime": "3.2.0",
10+
"fast-glob": "^3.3.3"
1011
},
1112
"peerDependencies": {
1213
"@softarc/sheriff-core": "^0.18.0"

libs/native-federation-core/src/lib/config/share-utils.ts

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ import { getConfigContext } from './configuration-context';
1919
import { logger } from '../utils/logger';
2020
import { resolveGlobSync } from '../utils/resolve-glob';
2121

22+
import {
23+
KeyValuePair,
24+
resolveWildcardKeys,
25+
} from '../utils/resolve-wildcard-keys';
26+
2227
let inferVersion = false;
2328

2429
export const DEFAULT_SECONARIES_SKIP_LIST = [
@@ -195,6 +200,10 @@ function readConfiguredSecondaries(
195200
}
196201

197202
const packageJson = JSON.parse(fs.readFileSync(libPackageJson, 'utf-8'));
203+
204+
const version = packageJson['version'] as string;
205+
const esm = packageJson['type'] === 'module';
206+
198207
const exports = packageJson['exports'] as Record<
199208
string,
200209
Record<string, string>
@@ -242,12 +251,29 @@ function readConfiguredSecondaries(
242251
continue;
243252
}
244253

245-
const items = resolveSecondaries(key, libPath, parent, secondaryName);
254+
const items = resolveSecondaries(
255+
key,
256+
libPath,
257+
parent,
258+
secondaryName,
259+
entry
260+
);
246261

247262
for (const item of items) {
248-
result[item] = {
249-
...shareObject,
250-
};
263+
if (typeof item === 'object') {
264+
result[item.key] = {
265+
...shareObject,
266+
packageInfo: {
267+
entryPoint: item.value,
268+
version: shareObject.version ?? version,
269+
esm
270+
},
271+
};
272+
} else {
273+
result[item] = {
274+
...shareObject,
275+
};
276+
}
251277
}
252278
}
253279

@@ -258,13 +284,16 @@ function resolveSecondaries(
258284
key: string,
259285
libPath: string,
260286
parent: string,
261-
secondaryName: string
262-
) {
263-
let items = [];
287+
secondaryName: string,
288+
entry: string
289+
): Array<string | KeyValuePair> {
290+
let items: Array<string | KeyValuePair> = [];
264291
if (key.includes('*')) {
265-
items = resolveGlobSync(key, libPath).map((e) =>
266-
path.join(parent, e.substring(libPath.length))
267-
);
292+
const expanded = resolveWildcardKeys(key, entry, libPath);
293+
items = expanded.map((e) => ({
294+
key: path.join(parent, e.key),
295+
value: path.join(libPath, e.value),
296+
}));
268297
} else {
269298
items = [secondaryName];
270299
}

libs/native-federation-core/src/lib/config/with-native-federation.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export function withNativeFederation(
1818
): NormalizedFederationConfig {
1919
const skip = prepareSkipList(config.skip ?? []);
2020

21-
return {
21+
const normalized = {
2222
name: config.name ?? '',
2323
exposes: config.exposes ?? {},
2424
shared: normalizeShared(config, skip),
@@ -30,6 +30,31 @@ export function withNativeFederation(
3030
ignoreUnusedDeps: config.features?.ignoreUnusedDeps ?? false,
3131
},
3232
};
33+
34+
// This is for being backwards compatible
35+
if (!normalized.features.ignoreUnusedDeps) {
36+
normalized.shared = filterShared(normalized.shared);
37+
}
38+
39+
return normalized;
40+
}
41+
42+
function filterShared(
43+
shared: Record<string, NormalizedSharedConfig>
44+
): Record<string, NormalizedSharedConfig> {
45+
const keys = Object.keys(shared).filter(
46+
(k) => !k.startsWith('@angular/common/locales')
47+
);
48+
49+
const filtered = keys.reduce(
50+
(acc, curr) => ({
51+
...acc,
52+
[curr]: shared[curr],
53+
}),
54+
{}
55+
);
56+
57+
return filtered;
3358
}
3459

3560
function normalizeShared(

libs/native-federation-core/src/lib/core/build-adapter.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export interface BuildAdapterOptions {
3131
kind: BuildKind;
3232
hash: boolean;
3333
platform?: 'browser' | 'node';
34+
optimizedMappings?: boolean
3435
}
3536

3637
export interface BuildResult {

libs/native-federation-core/src/lib/core/bundle-exposed-and-mappings.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export async function bundleExposedAndMappings(
5050
mappedPaths: config.sharedMappings,
5151
kind: 'mapping-or-exposed',
5252
hash,
53+
optimizedMappings: config.features.ignoreUnusedDeps
5354
});
5455

5556
const resultMap = createBuildResultMap(result, hash);

libs/native-federation-core/src/lib/core/bundle-shared.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ export async function bundleShared(
9999
kind: 'shared-package',
100100
hash: false,
101101
platform,
102+
optimizedMappings: config.features.ignoreUnusedDeps
102103
});
103104

104105
const cachedFiles = bundleResult.map((br) => path.basename(br.fileName));

libs/native-federation-core/src/lib/core/default-skip-list.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,22 @@ export const DEFAULT_SKIP_LIST: SkipList = [
1616
'@angular/localize',
1717
'@angular/localize/init',
1818
'@angular/localize/tools',
19-
(pkg) => pkg.startsWith('@angular/common/locales'),
2019
(pkg) => pkg.startsWith('rxjs/internal'),
2120
// '@angular/platform-server',
2221
// '@angular/platform-server/init',
2322
// '@angular/ssr',
24-
'express',
2523
/\/schematics(\/|$)/,
2624
/^@nx\/angular/,
2725
(pkg) => pkg.startsWith('@angular/') && !!pkg.match(/\/testing(\/|$)/),
2826
(pkg) => pkg.startsWith('@types/'),
27+
'express',
28+
// (pkg) => pkg.startsWith('@angular/common/locales'),
29+
2930
];
3031

3132
export const PREPARED_DEFAULT_SKIP_LIST = prepareSkipList(DEFAULT_SKIP_LIST);
3233

34+
3335
export type PreparedSkipList = {
3436
strings: Set<string>;
3537
functions: SkipFn[];
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import fg from 'fast-glob';
2+
3+
export type KeyValuePair = {
4+
key: string;
5+
value: string;
6+
};
7+
8+
function escapeRegex(str: string) {
9+
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
10+
}
11+
12+
function compilePattern(pattern: string) {
13+
const tokens = pattern.split(/(\*\*|\*)/);
14+
const regexParts = [];
15+
16+
for (const token of tokens) {
17+
if (token === '*') {
18+
regexParts.push('([^/]+)');
19+
} else if (token === '**') {
20+
regexParts.push('(.*)');
21+
} else {
22+
regexParts.push(escapeRegex(token));
23+
}
24+
}
25+
26+
return new RegExp(`^${regexParts.join('')}$`);
27+
}
28+
29+
function applyWildcards(template: string, wildcardValues: string[]) {
30+
const tokens = template.split(/(\*\*|\*)/);
31+
let result = '';
32+
let i = 0;
33+
for (const token of tokens) {
34+
if (token === '*' || token === '**') {
35+
result += wildcardValues[i++];
36+
} else {
37+
result += token;
38+
}
39+
}
40+
return result;
41+
}
42+
43+
export function resolveWildcardKeys(
44+
keyPattern: string,
45+
valuePattern: string,
46+
cwd: string
47+
): KeyValuePair[] {
48+
const normalizedPattern = valuePattern.replace(/^\.?\/+/, '');
49+
const regex = compilePattern(normalizedPattern);
50+
51+
const files = fg.sync(valuePattern, {
52+
cwd,
53+
onlyFiles: true,
54+
});
55+
56+
const keys: KeyValuePair[] = [];
57+
58+
for (const file of files) {
59+
const relPath = file.replace(/\\/g, '/').replace(/^\.\//, '');
60+
61+
const match = relPath.match(regex);
62+
if (!match) {
63+
continue;
64+
}
65+
66+
const wildcards = match.slice(1);
67+
const key = applyWildcards(keyPattern, wildcards);
68+
69+
// Change this:
70+
keys.push({ key, value: relPath });
71+
}
72+
73+
return keys;
74+
}

libs/native-federation-esbuild/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@softarc/native-federation-esbuild",
3-
"version": "3.1.1",
3+
"version": "3.2.0",
44
"type": "commonjs",
55
"dependencies": {
66
"@rollup/plugin-commonjs": "^22.0.2",
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"name": "@softarc/native-federation-node",
3-
"version": "3.1.1",
3+
"version": "3.2.0",
44
"license": "MIT"
55
}

0 commit comments

Comments
 (0)