Skip to content
Merged
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
60 changes: 43 additions & 17 deletions packages/data-prefetch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,54 @@
],
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.esm.js",
"require": "./dist/index.cjs.cjs"
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"require": {
"types": "./dist/index.d.ts",
"default": "./dist/index.cjs"
}
},
"./react": {
"types": "./dist/react.d.ts",
"import": "./dist/react.esm.js",
"require": "./dist/react.cjs.cjs"
"import": {
"types": "./dist/react.d.ts",
"default": "./dist/react.js"
},
"require": {
"types": "./dist/react.d.ts",
"default": "./dist/react.cjs"
}
},
"./cli": {
"types": "./dist/cli.d.ts",
"import": "./dist/cli.esm.js",
"require": "./dist/cli.cjs.cjs"
"import": {
"types": "./dist/cli.d.ts",
"default": "./dist/cli.js"
},
"require": {
"types": "./dist/cli.d.ts",
"default": "./dist/cli.cjs"
}
},
"./babel-plugin": {
"types": "./dist/babel.d.ts",
"import": "./dist/babel.esm.js",
"require": "./dist/babel.cjs.cjs"
"import": {
"types": "./dist/babel.d.ts",
"default": "./dist/babel.js"
},
"require": {
"types": "./dist/babel.d.ts",
"default": "./dist/babel.cjs"
}
},
"./universal": {
"types": "./dist/universal.d.ts",
"import": "./dist/universal.esm.js",
"require": "./dist/universal.cjs.cjs"
"import": {
"types": "./dist/universal.d.ts",
"default": "./dist/universal.js"
},
"require": {
"types": "./dist/universal.d.ts",
"default": "./dist/universal.cjs"
}
}
},
"typesVersions": {
Expand All @@ -68,8 +93,9 @@
]
}
},
"main": "dist/index.cjs.cjs",
"module": "dist/index.esm.js",
"main": "./dist/index.cjs",
"types": "./dist/index.d.ts",
"module": "./dist/index.js",
"peerDependencies": {
"react": ">=16.9.0",
"react-dom": ">=16.9.0"
Expand Down
24 changes: 11 additions & 13 deletions packages/data-prefetch/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,18 @@
"tags": ["type:pkg"],
"targets": {
"build": {
"executor": "@nx/rollup:rollup",
"outputs": ["{options.outputPath}"],
"executor": "nx:run-commands",
"outputs": ["{workspaceRoot}/packages/data-prefetch/dist"],
"options": {
"outputPath": "packages/data-prefetch/dist",
"main": "packages/data-prefetch/src/index.ts",
"tsConfig": "packages/data-prefetch/tsconfig.lib.json",
"assets": [],
"project": "packages/data-prefetch/package.json",
"rollupConfig": "packages/data-prefetch/rollup.config.cjs",
"compiler": "tsc",
"format": ["cjs", "esm"],
"generatePackageJson": false,
"useLegacyTypescriptPlugin": false
}
"command": "rslib build",
"cwd": "packages/data-prefetch"
},
"dependsOn": [
{
"target": "build",
"dependencies": true
}
]
},
"test": {
"executor": "@nx/jest:jest",
Expand Down
47 changes: 47 additions & 0 deletions packages/data-prefetch/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { defineConfig } from '@rslib/core';

export default defineConfig({
lib: [
{
format: 'esm',
syntax: 'es2021',
bundle: false,
outBase: 'src',
dts: {
bundle: false,
distPath: './dist',
},
},
{
format: 'cjs',
syntax: 'es2021',
bundle: false,
outBase: 'src',
dts: false,
},
],
source: {
entry: {
index: [
'./src/**/*.{ts,tsx,js,jsx}',
'!./src/**/*.spec.*',
'!./src/**/*.test.*',
],
},
tsconfigPath: './tsconfig.lib.json',
},
output: {
target: 'node',
minify: false,
distPath: {
root: './dist',
},
externals: [/@module-federation\//, 'react', 'react-dom'],
copy: [
{
from: './LICENSE',
to: '.',
},
],
},
});
1 change: 1 addition & 0 deletions packages/data-prefetch/src/babel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './cli/babel';
1 change: 1 addition & 0 deletions packages/data-prefetch/src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './cli/index';
2 changes: 1 addition & 1 deletion packages/data-prefetch/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ModuleFederationRuntimePlugin } from '@module-federation/runtime/types';
import type { ModuleFederationRuntimePlugin } from '@module-federation/runtime';
import { ModuleInfo, getResourceUrl } from '@module-federation/sdk';

import { getSignalFromManifest } from './common/runtime-utils';
Expand Down
24 changes: 12 additions & 12 deletions packages/data-prefetch/src/prefetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ interface FederationWithPrefetch extends Federation {
}

type PrefetchExports = Record<string, any>;
type FederationGlobal = typeof globalThis & {
__FEDERATION__?: FederationWithPrefetch;
};

export interface DataPrefetchOptions {
name: string;
Expand All @@ -38,14 +41,15 @@ export interface prefetchOptions {
}

// @ts-ignore init global variable for test
globalThis.__FEDERATION__ ??= {};
(
globalThis.__FEDERATION__ as unknown as FederationWithPrefetch
).__PREFETCH__ ??= {
const federationGlobal = globalThis as FederationGlobal;
federationGlobal.__FEDERATION__ ??= {} as FederationWithPrefetch;
federationGlobal.__FEDERATION__.__PREFETCH__ ??= {
entryLoading: {},
instance: new Map(),
__PREFETCH_EXPORTS__: {},
} as FederationWithPrefetch['__PREFETCH__'];
const getFederation = (): FederationWithPrefetch =>
(globalThis as FederationGlobal).__FEDERATION__!;
export class MFDataPrefetch {
public prefetchMemory: Map<string, Promise<any>>;
public recordOutdate: Record<string, Record<string, boolean>>;
Expand All @@ -61,14 +65,11 @@ export class MFDataPrefetch {
}

get global(): FederationWithPrefetch['__PREFETCH__'] {
return (globalThis.__FEDERATION__ as unknown as FederationWithPrefetch)
.__PREFETCH__;
return getFederation().__PREFETCH__;
}

static getInstance(id: string): MFDataPrefetch | undefined {
return (
globalThis.__FEDERATION__ as unknown as FederationWithPrefetch
).__PREFETCH__.instance.get(id);
return getFederation().__PREFETCH__.instance.get(id);
}

async loadEntry(entry: string | undefined): Promise<any> {
Expand Down Expand Up @@ -98,9 +99,8 @@ export class MFDataPrefetch {
return this._exports;
}
const { name } = this._options;
const exportsPromiseFn = (
globalThis.__FEDERATION__ as unknown as FederationWithPrefetch
).__PREFETCH__.__PREFETCH_EXPORTS__?.[name];
const exportsPromiseFn =
getFederation().__PREFETCH__.__PREFETCH_EXPORTS__?.[name];
const exportsPromise =
typeof exportsPromiseFn === 'function'
? exportsPromiseFn()
Expand Down
1 change: 1 addition & 0 deletions packages/data-prefetch/src/react.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './react/index';
1 change: 1 addition & 0 deletions packages/data-prefetch/src/universal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './universal/index';
2 changes: 1 addition & 1 deletion packages/data-prefetch/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"rootDir": "./src",
"declaration": true,
"types": ["node"]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,47 @@ const { mkdirpSync } = require(
normalizeWebpackPath('webpack/lib/util/fs'),
) as typeof import('webpack/lib/util/fs');

const RuntimeToolsPath = require.resolve(
function resolveModule(
candidates: string[],
options?: NodeJS.RequireResolveOptions,
): string {
let lastError: unknown;
for (const candidate of candidates) {
try {
return require.resolve(candidate, options);
} catch (error) {
lastError = error;
}
}

throw (
lastError ??
new Error(`Unable to resolve any module from: ${candidates.join(', ')}`)
);
}

const RuntimeToolsPath = resolveModule([
'@module-federation/runtime-tools',
'@module-federation/runtime-tools/dist/index.esm.js',
);
const BundlerRuntimePath = require.resolve(
'@module-federation/webpack-bundler-runtime/dist/index.esm.js',
'@module-federation/runtime-tools/dist/index.cjs.cjs',
]);
const BundlerRuntimePath = resolveModule(
[
'@module-federation/webpack-bundler-runtime',
'@module-federation/webpack-bundler-runtime/dist/index.js',
'@module-federation/webpack-bundler-runtime/dist/index.esm.js',
'@module-federation/webpack-bundler-runtime/dist/index.cjs.cjs',
],
{
paths: [RuntimeToolsPath],
},
);
const RuntimePath = require.resolve(
'@module-federation/runtime/dist/index.esm.js',
const RuntimePath = resolveModule(
[
'@module-federation/runtime',
'@module-federation/runtime/dist/index.esm.js',
'@module-federation/runtime/dist/index.cjs.cjs',
],
{
paths: [RuntimeToolsPath],
},
Expand Down Expand Up @@ -431,8 +461,13 @@ class FederationRuntimePlugin {
}

if (this.options?.implementation) {
this.bundlerRuntimePath = require.resolve(
'@module-federation/webpack-bundler-runtime/dist/index.esm.js',
this.bundlerRuntimePath = resolveModule(
[
'@module-federation/webpack-bundler-runtime',
'@module-federation/webpack-bundler-runtime/dist/index.js',
'@module-federation/webpack-bundler-runtime/dist/index.esm.js',
'@module-federation/webpack-bundler-runtime/dist/index.cjs.cjs',
],
{
paths: [this.options.implementation],
},
Expand Down
3 changes: 3 additions & 0 deletions packages/error-codes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
"publishConfig": {
"access": "public"
},
"browser": {
"url": false
},
"main": "./dist/index.cjs.js",
"module": "./dist/index.esm.mjs",
"types": "./dist/index.d.ts",
Expand Down
3 changes: 3 additions & 0 deletions packages/runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
"main": "./dist/index.cjs.cjs",
"module": "./dist/index.esm.js",
"types": "./dist/index.d.ts",
"browser": {
"url": false
},
"license": "MIT",
"publishConfig": {
"access": "public"
Expand Down
23 changes: 16 additions & 7 deletions packages/runtime/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,22 @@ export type {
IShareUtils,
} from '@module-federation/runtime-core';

export default {
...helpers,
global: {
...helpers.global,
getGlobalFederationInstance,
},
} as {
export const global = {
...helpers.global,
getGlobalFederationInstance,
};

export const share = helpers.share;

export const utils = helpers.utils;

const runtimeHelpers = {
global,
share,
utils,
};

export default runtimeHelpers as {
global: typeof helpers.global & {
getGlobalFederationInstance: typeof getGlobalFederationInstance;
};
Expand Down
3 changes: 3 additions & 0 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
"main": "./dist/index.cjs.cjs",
"module": "./dist/index.esm.js",
"types": "./dist/index.d.ts",
"browser": {
"url": false
},
"exports": {
".": {
"import": {
Expand Down