Skip to content

chore: general housekeeping #4000

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 2 commits into from
Jun 3, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const Main = () => {
<h3>my env is {ENV.API_URL}</h3>
<p>
The Dynamic System will take advantage Module Federation <strong>remotes</strong> and{' '}
<strong>exposes</strong>. It will no load components that have been loaded already.
<strong>exposes</strong>. It will not load components that have been loaded already.
</p>
<button onClick={loadRemoteWidget}>Load Remote Widget</button>
<div style={{ marginTop: '2em' }}>
Expand Down
59 changes: 31 additions & 28 deletions advanced-api/dynamic-remotes/app1/src/App.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,59 @@
import React, { useState, useEffect, Suspense } from 'react';

import { init,loadRemote } from '@module-federation/runtime'
import { init, loadRemote } from '@module-federation/runtime';

init({
name: 'app1',
remotes: [
{
name:'app2',
entry: 'http://localhost:3002/remoteEntry.js'
name: 'app2',
entry: 'http://localhost:3002/remoteEntry.js',
},
{
name:'app3',
entry: 'http://localhost:3003/remoteEntry.js'
name: 'app3',
entry: 'http://localhost:3003/remoteEntry.js',
},
]
})

],
});

function useDynamicImport({module,scope}) {
console.log(module,scope)
function useDynamicImport({ module, scope }) {
const [component, setComponent] = useState(null);

useEffect(() => {
if(!module && !scope) return
if (!module || !scope) return;

const loadComponent = async () => {
const { default: component } = await loadRemote(`${scope}/${module}`);
setComponent(() => component);
try {
const { default: Component } = await loadRemote(`${scope}/${module}`);
setComponent(() => Component);
} catch (error) {
console.error(`Error loading remote module ${scope}/${module}:`, error);
}
};

loadComponent();
}, [module,scope]);
const fallback = ()=> null
return component || fallback
}, [module, scope]);

return component;
}

function App() {
const [{ module, scope }, setSystem] = React.useState({});
const [{ module, scope }, setSystem] = useState({});

function setApp2() {
const setApp2 = () => {
setSystem({
scope: 'app2',
module: 'Widget',
});
}
};

function setApp3() {
const setApp3 = () => {
setSystem({
scope: 'app3',
module: 'Widget',
});
}
};

const Component = useDynamicImport({module,scope});
const Component = useDynamicImport({ module, scope });

return (
<div
Expand All @@ -62,15 +65,15 @@ function App() {
<h1>Dynamic System Host</h1>
<h2>App 1</h2>
<p>
The Dynamic System will take advantage Module Federation <strong>remotes</strong> and{' '}
<strong>exposes</strong>. It will no load components that have been loaded already.
The Dynamic System will take advantage of Module Federation <strong>remotes</strong> and{' '}
<strong>exposes</strong>. It will not load components that have already been loaded.
</p>
<button onClick={setApp2}>Load App 2 Widget</button>
<button onClick={setApp3}>Load App 3 Widget</button>
<div style={{ marginTop: '2em' }}>
<React.Suspense fallback="Loading System">
<Component />
</React.Suspense>
<Suspense fallback="Loading System">
{Component ? <Component /> : null}
</Suspense>
</div>
</div>
);
Expand Down
4 changes: 2 additions & 2 deletions advanced-api/dynamic-remotes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"build": "pnpm --filter dynamic-remotes_app* --parallel build",
"serve": "pnpm --filter dynamic-remotes_app* --parallel serve",
"clean": "pnpm --filter dynamic-remotes_app* --parallel clean",
"e2e:ci": "pnpm start & wait-on http-get://localhost:3001/ && npx cypress run --config-file ../../cypress-e2e/config/cypress.config.ts --config '{\"supportFile\": \"../../cypress-e2e/support/e2e.ts\"}' --spec \"./e2e/*.cy.ts\" --browser=chrome",
"legacy:e2e:ci": "pnpm legacy:start & wait-on http-get://localhost:3001/ && npx cypress run --config-file ../../cypress-e2e/config/cypress.config.ts --config '{\"supportFile\": \"../../cypress-e2e/support/e2e.ts\"}' --spec \"./e2e/*.cy.ts\" --browser=chrome"
"e2e:ci": "pnpm start & sleep 1 && wait-on tcp:3001 && wait-on tcp:3002 && wait-on tcp:3003 && npx cypress run --config-file ../../cypress-e2e/config/cypress.config.ts --config '{\"supportFile\": \"../../cypress-e2e/support/e2e.ts\"}' --spec \"./e2e/*.cy.ts\" --browser=chrome",
"legacy:e2e:ci": "pnpm legacy:start & sleep 1 && wait-on tcp:3001 && wait-on tcp:3002 && wait-on tcp:3003 && npx cypress run --config-file ../../cypress-e2e/config/cypress.config.ts --config '{\"supportFile\": \"../../cypress-e2e/support/e2e.ts\"}' --spec \"./e2e/*.cy.ts\" --browser=chrome"
},
"devDependencies": {
"wait-on": "7.2.0"
Expand Down
4 changes: 2 additions & 2 deletions cypress-e2e/fixtures/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ export class Constants {
hostH3: 'my env is https://host.api.com',
button: 'Load Remote Widget'
},
paragraph: 'The Dynamic System will take advantage Module Federation remotes and exposes. It will no load components that have been loaded already.',
paragraph: 'The Dynamic System will take advantage Module Federation remotes and exposes. It will not load components that have been loaded already.',
buttonHeader: 'Remote Widget',
buttonH2: 'My env is ',
buttonParagraph: 'Using momentjs for format the date'
Expand Down Expand Up @@ -849,7 +849,7 @@ export class Constants {
}
},
dynamicRemotesApp: {
paragraphText: 'The Dynamic System will take advantage Module Federation remotes and exposes. It will no load components that have been loaded already.',
paragraphText: 'The Dynamic System will take advantage of Module Federation remotes and exposes. It will not load components that have already been loaded.',
widgetParagraphText: [
'Moment shouldn\'t download twice, the host has no moment.js',
'Using momentjs for format the date'
Expand Down
Loading