Skip to content

feat(modern-js-plugin): add server plugin to handle remote's SSR assets #3777

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 27, 2025
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
9 changes: 9 additions & 0 deletions .changeset/spotty-ducks-look.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'create-module-federation': patch
'@module-federation/devtools': patch
'@module-federation/modern-js': patch
'@module-federation/modernjsapp': patch
'@module-federation/cli': patch
---

feat(modern-js-plugin): add server plugin to handle remote's SSR assets
6 changes: 3 additions & 3 deletions apps/modernjs-ssr/dynamic-nested-remote/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@
],
"dependencies": {
"@babel/runtime": "7.26.0",
"@modern-js/runtime": "2.65.1",
"@modern-js/runtime": "2.67.6",
"@module-federation/modern-js": "workspace:*",
"antd": "4.24.15",
"react": "~18.3.1",
"react-dom": "~18.3.1"
},
"devDependencies": {
"@modern-js-app/eslint-config": "2.59.0",
"@modern-js/app-tools": "2.65.1",
"@modern-js/app-tools": "2.67.6",
"@modern-js/eslint-config": "2.59.0",
"@modern-js/tsconfig": "2.65.1",
"@modern-js/tsconfig": "2.67.6",
"@types/jest": "~29.5.0",
"@types/node": "~16.11.7",
"@types/react": "~18.2.0",
Expand Down
6 changes: 3 additions & 3 deletions apps/modernjs-ssr/dynamic-remote-new-version/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@
],
"dependencies": {
"@babel/runtime": "7.26.0",
"@modern-js/runtime": "2.65.1",
"@modern-js/runtime": "2.67.6",
"@module-federation/modern-js": "workspace:*",
"antd": "4.24.15",
"react": "~18.3.1",
"react-dom": "~18.3.1"
},
"devDependencies": {
"@modern-js-app/eslint-config": "2.59.0",
"@modern-js/app-tools": "2.65.1",
"@modern-js/app-tools": "2.67.6",
"@modern-js/eslint-config": "2.59.0",
"@modern-js/tsconfig": "2.65.1",
"@modern-js/tsconfig": "2.67.6",
"@types/jest": "~29.5.0",
"@types/node": "~16.11.7",
"@types/react": "~18.2.0",
Expand Down
6 changes: 3 additions & 3 deletions apps/modernjs-ssr/dynamic-remote/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@
],
"dependencies": {
"@babel/runtime": "7.26.0",
"@modern-js/runtime": "2.65.1",
"@modern-js/runtime": "2.67.6",
"@module-federation/modern-js": "workspace:*",
"antd": "4.24.15",
"react": "~18.3.1",
"react-dom": "~18.3.1"
},
"devDependencies": {
"@modern-js-app/eslint-config": "2.59.0",
"@modern-js/app-tools": "2.65.1",
"@modern-js/app-tools": "2.67.6",
"@modern-js/eslint-config": "2.59.0",
"@modern-js/tsconfig": "2.65.1",
"@modern-js/tsconfig": "2.67.6",
"@types/jest": "~29.5.0",
"@types/node": "~16.11.7",
"@types/react": "~18.2.0",
Expand Down
6 changes: 3 additions & 3 deletions apps/modernjs-ssr/host/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@
],
"dependencies": {
"@babel/runtime": "7.26.0",
"@modern-js/runtime": "2.65.1",
"@modern-js/runtime": "2.67.6",
"@module-federation/modern-js": "workspace:*",
"antd": "4.24.15",
"react": "~18.3.1",
"react-dom": "~18.3.1"
},
"devDependencies": {
"@modern-js-app/eslint-config": "2.59.0",
"@modern-js/app-tools": "2.65.1",
"@modern-js/app-tools": "2.67.6",
"@modern-js/eslint-config": "2.59.0",
"@modern-js/tsconfig": "2.65.1",
"@modern-js/tsconfig": "2.67.6",
"@types/jest": "~29.5.0",
"@types/node": "~16.11.7",
"@types/react": "~18.2.0",
Expand Down
6 changes: 3 additions & 3 deletions apps/modernjs-ssr/nested-remote/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@
],
"dependencies": {
"@babel/runtime": "7.26.0",
"@modern-js/runtime": "2.65.1",
"@modern-js/runtime": "2.67.6",
"@module-federation/modern-js": "workspace:*",
"antd": "4.24.15",
"react": "~18.3.1",
"react-dom": "~18.3.1"
},
"devDependencies": {
"@modern-js-app/eslint-config": "2.59.0",
"@modern-js/app-tools": "2.65.1",
"@modern-js/app-tools": "2.67.6",
"@modern-js/eslint-config": "2.59.0",
"@modern-js/tsconfig": "2.65.1",
"@modern-js/tsconfig": "2.67.6",
"@types/jest": "~29.5.0",
"@types/node": "~16.11.7",
"@types/react": "~18.2.0",
Expand Down
6 changes: 3 additions & 3 deletions apps/modernjs-ssr/remote-new-version/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@
],
"dependencies": {
"@babel/runtime": "7.26.0",
"@modern-js/runtime": "2.65.1",
"@modern-js/runtime": "2.67.6",
"@module-federation/modern-js": "workspace:*",
"antd": "4.24.15",
"react": "~18.3.1",
"react-dom": "~18.3.1"
},
"devDependencies": {
"@modern-js-app/eslint-config": "2.59.0",
"@modern-js/app-tools": "2.65.1",
"@modern-js/app-tools": "2.67.6",
"@modern-js/eslint-config": "2.59.0",
"@modern-js/tsconfig": "2.65.1",
"@modern-js/tsconfig": "2.67.6",
"@types/jest": "~29.5.0",
"@types/node": "~16.11.7",
"@types/react": "~18.2.0",
Expand Down
3 changes: 3 additions & 0 deletions apps/modernjs-ssr/remote/modern.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ export default defineConfig({
dev: {
port: 3051,
},
output: {
assetPrefix: 'http://localhost:8080',
},
runtime: {
router: true,
},
Expand Down
5 changes: 4 additions & 1 deletion apps/modernjs-ssr/remote/module-federation.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { createModuleFederationConfig } from '@module-federation/modern-js';
export default createModuleFederationConfig({
name: 'remote',
filename: 'remoteEntry.js',
manifest: {
filePath: 'static',
},
filename: 'static/remoteEntry.js',
exposes: {
'./Image': './src/components/Image.tsx',
'./Button': './src/components/Button.tsx',
Expand Down
6 changes: 3 additions & 3 deletions apps/modernjs-ssr/remote/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@
],
"dependencies": {
"@babel/runtime": "7.26.0",
"@modern-js/runtime": "2.65.1",
"@modern-js/runtime": "2.67.6",
"@module-federation/modern-js": "workspace:*",
"antd": "4.24.15",
"react": "~18.3.1",
"react-dom": "~18.3.1"
},
"devDependencies": {
"@modern-js-app/eslint-config": "2.59.0",
"@modern-js/app-tools": "2.65.1",
"@modern-js/app-tools": "2.67.6",
"@modern-js/eslint-config": "2.59.0",
"@modern-js/tsconfig": "2.65.1",
"@modern-js/tsconfig": "2.67.6",
"@types/jest": "~29.5.0",
"@types/node": "~16.11.7",
"@types/react": "~18.2.0",
Expand Down
6 changes: 3 additions & 3 deletions apps/modernjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@
],
"dependencies": {
"@babel/runtime": "7.26.0",
"@modern-js/runtime": "2.65.1",
"@modern-js/runtime": "2.67.6",
"@module-federation/enhanced": "workspace:*",
"react": "18.3.1",
"react-dom": "18.3.1"
},
"devDependencies": {
"@modern-js-app/eslint-config": "2.59.0",
"@modern-js/app-tools": "2.65.1",
"@modern-js/app-tools": "2.67.6",
"@modern-js/eslint-config": "2.59.0",
"@modern-js/tsconfig": "2.65.1",
"@modern-js/tsconfig": "2.67.6",
"@types/jest": "~29.5.0",
"@types/node": "~20.12.12",
"@types/react": "~18.2.0",
Expand Down
23 changes: 10 additions & 13 deletions packages/chrome-devtools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,16 @@
"build:storybook": "storybook build",
"storybook": "storybook dev -p 6006",
"reset": "npx rimraf ./**/node_modules",
"dev": "modern dev",
"build": "modern build && node postpack.js",
"build:debug": "DEBUG=true modern build && node postpack.js",
"dev": "modern-app dev",
"build": "modern-app build && node postpack.js",
"build:debug": "DEBUG=true modern-app build && node postpack.js",
"build:lib": "rm -rf dist && modern-module build -c modern.lib.config.ts",
"release": "npm publish --tag canary",
"test": "vitest run",
"test:e2e": "E2ETEST=true pnpm build && playwright test",
"test:e2e:ui": "E2ETEST=true pnpm build && playwright test --ui",
"start": "modern start",
"serve": "modern serve",
"new": "modern new",
"upgrade": "modern upgrade",
"start": "modern-app start",
"serve": "modern-app serve",
"postinstall": "bash install-deps.bash"
},
"files": [
Expand Down Expand Up @@ -57,7 +55,7 @@
},
"dependencies": {
"@arco-design/web-react": "^2.64.1",
"@modern-js/runtime": "2.65.1",
"@modern-js/runtime": "2.67.6",
"@module-federation/sdk": "workspace:*",
"ahooks": "^3.7.10",
"dagre": "^0.8.5",
Expand All @@ -67,12 +65,11 @@
},
"devDependencies": {
"@modern-js-app/eslint-config": "2.54.6",
"@modern-js/app-tools": "2.65.1",
"@modern-js/builder-webpack-provider": "2.46.1",
"@modern-js/app-tools": "2.67.6",
"@modern-js/eslint-config": "2.59.0",
"@modern-js/module-tools": "2.65.1",
"@modern-js/storybook": "2.65.1",
"@modern-js/tsconfig": "2.65.1",
"@modern-js/module-tools": "2.67.6",
"@modern-js/storybook": "2.67.6",
"@modern-js/tsconfig": "2.67.6",
"@module-federation/runtime": "workspace:*",
"@playwright/test": "1.49.1",
"@storybook/addon-essentials": "^8",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"@module-federation/dts-plugin": "workspace:*",
"commander": "11.1.0",
"chalk": "3.0.0",
"@modern-js/node-bundle-require": "2.65.1"
"@modern-js/node-bundle-require": "2.67.6"
},
"devDependencies": {
"@types/node": "~16.11.7"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
"pre-commit": "npx lint-staged"
},
"dependencies": {
"@modern-js/runtime": "^2.65.1",
"@modern-js/runtime": "^2.67.6",
"react": "^18.3.1",
"react-dom": "^18.3.1"
},
"devDependencies": {
"@modern-js/app-tools": "^2.65.1",
"@modern-js/tsconfig": "^2.65.1",
"@modern-js/app-tools": "^2.67.6",
"@modern-js/tsconfig": "^2.67.6",
"@biomejs/biome": "1.8.3",
"typescript": "~5.0.4",
"@types/jest": "~29.2.4",
Expand Down
22 changes: 15 additions & 7 deletions packages/modernjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@
"types": "./dist/types/cli/mfRuntimePlugins/inject-node-fetch.d.ts",
"import": "./dist/esm/cli/mfRuntimePlugins/inject-node-fetch.js",
"require": "./dist/esm/cli/mfRuntimePlugins/inject-node-fetch.js"
},
"./server": {
"types": "./dist/types/server/index.d.ts",
"default": "./dist/cjs/server/index.js"
}
},
"typesVersions": {
Expand All @@ -79,6 +83,9 @@
],
"inject-node-fetch": [
"./dist/types/cli/mfRuntimePlugins/inject-node-fetch.d.ts"
],
"server": [
"./dist/types/server/index.d.ts"
]
}
},
Expand All @@ -87,9 +94,10 @@
"author": "hanric <[email protected]>",
"license": "MIT",
"dependencies": {
"@modern-js/node-bundle-require": "2.65.1",
"@modern-js/node-bundle-require": "2.67.6",
"@module-federation/rsbuild-plugin": "workspace:*",
"@modern-js/utils": "2.65.1",
"fs-extra": "11.3.0",
"lru-cache": "11.1.0",
"@module-federation/enhanced": "workspace:*",
"@module-federation/node": "workspace:*",
"@module-federation/sdk": "workspace:*",
Expand All @@ -100,11 +108,11 @@
},
"devDependencies": {
"@rsbuild/core": "1.2.8",
"@modern-js/app-tools": "2.65.1",
"@modern-js/core": "2.65.1",
"@modern-js/module-tools": "2.65.1",
"@modern-js/runtime": "2.65.1",
"@modern-js/tsconfig": "2.65.1",
"@modern-js/app-tools": "2.67.6",
"@modern-js/server-runtime": "2.67.6",
"@modern-js/module-tools": "2.67.6",
"@modern-js/runtime": "2.67.6",
"@modern-js/tsconfig": "2.67.6",
"@module-federation/manifest": "workspace:*"
},
"peerDependencies": {
Expand Down
7 changes: 7 additions & 0 deletions packages/modernjs/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ export const moduleFederationPlugin = (
}
}
});

api._internalServerPlugins(({ plugins }) => {
plugins.push({
name: '@module-federation/modern-js/server',
});
return { plugins };
});
},
usePlugins: [
moduleFederationConfigPlugin(internalModernPluginOptions),
Expand Down
2 changes: 1 addition & 1 deletion packages/modernjs/src/cli/manifest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';
import { Stats, Manifest } from '@module-federation/sdk';
import { fs } from '@modern-js/utils';
import fs from 'fs-extra';
import { BundlerPlugin } from '../types';

function mergeStats(browserStats: Stats, nodeStats: Stats): Stats {
Expand Down
2 changes: 1 addition & 1 deletion packages/modernjs/src/cli/ssrPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path';
import { fs } from '@modern-js/utils';
import fs from 'fs-extra';
import { ModuleFederationPlugin } from '@module-federation/enhanced/webpack';
import { ModuleFederationPlugin as RspackModuleFederationPlugin } from '@module-federation/enhanced/rspack';
import UniverseEntryChunkTrackerPlugin from '@module-federation/node/universe-entry-chunk-tracker-plugin';
Expand Down
29 changes: 29 additions & 0 deletions packages/modernjs/src/server/fileCache.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { it, expect, describe, vi, beforeAll } from 'vitest';
import { FileCache } from './fileCache';

beforeAll(() => {
vi.mock('fs-extra', () => ({
default: {
pathExists: () => {
return true;
},
lstat: () => {
return {
mtimeMs: Date.now(),
size: 4,
};
},
readFile: () => {
return 'test';
},
},
}));
});

describe('modern serve static file cache', async () => {
it('should cache file', async () => {
const cache = new FileCache();
const result = await cache.getFile('test.txt');
expect(result?.content).toBe('test');
});
});
Loading