0.0.28 • Published 5 months ago

pro-module-federation v0.0.28

Weekly downloads
-
License
ISC
Repository
github
Last release
5 months ago

pro-module-federation

Install

npm install pro-module-federation

Usage

webpack.module.ts

import packageJson from './package.json';
import { ProMFConfig } from 'pro-module-federation/dist/plugin';

const { dependencies } = packageJson;

export const { ProMFEnvironment, ProMFOptions } = ProMFConfig({
    name: 'MY_APP',
    filename: 'myApp.js',
    dependencies,
    shared: ['react', 'react-dom', 'react-router-dom'],
    remotes: [
        {
            name: 'TestApp',
            modules: ['./TestApp'],
            urls: {
                LOCAL: 'http://localhost:3000/',
            },
            entry: 'testApp.js',
        },
    ],
    remoteConfigs: {
        TestApp: 'LOCAL',
    },
});

webpack.config.ts

...
import { container, EnvironmentPlugin } from 'webpack';
import { ProMFEnvironment, ProMFOptions } from './webpack.module';
const { ModuleFederationPlugin } = container;

...
    plugins: [
        ...
        new ModuleFederationPlugin(ProMFOptions),
        new EnvironmentPlugin(ProMFEnvironment),
    ],
...

index.tsx

import React from 'react';
import ReactDOM from 'react-dom';
import { ProMFProvider } from '../pro-module-federation';
import App from './App';

const element = document.getElementById('root');

ReactDOM.render(
    <ProMFProvider>
        <App />
    </ProMFProvider>,
    element,
);

MFComponent.tsx

import { ProMFComponent, getProMF } from 'pro-module-federation';
import { MFComponentProps } from 'pro-module-federation/dist/components/MFComponent';
import React from 'react';
import { ShareScopes } from 'pro-module-federation/dist/functions/getModule';

declare const __webpack_init_sharing__: (shareScope: string) => Promise<void>;
declare const __webpack_share_scopes__: { default: any };

const shareScopes: ShareScopes = async () => {
    await __webpack_init_sharing__('default');
    return __webpack_share_scopes__.default;
};

const MFComponent = <Props extends Record<string, unknown> = Record<string, unknown>>({
    module,
    props,
    scope,
    styles,
}: Pick<MFComponentProps<Props>, 'module' | 'scope' | 'styles' | 'props'>) => {
    return (
        <ProMFComponent<Props>
            module={module}
            scope={scope}
            props={props}
            styles={styles}
            shareScopes={shareScopes}
        />
    );
};

export const getMF = async ({ module, scope }: Pick<MFComponentProps<any>, 'module' | 'scope'>) =>
    getProMF({
        module,
        scope,
        shareScopes,
    });

export default MFComponent;

TestApp.tsx

import React from 'react';
import MFComponent from './MFComponent';

const TestApp = <Props extends Record<string, unknown> = Record<string, unknown>>({
    module,
    props,
}: {
    module: string;
    props?: Props;
}) => {
    return <MFComponent<Props> module={module} scope="TestApp" props={props} />;
};

export default TestApp;
0.0.27

5 months ago

0.0.28

5 months ago

0.0.20

9 months ago

0.0.21

9 months ago

0.0.22

9 months ago

0.0.23

9 months ago

0.0.24

9 months ago

0.0.25

9 months ago

0.0.17

9 months ago

0.0.18

9 months ago

0.0.19

9 months ago

0.0.26

9 months ago

0.0.15

1 year ago

0.0.14

1 year ago

0.0.13

1 year ago

0.0.12

1 year ago

0.0.11

1 year ago

0.0.10

1 year ago

0.0.9

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago