Skip to content

Commit

Permalink
wip: fix(plugin-mf): 🐛 re-export umi.ts exports for qiankun (#11109)
Browse files Browse the repository at this point in the history
* fix(plugin-mf): 🐛  re-export umi.ts exports for qiankun

* fix: 🐛 unit test

* fix: 🐛 change umi entry when in produciton (umi build)

* chore: 🙈 cypress videos snapshots

* test: ✅ add e2e for mf-host and qiankun-slave

* docs: 📝  文档更新

---------

Co-authored-by: pshu <pishu.spf@antfin.com>
  • Loading branch information
stormslowly and stormslowly authored May 9, 2023
1 parent bdc716d commit ae7fc7d
Show file tree
Hide file tree
Showing 15 changed files with 98 additions and 21 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,5 @@
/packages/umi/client/**/*.d.ts
/packages/umi/client/**/*.d.ts.map
/packages/bundler-vite/src/fixtures/alias/node_modules
**/cypress/screenshots/
**/cypress/videos/
4 changes: 4 additions & 0 deletions docs/docs/max/mf.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { Tabbed, Message } from 'umi';

在 Umi 项目使用 Module Federation 功能。

<Message emoji="🚨" type="warning">
Module Federation 功能需要浏览器支持 `Top Level Await` 特性。在生产环境中使用请注意浏览器是否支持([浏览器支持情况](https://caniuse.com/?search=top%20level%20await))。
</Message>

## 配置

### 使用远端模块配置
Expand Down
3 changes: 0 additions & 3 deletions examples/mf-host/.umirc.prod.ts

This file was deleted.

7 changes: 7 additions & 0 deletions examples/mf-host/.umirc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ const shared = {
};

export default defineConfig({
publicPath: 'http://localhost:8000/',
qiankun: {
slave: {},
},
base: '/',
mf: {
name: 'hostUser',
remotes: [
Expand All @@ -31,4 +36,6 @@ export default defineConfig({
],
shared,
},
// dont use in production!!! just for tests
hash: false,
});
4 changes: 2 additions & 2 deletions examples/mf-host/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
"ci": "cypress run",
"cypress:ci": "pnpm umi-scripts cypress",
"cypress:open": "cypress open",
"dev": "max dev",
"dev": "cross-env PORT=8000 max dev",
"e2e": "cypress run",
"e2e:ci": "npm run e2e:ci:dev&&npm run e2e:ci:prod",
"e2e:ci:dev": "cross-env PORT=8000 start-server-and-test dev http://127.0.0.1:8000/__umi/api/status start-remote-dev http://127.0.0.1:9000/__umi/api/status cypress:ci",
"e2e:ci:local": "cross-env PORT=8000 start-server-and-test dev http://127.0.0.1:8000/__umi/api/status start-remote-dev http://127.0.0.1:9000/__umi/api/status cypress:open",
"e2e:ci:prod": "cross-env PORT=8000 start-server-and-test dev http://127.0.0.1:8000/__umi/api/status start-remote-prod 9000 cypress:ci",
"preview": "max preview --port 9527",
"preview": "max preview --port 8000",
"start": "npm run dev",
"start-remote-dev": "pnpm run --dir ../mf-remote dev",
"start-remote-prod": "pnpm run --dir ../mf-remote preview"
Expand Down
1 change: 1 addition & 0 deletions examples/mf-remote/.umirc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ export default defineConfig({
library: { type: 'window', name: moduleFederationName },
},
publicPath: 'http://127.0.0.1:9000/',
hash: false,
});
8 changes: 8 additions & 0 deletions examples/qiankun-master/.umirc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export default {
name: 'slave-app2',
entry: 'http://127.0.0.1:7002', // your slave app address
},
{
name: 'slave-mf',
entry: 'http://127.0.0.1:8000', // your slave app address
},
],
},
slave: {},
Expand All @@ -22,6 +26,10 @@ export default {
path: '/slave/*',
microApp: 'slave',
},
{
path: '/slave-mf/*',
microApp: 'slave-mf',
},
{
path: '/animal',
routes: [
Expand Down
4 changes: 2 additions & 2 deletions examples/qiankun-master/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"name": "@example/qiankun-master",
"private": true,
"scripts": {
"dev": "cross-env PORT=8888 max dev",
"preview": "max preview --port 8888",
"dev": "cross-env PORT=8889 max dev",
"preview": "max preview --port 8889",
"setup": "max setup",
"start": "npm run dev"
},
Expand Down
1 change: 1 addition & 0 deletions examples/qiankun-slave-app2/.umirc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export default {
{ path: '/hello', component: 'hello' },
],
runtimePublicPath: {},
hash: false,
};
2 changes: 2 additions & 0 deletions examples/qiankun-slave-app2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"name": "@example/qiankun-slave-app2",
"private": true,
"scripts": {
"build": "max build",
"dev": "max dev",
"preview": "max preview --port 7002",
"setup": "max setup",
"start": "npm run dev"
},
Expand Down
1 change: 1 addition & 0 deletions examples/qiankun-slave/.umirc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export default {
{ path: '/count', component: 'count' },
{ path: '/nav', component: 'nav' },
],
hash: false,
};
11 changes: 10 additions & 1 deletion examples/qiankun-slave/cypress/e2e/example.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe('QianKun Plugin', () => {
it('can navigate to slave', () => {
// contains https://docs.cypress.io/api/commands/contains
cy.visit('/home');
cy.get('button').click();
cy.get('a').click();

cy.contains('Slave Home Page');
});
Expand All @@ -20,6 +20,15 @@ describe('QianKun Plugin', () => {
cy.contains('count:1');
});

it('support app using module-federation', () => {
cy.visit('/slave-mf/dynamic-import');

cy.contains('remote Counter');
cy.contains('remote hooks counter10');
cy.get('[data-testid="remote-button"]').click();
cy.contains('remote hooks counter11');
});

describe('manual loaded app', function () {
it('be loaded', () => {
cy.visit('/manual-slave/home');
Expand Down
11 changes: 8 additions & 3 deletions examples/qiankun-slave/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
"private": true,
"scripts": {
"dev": "cross-env PORT=5555 max dev",
"e2e:ci": "cross-env-shell PORT=8888 start-test start:all-qiankun 8888 test:ci",
"e2e:local": "cross-env-shell PORT=8888 start-test start:all-qiankun 8888 test:local",
"e2e:ci": "npm run e2e:ci:dev",
"e2e:ci:dev": "cross-env PORT=8889 start-test start:all-qiankun:dev 8889 test:ci",
"e2e:ci:preview": "cross-env PORT=8889 start-test start:all-qiankun:preview 8889 test:ci:preview",
"e2e:local": "cross-env PORT=8889 start-test start:all-qiankun:dev 8889 test:local",
"e2e:local:preview": "cross-env-shell PORT=8889 start-test start:all-qiankun:preview 8889 test:local",
"preview": "cross-env PORT=5555 max preview --port 5555",
"setup": "max setup",
"start": "npm run dev",
"start:all-qiankun": "pnpm umi-scripts turbo --filter @example/qiankun-* dev",
"start:all-qiankun:dev": "pnpm umi-scripts turbo --filter @example/qiankun-* --filter @example/mf-* dev",
"start:all-qiankun:preview": "pnpm umi-scripts turbo --filter @example/qiankun-* --filter @example/mf-* preview",
"test:ci": "pnpm umi-scripts cypress",
"test:local": "cypress open"
},
Expand Down
1 change: 1 addition & 0 deletions packages/plugins/src/mf.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ async function executePlugin(mfConfig: any) {
modifyWebpackConfig(fn: Function) {
modifier = fn;
},
register() {},
addRuntimePluginKey() {},
modifyDefaultConfig() {},
};
Expand Down
59 changes: 49 additions & 10 deletions packages/plugins/src/mf.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { existsSync, opendirSync } from 'fs';
import { parseModule } from '@umijs/bundler-utils';
import { existsSync, opendirSync, readFileSync } from 'fs';
import { join } from 'path';
import type { IApi } from 'umi';
import { lodash, winPath } from 'umi/plugin-utils';
Expand Down Expand Up @@ -52,7 +53,7 @@ export default function mf(api: IApi) {
}

if (!isEmpty(remotes)) {
if (!api.config.mfsu) {
if (api.env === 'production' || !api.config.mfsu) {
changeUmiEntry(config);
}
}
Expand Down Expand Up @@ -126,16 +127,54 @@ export default function mf(api: IApi) {
},
tplPath: winPath(join(MF_TEMPLATES_DIR, 'runtime.ts.tpl')),
});
});

if (api.env === 'development' && api.config.mfsu) {
// skip mfsu already dynamic import
return;
}
api.register({
key: 'onGenerateFiles',
// ensure after generate tpm files
stage: 10001,
fn: async () => {
if (api.env === 'development' && api.config.mfsu) {
// skip mfsu already dynamic import
return;
}

api.writeTmpFile({
content: `import('${winPath(join(api.paths.absTmpPath, 'umi.ts'))}')`,
path: mfAsyncEntryFileName,
});
const entry = join(api.paths.absTmpPath, 'umi.ts');
const content = readFileSync(
join(api.paths.absTmpPath, 'umi.ts'),
'utf-8',
);

const [_imports, exports] = await parseModule({ content, path: entry });

const mfEntryContent: string[] = [];
let hasDefaultExport = false;
if (exports.length) {
mfEntryContent.push(
`const umiExports = await import('${winPath(entry)}')`,
);
for (const exportName of exports) {
if (exportName === 'default') {
hasDefaultExport = true;
mfEntryContent.push(`export default umiExports.${exportName}`);
} else {
mfEntryContent.push(
`export const ${exportName} = umiExports.${exportName}`,
);
}
}
} else {
mfEntryContent.push(`import('${winPath(entry)}')`);
}
if (!hasDefaultExport) {
mfEntryContent.push('export default 1');
}

api.writeTmpFile({
content: mfEntryContent.join('\n'),
path: mfAsyncEntryFileName,
});
},
});

function formatRemotes() {
Expand Down

1 comment on commit ae7fc7d

@vercel
Copy link

@vercel vercel bot commented on ae7fc7d May 9, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.