Skip to content

Commit

Permalink
feat: support interface data prefetch (#2543)
Browse files Browse the repository at this point in the history
  • Loading branch information
nyqykk authored Sep 19, 2024
1 parent 13975e1 commit 3070e26
Show file tree
Hide file tree
Showing 68 changed files with 26,661 additions and 20,727 deletions.
5 changes: 5 additions & 0 deletions .changeset/light-items-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@module-federation/data-prefetch': patch
---

feat(@module-federation/data-prefetch): support data prefetch in Module Federation
2 changes: 1 addition & 1 deletion .github/workflows/e2e-manifest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ jobs:

- name: E2E Test for Manifest Demo
if: steps.check-ci.outcome == 'success'
run: pnpm run app:manifest:dev & echo "done" && npx wait-on tcp:3009 && npx wait-on tcp:3012 && npx nx run-many --target=e2e --projects=manifest-webpack-host --parallel=1 && npx kill-port 3013 3009 3010 3011 3012
run: pnpm run app:manifest:dev & echo "done" && npx wait-on tcp:3009 && npx wait-on tcp:3012 && npx wait-on http://127.0.0.1:4001/ && npx nx run-many --target=e2e --projects=manifest-webpack-host --parallel=1 && npx kill-port 3013 3009 3010 3011 3012 4001
12 changes: 11 additions & 1 deletion apps/manifest-demo/3009-webpack-provider/tsconfig.app.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
{
"extends": "./tsconfig.json",
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"jsx": "react-jsx",
"allowJs": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"outDir": "../../../dist/out-tsc",
"types": [
"node",
Expand Down
31 changes: 24 additions & 7 deletions apps/manifest-demo/3009-webpack-provider/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,29 @@
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true
"noFallthroughCasesInSwitch": true,
"outDir": "../../../dist/out-tsc",
"types": [
"node",
"@nx/react/typings/cssmodule.d.ts",
"@nx/react/typings/image.d.ts"
]
},
"files": [],
"references": [
{
"path": "./tsconfig.app.json"
}
]
"files": [
"../../../node_modules/@nx/react/typings/cssmodule.d.ts",
"../../../node_modules/@nx/react/typings/image.d.ts"
],
"exclude": [
"jest.config.ts",
"**/*.spec.ts",
"**/*.test.ts",
"**/*.spec.tsx",
"**/*.test.tsx",
"**/*.spec.js",
"**/*.test.js",
"**/*.spec.jsx",
"**/*.test.jsx",
"dist/**"
],
"include": ["**/*.js", "**/*.jsx", "**/*.ts", "**/*.tsx"]
}
3 changes: 2 additions & 1 deletion apps/manifest-demo/3010-rspack-provider/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"@rspack/plugin-react-refresh": "0.5.9"
},
"dependencies": {
"antd": "4.24.15"
"antd": "4.24.15",
"react-router-dom": "^6.23.1"
}
}
1 change: 1 addition & 0 deletions apps/manifest-demo/3010-rspack-provider/rspack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ module.exports = composePlugins(
'react-dom': {},
'react-dom/': {},
},
dataPrefetch: true,
}),
);
(config.devServer = {
Expand Down
2 changes: 2 additions & 0 deletions apps/manifest-demo/3010-rspack-provider/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import LocalButton from './Button';
import { Await } from 'react-router-dom';
console.log(Await);

const App = () => (
<div>
Expand Down
3 changes: 3 additions & 0 deletions apps/manifest-demo/webpack-host/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import React, { Suspense, lazy } from 'react';
// @ts-ignore
import ReactComponent from 'modern-js-provider/react-component';
import TestRemoteHook from './test-remote-hook';
import { loadRemote } from '@module-federation/runtime';
import LocalBtn from './components/ButtonOldAnt';
Expand Down Expand Up @@ -29,6 +31,7 @@ const WebpackPngRemote = lazy(() => import('remote1/WebpackPng'));

const App = () => (
<div>
<ReactComponent />
<h2>Manifest Basic Usage</h2>
<h3>check static remote</h3>
<table border={1} cellPadding={5}>
Expand Down
1 change: 1 addition & 0 deletions apps/manifest-demo/webpack-host/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module.exports = composePlugins(withNx(), withReact(), (config, context) => {
'rspack_manifest_provider@http://localhost:3011/mf-manifest.json',
'js-entry-provider':
'rspack_js_entry_provider@http://localhost:3012/remoteEntry.js',
'modern-js-provider': 'app1@http://127.0.0.1:4001/mf-manifest.json',
},
filename: 'remoteEntry.js',
exposes: {
Expand Down
2 changes: 1 addition & 1 deletion apps/modernjs/cypress/e2e/app.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ describe('modernjs/', () => {

describe('Welcome message', () => {
it('should display welcome message', () => {
cy.get('.title').contains('Welcome');
cy.get('.container-box').contains('Resend request with parameters');
});
});
});
5 changes: 4 additions & 1 deletion apps/modernjs/modern.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ export default defineConfig({
},
webpack: (config, { webpack, appendPlugins }) => {
if (config?.output) {
config.output.publicPath = 'http://localhost:4001/';
config.output.publicPath = 'http://127.0.0.1:4001/';
config.output.uniqueName = 'modern-js-app1';
}

appendPlugins([
Expand All @@ -44,12 +45,14 @@ export default defineConfig({
name: 'app1',
exposes: {
'./thing': './src/test.ts',
'./react-component': './src/components/react-component.tsx',
},
runtimePlugins: ['./runtimePlugin.ts'],
shared: {
react: { singleton: true },
'react-dom': { singleton: true },
},
dataPrefetch: true,
}),
]);
},
Expand Down
19 changes: 19 additions & 0 deletions apps/modernjs/src/components/react-component.prefetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { defer } from '@modern-js/runtime/router';
import React from 'react';

console.log(React);
const defaultVal = {
data: {
id: 1,
title: 'A Prefetch Title',
},
};

export default (params = defaultVal) =>
defer({
userInfo: new Promise(resolve => {
setTimeout(() => {
resolve(params);
}, 2000);
}),
});
44 changes: 44 additions & 0 deletions apps/modernjs/src/components/react-component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Suspense } from 'react';
import { usePrefetch } from '@module-federation/enhanced/prefetch';
import { Await } from '@modern-js/runtime/router';

interface UserInfo {
id: number;
title: string;
}
const reFetchParams = {
data: {
id: 2,
title: 'Another Prefetch Title',
},
};

const ReactComponent = () => {
const [prefetchResult, reFetchUserInfo] = usePrefetch<UserInfo>({
id: 'app1/react-component',
// Optional parameters, required after using defer
deferId: 'userInfo',
});

return (
<>
<button onClick={() => reFetchUserInfo(reFetchParams)}>
Resend request with parameters
</button>
<Suspense fallback={<p>Loading...</p>}>
<Await
resolve={prefetchResult}
// eslint-disable-next-line react/no-children-prop
children={userInfo => (
<div>
<div>{userInfo.data.id}</div>
<div>{userInfo.data.title}</div>
</div>
)}
/>
</Suspense>
</>
);
};

export default ReactComponent;
2 changes: 2 additions & 0 deletions apps/modernjs/src/routes/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @ts-nocheck
import { Helmet } from '@modern-js/runtime/head';
import Component from '../components/react-component';
import './index.css';

const Index = () => (
Expand Down Expand Up @@ -87,6 +88,7 @@ const Index = () => (
</a>
</div>
</main>
<Component />
</div>
);

Expand Down
5 changes: 5 additions & 0 deletions apps/website-new/docs/en/guide/_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,10 @@
"type": "dir",
"name": "framework",
"label": "Frameworks"
},
{
"type": "dir",
"name": "performance",
"label": "Performance"
}
]
1 change: 1 addition & 0 deletions apps/website-new/docs/en/guide/performance/_meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["prefetch"]
Loading

0 comments on commit 3070e26

Please sign in to comment.