Skip to content

Commit 848fa67

Browse files
committed
feat(adapter): add loader config option
1 parent 2f83307 commit 848fa67

File tree

16 files changed

+103
-33
lines changed

16 files changed

+103
-33
lines changed

libs/native-federation-core/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,8 @@ This library uses Import Maps. As currently not all browsers support this emergi
310310
```html
311311
<script type="esms-options">
312312
{
313-
"shimMode": true
313+
"shimMode": true,
314+
"mapOverrides": true
314315
}
315316
</script>
316317

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"name": "@softarc/native-federation",
3-
"version": "1.1.0",
3+
"version": "1.1.1-beta.0",
44
"type": "commonjs",
55
"dependencies": {
66
"json5": "^2.2.0",
77
"npmlog": "^6.0.2",
8-
"@softarc/native-federation-runtime": "1.1.0"
8+
"@softarc/native-federation-runtime": "1.1.1-beta.0"
99
}
1010
}

libs/native-federation-core/src/lib/config/configuration-context.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export interface ConfigurationContext {
22
workspaceRoot?: string;
3+
packageJson?: string;
34
}
45

56
let _context: ConfigurationContext = {};
@@ -8,6 +9,11 @@ export function useWorkspace(workspaceRoot: string): void {
89
_context = {..._context, workspaceRoot};
910
}
1011

12+
export function usePackageJson(packageJson?: string): void {
13+
_context = {..._context, packageJson};
14+
}
15+
16+
1117
export function getConfigContext(): ConfigurationContext {
1218
return _context;
1319
}

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

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -245,19 +245,17 @@ export function shareAll(
245245
projectPath = ''
246246
): Config | null {
247247

248-
let workspacePath: string | undefined = undefined;
248+
// let workspacePath: string | undefined = undefined;
249249

250-
if (!projectPath) {
251-
projectPath = cwd();
252-
}
250+
projectPath = inferProjectPath(projectPath);
253251

254-
workspacePath = getConfigContext().workspaceRoot ?? '';
252+
// workspacePath = getConfigContext().workspaceRoot ?? '';
255253

256-
if (!workspacePath) {
257-
workspacePath = projectPath;
258-
}
254+
// if (!workspacePath) {
255+
// workspacePath = projectPath;
256+
// }
259257

260-
const versionMaps = getVersionMaps(projectPath, workspacePath);
258+
const versionMaps = getVersionMaps(projectPath, projectPath);
261259
const share: Record<string, unknown> = {};
262260

263261
for(const versions of versionMaps) {
@@ -283,14 +281,28 @@ export function shareAll(
283281
return module.exports.share(share, projectPath);
284282
}
285283

284+
function inferProjectPath(projectPath: string) {
285+
if (!projectPath && getConfigContext().packageJson) {
286+
projectPath = path.dirname(getConfigContext().packageJson || '');
287+
}
288+
289+
if (!projectPath && getConfigContext().workspaceRoot) {
290+
projectPath = getConfigContext().workspaceRoot || '';
291+
}
292+
293+
if (!projectPath) {
294+
projectPath = cwd();
295+
}
296+
return projectPath;
297+
}
298+
286299
export function setInferVersion(infer: boolean): void {
287300
inferVersion = infer;
288301
}
289302

290303
export function share(shareObjects: Config, projectPath = ''): Config {
291-
if (!projectPath) {
292-
projectPath = cwd();
293-
}
304+
305+
projectPath = inferProjectPath(projectPath);
294306

295307
const packagePath = findPackageJson(projectPath);
296308

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,16 @@ export async function bundleShared(
1515
externals: string[]
1616
): Promise<Array<SharedInfo>> {
1717
const result: Array<SharedInfo> = [];
18+
19+
const folder = fedOptions.packageJson ?
20+
path.dirname(fedOptions.packageJson) :
21+
fedOptions.workspaceRoot;
22+
1823
const packageInfos = Object.keys(config.shared)
1924
// .filter((packageName) => !isInSkipList(packageName, PREPARED_DEFAULT_SKIP_LIST))
2025
.map((packageName) => getPackageInfo(
2126
packageName,
22-
fedOptions.workspaceRoot,
27+
folder,
2328
))
2429
.filter((pi) => !!pi) as PackageInfo[];
2530

@@ -42,6 +47,8 @@ export async function bundleShared(
4247
const encName = pi.packageName.replace(/[^A-Za-z0-9]/g, '_');
4348
const encVersion = pi.version.replace(/[^A-Za-z0-9]/g, '_');
4449

50+
// const env = fedOptions.dev ? 'dev' : 'prod';
51+
4552
// const outFileName = `${encName}-${encVersion}-${hash}.js`;
4653
const outFileName = `${encName}-${encVersion}.js`;
4754

libs/native-federation-core/src/lib/core/federation-builder.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getConfigContext, useWorkspace } from '../config/configuration-context';
1+
import { getConfigContext, usePackageJson, useWorkspace } from '../config/configuration-context';
22
import { NormalizedFederationConfig } from '../config/federation-config';
33
import { BuildAdapter, setBuildAdapter } from './build-adapter';
44
import { buildForFederation, defaultBuildParams } from './build-for-federation';
@@ -19,6 +19,7 @@ async function init(params: BuildHelperParams): Promise<void> {
1919
setBuildAdapter(params.adapter);
2020
fedOptions = params.options;
2121
useWorkspace(params.options.workspaceRoot);
22+
usePackageJson(params.options.packageJson);
2223
config = await loadFederationConfig(fedOptions);
2324
params.options.workspaceRoot = getConfigContext().workspaceRoot ?? params.options.workspaceRoot;
2425
externals = getExternals(config);

libs/native-federation-core/src/lib/core/federation-options.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ export interface FederationOptions {
66
verbose?: boolean;
77
dev?: boolean;
88
watch?: boolean;
9+
packageJson?: string;
910
}

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { dir } from 'console';
21
import * as fs from 'fs';
32
import * as path from 'path';
43
import { logger } from './logger';
@@ -63,7 +62,6 @@ export function getPackageInfo(
6362

6463
workspaceRoot = normalize(workspaceRoot, true);
6564

66-
6765
const packageJsonInfos = getPackageJsonFiles(workspaceRoot, workspaceRoot);
6866

6967
for (const info of packageJsonInfos) {
@@ -207,6 +205,27 @@ export function _getPackageInfo(
207205
};
208206
}
209207

208+
cand = mainPkgJson?.exports?.[relSecondaryPath]?.module;
209+
210+
if (typeof cand === 'object') {
211+
if (cand.module) {
212+
cand = cand.module;
213+
} else if (cand.default) {
214+
cand = cand.default;
215+
} else {
216+
cand = null;
217+
}
218+
}
219+
220+
if (cand) {
221+
return {
222+
entryPoint: path.join(mainPkgPath, cand),
223+
packageName,
224+
version,
225+
esm,
226+
};
227+
}
228+
210229
cand = mainPkgJson?.exports?.[relSecondaryPath]?.default;
211230
if (cand) {
212231
return {

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": "1.1.0",
3+
"version": "1.1.1-beta.0",
44
"type": "commonjs",
55
"dependencies": {
66
"@rollup/plugin-commonjs": "^22.0.2",

libs/native-federation-esbuild/src/lib/adapter.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,20 @@ export type ReplacementConfig = {
2424
file: string
2525
};
2626

27+
export type Environment = 'dev' | 'prod';
28+
2729
export interface EsBuildAdapterConfig {
2830
plugins: esbuild.Plugin[];
2931
fileReplacements?: Record<string, string | ReplacementConfig>
3032
skipRollup?: boolean,
31-
compensateExports?: RegExp[]
33+
compensateExports?: RegExp[],
34+
loader?: { [ext: string]: esbuild.Loader }
3235
}
3336

3437
export function createEsBuildAdapter(config: EsBuildAdapterConfig) {
3538

39+
normalizeConfig(config);
40+
3641
if (!config.compensateExports) {
3742
config.compensateExports = [new RegExp('/react/')];
3843
}
@@ -52,6 +57,7 @@ export function createEsBuildAdapter(config: EsBuildAdapterConfig) {
5257
entryPoints: [isPkg ? tmpFolder : entryPoint],
5358
external,
5459
outfile,
60+
loader: config.loader,
5561
bundle: true,
5662
sourcemap: true,
5763
minify: true,
@@ -80,6 +86,16 @@ export function createEsBuildAdapter(config: EsBuildAdapterConfig) {
8086
};
8187
}
8288

89+
function normalizeConfig(config: EsBuildAdapterConfig) {
90+
if (!config.environment) {
91+
config.environment = 'dev';
92+
}
93+
94+
if (!config.sourcemap) {
95+
config.sourcemap = config.environment === 'dev';
96+
}
97+
}
98+
8399
function compensateExports(entryPoint: string, outfile?: string): void {
84100
const inExports = collectExports(entryPoint);
85101
const outExports = outfile ? collectExports(outfile) : inExports;

0 commit comments

Comments
 (0)