Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): add support for tsconfig wildcard paths #571

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion libs/native-federation-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
"dependencies": {
"json5": "^2.2.0",
"npmlog": "^6.0.2",
"@softarc/native-federation-runtime": "2.0.9"
"@softarc/native-federation-runtime": "2.0.9",
"fast-glob": "^3.3.2",
"micromatch": "^4.0.7"
},
"devDependencies": {
"@types/micromatch": "^4.0.7"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,5 @@ function normalizeSharedMappings(
(p) => !isInSkipList(p.key, skip) && !p.key.includes('*')
);

if (paths.find((p) => p.key.includes('*'))) {
logger.warn('Sharing mapped paths with wildcards (*) not supported');
}

return result;
}
36 changes: 34 additions & 2 deletions libs/native-federation-core/src/lib/utils/mapped-paths.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as path from 'path';
import * as fs from 'fs';
import * as JSON5 from 'json5';
import * as glob from 'fast-glob';
import * as mm from 'micromatch';

export interface MappedPath {
key: string;
Expand Down Expand Up @@ -39,8 +41,7 @@ export function getMappedPaths({
fs.readFileSync(rootTsConfigPath, { encoding: 'utf-8' })
);

const mappings = tsConfig?.compilerOptions?.paths;

const mappings = resolveWildcardsToPaths(tsConfig?.compilerOptions?.paths);
if (!mappings) {
return result;
}
Expand All @@ -58,3 +59,34 @@ export function getMappedPaths({

return result;
}

function resolveWildcardsToPaths(paths: { [key: string]: string[] }): {
[key: string]: string[];
} {
let results = {};
for (const key in paths) {
const path = paths[key][0];
if (path.includes('*')) {
const entries = glob.sync(path, { unique: true });

for (const entry of entries) {
if (!entry.includes('index.ts') && !entry.includes('index.js')) {
continue;
}

const capturedPath = mm.capture(path, entry);
if (!capturedPath) {
Comment on lines +73 to +78
Copy link

@John-Pier John-Pier Oct 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know exactly how micromatch works, but...
Angular (ng-packagr) uses "ng-package.json" with lib.entryFile in secondary entry point to define entryFile of the point;
Almost always this is an index.ts file, but it is still worth considering ng-package.json

continue;
}

results = {
...results,
[key.replace('*', capturedPath[0])]: [entry],
};
}
} else {
results = { ...results, [key]: [path] };
}
}
return results;
}
66 changes: 33 additions & 33 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
"@angular/router": "17.1.2",
"@module-federation/vite": "^0.2.6",
"es-module-shims": "^1.5.12",
"fast-glob": "^3.3.2",
"micromatch": "^4.0.7",
"rxjs": "^7.0.0",
"tslib": "^2.0.0",
"zone.js": "0.14.2"
Expand Down Expand Up @@ -82,6 +84,7 @@
"@types/browser-sync": "^2.29.0",
"@types/cross-spawn": "^6.0.2",
"@types/jest": "29.5.11",
"@types/micromatch": "^4.0.7",
"@types/node": "^18.16.9",
"@types/npmlog": "^4.1.4",
"@typescript-eslint/eslint-plugin": "6.21.0",
Expand Down