Skip to content

Commit

Permalink
fix(nf-core): provide flag for transient deps
Browse files Browse the repository at this point in the history
  • Loading branch information
manfredsteyer committed Dec 22, 2024
1 parent d5183c6 commit d13fe55
Show file tree
Hide file tree
Showing 19 changed files with 177 additions and 65 deletions.
4 changes: 2 additions & 2 deletions libs/native-federation-core/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "@softarc/native-federation",
"version": "2.0.13",
"version": "2.0.15",
"type": "commonjs",
"license": "MIT",
"dependencies": {
"json5": "^2.2.0",
"npmlog": "^6.0.2",
"@softarc/native-federation-runtime": "2.0.13"
"@softarc/native-federation-runtime": "2.0.15"
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SkipList } from '../core/default-skip-list';
import { PreparedSkipList, SkipList } from '../core/default-skip-list';
import { MappedPath } from '../utils/mapped-paths';

export interface SharedConfig {
Expand All @@ -7,6 +7,8 @@ export interface SharedConfig {
requiredVersion?: string;
version?: string;
includeSecondaries?: boolean;
transient?: boolean;
platform?: 'browser' | 'node';
packageInfo?: {
entryPoint: string;
version: string;
Expand All @@ -28,6 +30,7 @@ export interface NormalizedSharedConfig {
requiredVersion: string;
version?: string;
includeSecondaries?: boolean;
platform: 'browser' | 'node';
packageInfo?: {
entryPoint: string;
version: string;
Expand All @@ -40,4 +43,5 @@ export interface NormalizedFederationConfig {
exposes: Record<string, string>;
shared: Record<string, NormalizedSharedConfig>;
sharedMappings: Array<MappedPath>;
skip: PreparedSkipList;
}
66 changes: 37 additions & 29 deletions libs/native-federation-core/src/lib/config/share-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
import { getConfigContext } from './configuration-context';
import { logger } from '../utils/logger';
import { resolveGlobSync } from '../utils/resolve-glob';
import { ShareObject } from '@softarc/native-federation-runtime';

let inferVersion = false;

Expand Down Expand Up @@ -109,26 +110,26 @@ function _findSecondaries(
): void {
const files = fs.readdirSync(libPath);

const dirs = files
const secondaries = files
.map((f) => path.join(libPath, f))
.filter((f) => fs.lstatSync(f).isDirectory() && f !== 'node_modules');

const secondaries = dirs.filter((d) =>
fs.existsSync(path.join(d, 'package.json'))
);
for (const s of secondaries) {
const secondaryLibName = s
.replace(/\\/g, '/')
.replace(/^.*node_modules[/]/, '');
if (excludes.includes(secondaryLibName)) {
continue;
}
if (fs.existsSync(path.join(s, 'package.json'))) {
const secondaryLibName = s
.replace(/\\/g, '/')
.replace(/^.*node_modules[/]/, '');
if (excludes.includes(secondaryLibName)) {
continue;
}

if (isInSkipList(secondaryLibName, PREPARED_DEFAULT_SKIP_LIST)) {
continue;
if (isInSkipList(secondaryLibName, PREPARED_DEFAULT_SKIP_LIST)) {
continue;
}

acc[secondaryLibName] = { ...shareObject };
}

acc[secondaryLibName] = { ...shareObject };
_findSecondaries(s, excludes, shareObject, acc);
}
}
Expand Down Expand Up @@ -351,27 +352,35 @@ type TransientDependency = {
};

function findTransientDeps(
packageNames: string[],
configuredShareObjects: Config,
projectRoot: string,
preparedSkipList: PreparedSkipList
): TransientDependency[] {
const discovered = new Set<string>();
const result: TransientDependency[] = [];

const packageNames = Object.keys(configuredShareObjects) as Array<
keyof typeof configuredShareObjects
>;

for (const packageName of packageNames) {
const packagePath = path.join(
projectRoot,
'node_modules',
packageName,
'package.json'
);
_findTransientDeps(
packagePath,
projectRoot,
preparedSkipList,
discovered,
result
);
const shareConfig = configuredShareObjects[packageName];

if (typeof shareConfig === 'object' && shareConfig.transient) {
const packagePath = path.join(
projectRoot,
'node_modules',
packageName,
'package.json'
);
_findTransientDeps(
packagePath,
projectRoot,
preparedSkipList,
discovered,
result
);
}
}

return result;
Expand Down Expand Up @@ -431,13 +440,12 @@ export function share(
projectPath = inferProjectPath(projectPath);
const packagePath = findPackageJson(projectPath);

const sharedPackageNames = Object.keys(configuredShareObjects);
const packageDirectory = path.dirname(packagePath);

const preparedSkipList = prepareSkipList(skipList);

const transientDeps = findTransientDeps(
sharedPackageNames,
configuredShareObjects,
packageDirectory,
preparedSkipList
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
prepareSkipList,
} from '../core/default-skip-list';
import { logger } from '../utils/logger';
import { DEFAULT_SERVER_DEPS_LIST } from '../core/default-server-deps-list';

export function withNativeFederation(
config: FederationConfig
Expand All @@ -22,6 +23,7 @@ export function withNativeFederation(
exposes: config.exposes ?? {},
shared: normalizeShared(config, skip),
sharedMappings: normalizeSharedMappings(config, skip),
skip
};
}

Expand All @@ -38,6 +40,7 @@ function normalizeShared(
singleton: true,
strictVersion: true,
requiredVersion: 'auto',
platform: 'browser',
}) as Record<string, NormalizedSharedConfig>;
} else {
result = Object.keys(shared).reduce(
Expand All @@ -50,6 +53,7 @@ function normalizeShared(
version: shared[cur].version,
includeSecondaries: shared[cur].includeSecondaries,
packageInfo: shared[cur].packageInfo,
platform: shared[cur].platform ?? getDefaultPlatform(cur)
},
}),
{}
Expand Down Expand Up @@ -82,7 +86,8 @@ function normalizeSharedMappings(
sharedMappings: config.sharedMappings,
});

const result = paths.filter(
const result = paths
.filter(
(p) => !isInSkipList(p.key, skip) && !p.key.includes('*')
);

Expand All @@ -92,3 +97,12 @@ function normalizeSharedMappings(

return result;
}

function getDefaultPlatform(cur: string): 'browser' | 'node' {
if (DEFAULT_SERVER_DEPS_LIST.find(e => cur.startsWith(e))) {
return 'node';
}
else {
return 'browser';
}
}
1 change: 1 addition & 0 deletions libs/native-federation-core/src/lib/core/build-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface BuildAdapterOptions {
watch?: boolean;
kind: BuildKind;
hash: boolean;
platform?: 'browser' | 'node';
}

export interface BuildResult {
Expand Down
31 changes: 29 additions & 2 deletions libs/native-federation-core/src/lib/core/build-for-federation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NormalizedFederationConfig } from '../config/federation-config';
import { NormalizedFederationConfig, NormalizedSharedConfig } from '../config/federation-config';
import { FederationInfo } from '@softarc/native-federation-runtime';
import { FederationOptions } from './federation-options';
import { writeImportMap } from './write-import-map';
Expand All @@ -19,6 +19,7 @@ export const defaultBuildParams: BuildParams = {
skipMappingsAndExposed: false,
};


export async function buildForFederation(
config: NormalizedFederationConfig,
fedOptions: FederationOptions,
Expand All @@ -38,7 +39,12 @@ export async function buildForFederation(
? describeExposed(config, fedOptions)
: artefactInfo.exposes;

const sharedPackageInfo = await bundleShared(config, fedOptions, externals);
const { sharedBrowser, sharedServer } = splitShared(config.shared);

const sharedPackageInfoBrowser = await bundleShared(sharedBrowser, config, fedOptions, externals, 'browser');
const sharedPackageInfoServer = await bundleShared(sharedServer, config, fedOptions, externals, 'node');

const sharedPackageInfo = [...sharedPackageInfoBrowser, ...sharedPackageInfoServer];

const sharedMappingInfo = !artefactInfo
? describeSharedMappings(config, fedOptions)
Expand All @@ -57,3 +63,24 @@ export async function buildForFederation(

return federationInfo;
}

function splitShared(shared: Record<string, NormalizedSharedConfig>): {sharedServer: Record<string, NormalizedSharedConfig>, sharedBrowser: Record<string, NormalizedSharedConfig>} {
const sharedServer: Record<string, NormalizedSharedConfig> = {};
const sharedBrowser: Record<string, NormalizedSharedConfig> = {};

for (const key in shared) {
if (shared[key].platform === 'node') {
sharedServer[key] = shared[key];
}
else {
sharedBrowser[key] = shared[key];
}
}

return {
sharedBrowser,
sharedServer
};

}

21 changes: 12 additions & 9 deletions libs/native-federation-core/src/lib/core/bundle-shared.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as path from 'path';
import * as fs from 'fs';
import { NormalizedFederationConfig } from '../config/federation-config';
import { NormalizedFederationConfig, NormalizedSharedConfig } from '../config/federation-config';
import { bundle } from '../utils/build-utils';
import { getPackageInfo, PackageInfo } from '../utils/package-info';
import { SharedInfo } from '@softarc/native-federation-runtime';
Expand All @@ -11,9 +11,11 @@ import { normalize } from '../utils/normalize';
import crypto from 'crypto';

export async function bundleShared(
sharedBundles: Record<string, NormalizedSharedConfig>,
config: NormalizedFederationConfig,
fedOptions: FederationOptions,
externals: string[]
externals: string[],
platform: 'browser' | 'node' = 'browser'
): Promise<Array<SharedInfo>> {
const folder = fedOptions.packageJson
? path.dirname(fedOptions.packageJson)
Expand All @@ -26,16 +28,16 @@ export async function bundleShared(

fs.mkdirSync(cachePath, { recursive: true });

const inferedPackageInfos = Object.keys(config.shared)
.filter((packageName) => !config.shared[packageName].packageInfo)
const inferedPackageInfos = Object.keys(sharedBundles)
.filter((packageName) => !sharedBundles[packageName].packageInfo)
.map((packageName) => getPackageInfo(packageName, folder))
.filter((pi) => !!pi) as PackageInfo[];

const configuredPackageInfos = Object.keys(config.shared)
.filter((packageName) => !!config.shared[packageName].packageInfo)
const configuredPackageInfos = Object.keys(sharedBundles)
.filter((packageName) => !!sharedBundles[packageName].packageInfo)
.map((packageName) => ({
packageName,
...config.shared[packageName].packageInfo,
...sharedBundles[packageName].packageInfo,
})) as PackageInfo[];

const packageInfos = [...inferedPackageInfos, ...configuredPackageInfos];
Expand Down Expand Up @@ -74,7 +76,7 @@ export async function bundleShared(
);

if (entryPoints.length > 0) {
logger.info('Preparing shared npm packages');
logger.info('Preparing shared npm packages for the platform ' + platform);
logger.notice('This only needs to be done once, as results are cached');
logger.notice(
"Skip packages you don't want to share in your federation config"
Expand All @@ -91,6 +93,7 @@ export async function bundleShared(
dev: fedOptions.dev,
kind: 'shared-package',
hash: false,
platform,
});

for (const fileName of exptedResults) {
Expand Down Expand Up @@ -134,7 +137,7 @@ export async function bundleShared(
const outFileNames = [...exptedResults];

return packageInfos.map((pi) => {
const shared = config.shared[pi.packageName];
const shared = sharedBundles[pi.packageName];
return {
packageName: pi.packageName,
outFileName: path.basename(outFileNames.shift() || ''),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

export const DEFAULT_SERVER_DEPS_LIST: string[] = [
'@angular/platform-server',
'@angular/platform-server/init',
'@angular/ssr',
];

export const DEFAULT_SERVER_DEPS_SET = new Set(DEFAULT_SERVER_DEPS_LIST);
6 changes: 3 additions & 3 deletions libs/native-federation-core/src/lib/core/get-externals.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { NormalizedFederationConfig } from '../config/federation-config';
// import { isInSkipList, PREPARED_DEFAULT_SKIP_LIST } from './default-skip-list';

export function getExternals(config: NormalizedFederationConfig) {
const shared = Object.keys(config.shared);
const sharedMappings = config.sharedMappings.map((m) => m.key);

const externals = [...shared, ...sharedMappings];
// TODO: Also handle deps that match RegExps and functions
const depsToSkip = config.skip.strings;
const externals = [...shared, ...sharedMappings, ...depsToSkip];

return externals;
// return externals.filter((p) => !isInSkipList(p, PREPARED_DEFAULT_SKIP_LIST));
}
2 changes: 1 addition & 1 deletion libs/native-federation-esbuild/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@softarc/native-federation-esbuild",
"version": "2.0.13",
"version": "2.0.15",
"type": "commonjs",
"dependencies": {
"@rollup/plugin-commonjs": "^22.0.2",
Expand Down
2 changes: 1 addition & 1 deletion libs/native-federation-node/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "@softarc/native-federation-node",
"version": "2.0.13"
"version": "2.0.15"
}
Loading

0 comments on commit d13fe55

Please sign in to comment.