Skip to content
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
20 changes: 10 additions & 10 deletions packages/runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"version": "0.24.1",
"type": "module",
"author": "zhouxiao <codingzx@gmail.com>",
"main": "./dist/index.cjs.cjs",
"module": "./dist/index.esm.js",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"license": "MIT",
"publishConfig": {
Expand All @@ -23,41 +23,41 @@
".": {
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.esm.js"
"default": "./dist/index.js"
},
Comment on lines 24 to 27

Choose a reason for hiding this comment

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

P2 Badge Use .mjs for ESM entrypoints

Rslib’s ESM output defaults to .mjs in this repo (see the existing rslib-built package packages/modernjs which exports dist/esm/*.mjs), but the runtime package now points all ESM entrypoints to .js (e.g., ./dist/index.js). If rslib keeps emitting .mjs here as well, ESM consumers will hit MODULE_NOT_FOUND at runtime because the files referenced in module/exports.import don’t exist. Consider switching these ESM paths to .mjs (or explicitly configuring rslib to emit .js).

Useful? React with 👍 / 👎.

"require": {
"types": "./dist/index.d.ts",
"default": "./dist/index.cjs.cjs"
"default": "./dist/index.cjs"
}
},
"./helpers": {
"import": {
"types": "./dist/helpers.d.ts",
"default": "./dist/helpers.esm.js"
"default": "./dist/helpers.js"
},
"require": {
"types": "./dist/helpers.d.ts",
"default": "./dist/helpers.cjs.cjs"
"default": "./dist/helpers.cjs"
}
},
"./types": {
"import": {
"types": "./dist/types.d.ts",
"default": "./dist/types.esm.js"
"default": "./dist/types.js"
},
"require": {
"types": "./dist/types.d.ts",
"default": "./dist/types.cjs.cjs"
"default": "./dist/types.cjs"
}
},
"./core": {
"import": {
"types": "./dist/core.d.ts",
"default": "./dist/core.esm.js"
"default": "./dist/core.js"
},
"require": {
"types": "./dist/core.d.ts",
"default": "./dist/core.cjs.cjs"
"default": "./dist/core.cjs"
}
},
"./*": "./*"
Expand Down
22 changes: 4 additions & 18 deletions packages/runtime/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,11 @@
"tags": ["type:pkg"],
"targets": {
"build": {
"executor": "@nx/rollup:rollup",
"executor": "nx:run-commands",
"outputs": ["{workspaceRoot}/packages/runtime/dist"],
"options": {
"parallel": false,
"outputPath": "packages/runtime/dist",
"main": "packages/runtime/src/index.ts",
"additionalEntryPoints": [
"packages/runtime/src/types.ts",
"packages/runtime/src/helpers.ts"
],
"tsConfig": "packages/runtime/tsconfig.lib.json",
"assets": [],
"external": ["@module-federation/*"],
"project": "packages/runtime/package.json",
"compiler": "tsc",
"rollupConfig": "packages/runtime/rollup.config.cjs",
"format": ["cjs", "esm"],
"generatePackageJson": false,
"useLegacyTypescriptPlugin": false
"command": "rslib build",
"cwd": "packages/runtime"
},
"dependsOn": [
{
Expand All @@ -49,7 +35,7 @@
"parallel": false,
"commands": [
{
"command": "FEDERATION_DEBUG=true nx run runtime:build",
"command": "FEDERATION_DEBUG=true nx run runtime:build --skip-nx-cache",
"forwardAllArgs": false
}
]
Expand Down
69 changes: 69 additions & 0 deletions packages/runtime/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { defineConfig } from '@rslib/core';
import { readFileSync } from 'fs';
import { join } from 'path';

const pkg = JSON.parse(
readFileSync(join(process.cwd(), 'package.json'), 'utf-8'),
);

const FEDERATION_DEBUG = process.env.FEDERATION_DEBUG || '';

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.*',
],
},
define: {
__VERSION__: JSON.stringify(pkg.version),
FEDERATION_DEBUG: JSON.stringify(FEDERATION_DEBUG),
},
tsconfigPath: './tsconfig.lib.json',
},
output: {
target: 'node',
minify: false,
distPath: {
root: './dist',
},
externals: [/@module-federation\//],
copy: [
{
from: './LICENSE',
to: '.',
},
],
},
tools: {
rspack: (config: any) => {
if (FEDERATION_DEBUG && config.output?.library?.type === 'module') {
config.output.library.type = 'var';
config.output.iife = true;
config.externals = undefined;
}
return config;
},
},
});
48 changes: 26 additions & 22 deletions packages/runtime/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,32 @@ export function getGlobalFederationInstance(
version: string | undefined,
): ModuleFederation | undefined {
const buildId = getBuilderId();
return CurrentGlobal.__FEDERATION__.__INSTANCES__.find(
(GMInstance: ModuleFederation) => {
if (buildId && GMInstance.options.id === buildId) {
return true;
}
const federation = (
CurrentGlobal as typeof globalThis & {
__FEDERATION__?: { __INSTANCES__?: ModuleFederation[] };
}
).__FEDERATION__;
const instances = federation?.__INSTANCES__ ?? [];
return instances.find((GMInstance: ModuleFederation) => {
if (buildId && GMInstance.options.id === buildId) {
return true;
}

if (
GMInstance.options.name === name &&
!GMInstance.options.version &&
!version
) {
return true;
}
if (
GMInstance.options.name === name &&
!GMInstance.options.version &&
!version
) {
return true;
}

if (
GMInstance.options.name === name &&
version &&
GMInstance.options.version === version
) {
return true;
}
return false;
},
);
if (
GMInstance.options.name === name &&
version &&
GMInstance.options.version === version
) {
return true;
}
return false;
});
}
9 changes: 7 additions & 2 deletions packages/runtime/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"rootDir": "./src",
"declaration": true,
"types": ["node"]
},
"include": ["src/**/*.ts", "global.d.ts"],
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"]
"exclude": [
"jest.config.ts",
"src/**/*.spec.ts",
"src/**/*.test.ts",
"dist/**"
]
}
Loading