Skip to content

Commit d87f613

Browse files
committed
feat(nf-core): determine if shared package is already esm
1 parent 5ad2a09 commit d87f613

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export interface BuildAdapterOptions {
1212
outfile: string;
1313
mappedPaths: MappedPath[];
1414
packageName?: string;
15+
esm?: boolean;
1516
}
1617

1718
export type BuildAdapter = (options: BuildAdapterOptions) => Promise<void>;

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

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export async function bundleShared(
4545
outfile: cachedFile,
4646
mappedPaths: config.sharedMappings,
4747
packageName: pi.packageName,
48+
esm: pi.esm,
4849
});
4950
}
5051
catch(e) {

libs/native-federation-core/src/lib/utils/package-info.ts

+20-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export interface PackageInfo {
55
packageName: string;
66
entryPoint: string;
77
version: string;
8+
esm: boolean;
89
}
910

1011
export interface PartialPackageJson {
@@ -33,6 +34,7 @@ export function getPackageInfo(
3334
const mainPkgJson = readJson(mainPkgJsonPath);
3435

3536
const version = mainPkgJson['version'] as string;
37+
const esm = mainPkgJson['type'] === 'module';
3638

3739
if (!version) {
3840
// TODO: Add logger
@@ -49,12 +51,23 @@ export function getPackageInfo(
4951
relSecondaryPath = './' + relSecondaryPath.replace(/\\/g, '/');
5052
}
5153

52-
let cand = mainPkgJson?.exports?.[relSecondaryPath]?.default;
54+
let cand = mainPkgJson?.exports?.[relSecondaryPath]?.import;
5355
if (cand) {
5456
return {
5557
entryPoint: path.join(mainPkgPath, cand),
5658
packageName,
5759
version,
60+
esm
61+
};
62+
}
63+
64+
cand = mainPkgJson?.exports?.[relSecondaryPath]?.default;
65+
if (cand) {
66+
return {
67+
entryPoint: path.join(mainPkgPath, cand),
68+
packageName,
69+
version,
70+
esm
5871
};
5972
}
6073

@@ -65,6 +78,7 @@ export function getPackageInfo(
6578
entryPoint: path.join(mainPkgPath, cand),
6679
packageName,
6780
version,
81+
esm: true
6882
};
6983
}
7084

@@ -80,6 +94,7 @@ export function getPackageInfo(
8094
entryPoint: path.join(secondaryPgkPath, secondaryPgkJson.module),
8195
packageName,
8296
version,
97+
esm: true
8398
};
8499
}
85100

@@ -89,6 +104,7 @@ export function getPackageInfo(
89104
entryPoint: cand,
90105
packageName,
91106
version,
107+
esm: true
92108
};
93109
}
94110

@@ -97,6 +113,7 @@ export function getPackageInfo(
97113
entryPoint: path.join(secondaryPgkPath, secondaryPgkJson.main),
98114
packageName,
99115
version,
116+
esm
100117
};
101118
}
102119

@@ -106,11 +123,12 @@ export function getPackageInfo(
106123
entryPoint: cand,
107124
packageName,
108125
version,
126+
esm
109127
};
110128
}
111129

112130
// TODO: Add logger
113-
console.warn('No esm-based entry point found for ' + packageName);
131+
console.warn('No entry point found for ' + packageName);
114132
console.warn(
115133
' >> Did you confuse dependencies with depDependencies in your package.json or your federation config?'
116134
);

libs/native-federation/src/utils/angular-esbuild-adapter.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ import { prepareNodePackage } from './prepare-node-package';
77
const SKIP_PACKAGE_PREPARATION = ['@angular', '@ngrx', 'rxjs', 'zone.js'];
88

99
export const AngularEsBuildAdapter: BuildAdapter = async (options) => {
10-
const { entryPoint, tsConfigPath, external, outfile, mappedPaths, packageName } = options;
10+
const { entryPoint, tsConfigPath, external, outfile, mappedPaths, packageName, esm } = options;
1111

1212
const pNameOrEmpty = packageName ?? '';
1313

1414
const preparePackage = entryPoint.includes("node_modules")
15-
&& !SKIP_PACKAGE_PREPARATION.find(p => pNameOrEmpty?.split('/')[0] === p);
15+
&& !(SKIP_PACKAGE_PREPARATION.find(p => pNameOrEmpty?.split('/')[0] === p)
16+
|| esm);
1617

1718
const pkgName = preparePackage ? inferePkgName(entryPoint) : "";
1819
const tmpFolder = `node_modules/.tmp/native-federation/${pkgName}`;

0 commit comments

Comments
 (0)