Skip to content

Commit 2316abb

Browse files
committed
fix: Wildcard pattern for secondary entry points
1 parent d2051f2 commit 2316abb

2 files changed

Lines changed: 34 additions & 22 deletions

File tree

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import {
1717
} from '../utils/package-info';
1818
import { getConfigContext } from './configuration-context';
1919
import { logger } from '../utils/logger';
20-
import { resolveGlobSync } from '../utils/resolve-glob';
2120

2221
import {
2322
KeyValuePair,
@@ -224,10 +223,10 @@ function readConfiguredSecondaries(
224223
);
225224

226225
const result = {} as Record<string, SharedConfig>;
226+
const discoveredFiles = new Set<string>();
227227

228228
for (const key of keys) {
229229
const secondaryName = path.join(parent, key).replace(/\\/g, '/');
230-
231230
if (exclude.includes(secondaryName)) {
232231
continue;
233232
}
@@ -256,7 +255,11 @@ function readConfiguredSecondaries(
256255
libPath,
257256
parent,
258257
secondaryName,
259-
entry
258+
entry,
259+
discoveredFiles
260+
);
261+
items.forEach((e) =>
262+
discoveredFiles.add(typeof e === 'string' ? e : e.value)
260263
);
261264

262265
for (const item of items) {
@@ -285,15 +288,18 @@ function resolveSecondaries(
285288
libPath: string,
286289
parent: string,
287290
secondaryName: string,
288-
entry: string
291+
entry: string,
292+
discoveredFiles: Set<string>
289293
): Array<string | KeyValuePair> {
290294
let items: Array<string | KeyValuePair> = [];
291295
if (key.includes('*')) {
292296
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-
}));
297+
items = expanded
298+
.map((e) => ({
299+
key: path.join(parent, e.key),
300+
value: path.join(libPath, e.value),
301+
}))
302+
.filter((e) => !discoveredFiles.has(typeof e === 'string' ? e : e.value));
297303
} else {
298304
items = [secondaryName];
299305
}

libs/native-federation-core/src/lib/utils/resolve-wildcard-keys.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,19 @@ function escapeRegex(str: string) {
99
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
1010
}
1111

12+
// Convert package.json exports pattern to glob pattern
13+
// * in exports means "one segment", but for glob we need ** for deep matching
14+
// Src: https://hirok.io/posts/package-json-exports#exposing-all-package-files
15+
function convertExportsToGlob(pattern: string) {
16+
return pattern.replace(/(?<!\*)\*(?!\*)/g, '**');
17+
}
18+
1219
function compilePattern(pattern: string) {
1320
const tokens = pattern.split(/(\*\*|\*)/);
1421
const regexParts = [];
1522

1623
for (const token of tokens) {
1724
if (token === '*') {
18-
regexParts.push('([^/]+)');
19-
} else if (token === '**') {
2025
regexParts.push('(.*)');
2126
} else {
2227
regexParts.push(escapeRegex(token));
@@ -26,12 +31,12 @@ function compilePattern(pattern: string) {
2631
return new RegExp(`^${regexParts.join('')}$`);
2732
}
2833

29-
function applyWildcards(template: string, wildcardValues: string[]) {
34+
function withoutWildcard(template: string, wildcardValues: string[]) {
3035
const tokens = template.split(/(\*\*|\*)/);
3136
let result = '';
3237
let i = 0;
3338
for (const token of tokens) {
34-
if (token === '*' || token === '**') {
39+
if (token === '*') {
3540
result += wildcardValues[i++];
3641
} else {
3742
result += token;
@@ -46,28 +51,29 @@ export function resolveWildcardKeys(
4651
cwd: string
4752
): KeyValuePair[] {
4853
const normalizedPattern = valuePattern.replace(/^\.?\/+/, '');
54+
55+
const globPattern = convertExportsToGlob(normalizedPattern);
56+
4957
const regex = compilePattern(normalizedPattern);
5058

51-
const files = fg.sync(valuePattern, {
59+
const files = fg.sync(globPattern, {
5260
cwd,
5361
onlyFiles: true,
62+
deep: Infinity,
5463
});
5564

5665
const keys: KeyValuePair[] = [];
5766

5867
for (const file of files) {
5968
const relPath = file.replace(/\\/g, '/').replace(/^\.\//, '');
6069

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);
70+
const wildcards = relPath.match(regex);
71+
if (!wildcards) continue;
6872

69-
// Change this:
70-
keys.push({ key, value: relPath });
73+
keys.push({
74+
key: withoutWildcard(keyPattern, wildcards.slice(1)),
75+
value: relPath,
76+
});
7177
}
7278

7379
return keys;

0 commit comments

Comments
 (0)