0.2.2 • Published 5 months ago

flatpad v0.2.2

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

Flatpad

A micro frontend library for esm.

Flatpad has no framework restriction, which means you can use Vue, React and any others you like at the same time.

Usage

  1. Install
npm i flatpad
  1. Import & Start
// main.ts in main app
import { registerApplication, start } from 'flatpad';

// apps is sub app configurations
const apps = [
  {
    name: 'app1',
    entry: 'http://localhost:9091',
  },
  {
    name: 'app2',
    entry: 'http://localhost:9092',
  },
];

// register
registerApplication(apps);

// start
start();
  1. Config sub apps
// main.ts in sub app

function mount() {
  // ...
}

function unmount() {
  // ...
}

Enter script should add a entry attribute.

<!-- index.html in sub app -->
...
<script type="module" entry src="/src/main.ts"></script>
...

How to build main app?

Reference to /examples/main.

Use registerApplication to register sub apps. Then start.

How to build sub apps?

Reference to /examples/child.

Sub apps need to export mount and unmount lifecycle functions, which will be called in main app.

Note: sub apps should clear app states and datas when unmounting, which could release memory and stop side effects.

Sub app's mount and unmount:

// for vue app

let app: App | null = null;
let router = null;
let history: RouterHistory | null = null;

export function mount() {
  history = createWebHistory();
  router = createRouter({ history, routes });
  app = createApp(Main);

  app.use(router).mount('#app');
}

export function unmount() {
  app?.unmount();
  history?.destroy();

  app = null;
  router = null;
  history = null;
}

Notice

Flatpad has below disadvantages:

  • no sandbox - Sandbox realized by with and eval is not suitable for esm.
  • only support single instance - For lack of sandbox, routers may produce unexpected effects when there are multiple instances.

License

MIT

0.2.2

5 months ago

0.2.1

5 months ago

0.2.0

5 months ago

0.1.0

2 years ago

0.1.2

1 year ago

0.1.1

1 year ago

0.1.3

1 year ago

0.0.2

2 years ago

0.0.1

2 years ago