Skip to content

Commit 2854506

Browse files
authored
feat: genesis (#1722)
1 parent 139cd93 commit 2854506

36 files changed

+608
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ This repository is to showcase examples of how Webpack 5's new Module Federation
6161
- [x] [NextJS SSR](./nextjs-ssr/README.md) — Powered by software streams, with [nextjs-ssr](https://github.com/module-federation/module-federation-examples/tree/master/nextjs-ssr) (currently in closed beta testing)
6262
- [x] [Building A Plugin-based Workflow Designer With Angular and Module Federation](https://github.com/manfredsteyer/module-federation-with-angular-dynamic-workflow-designer) — External Example
6363
- [x] [Vue.js](./vue3-demo/README.md) — Simple host/remote (render function / sfc) example using Vue 3.0.
64+
- [x] [Vue2 SSR](./genesis/README.md) — This example demonstrates module as a service
6465

6566
# Notes
6667

genesis/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Vue Genesis Example
2+
3+
[This example demonstrates module as a service](https://github.com/fmfe/genesis/blob/master/docs/zh-CN/why.md#%E4%BB%80%E4%B9%88%E6%98%AF%E6%A8%A1%E5%9D%97%E5%8D%B3%E6%9C%8D%E5%8A%A1)
4+
5+
- `ssr-mf-about` About service
6+
- `ssr-mf-home` Home service
7+
8+
# Running Demo
9+
Run `yarn && yarn dev` or `yarn && yarn build && yarn start`
10+
11+
- [localhost:3001](http://localhost:3001) (ssr-mf-home)
12+
- [localhost:3002](http://localhost:3002/about) (ssr-mf-about)

genesis/lerna.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"version": "0.0.0",
3+
"npmClient": "yarn",
4+
"useWorkspaces": true
5+
}

genesis/package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"private": true,
3+
"scripts": {
4+
"dev": "concurrently --raw \"lerna run --scope=ssr-mf-about dev\" \"lerna run --scope=ssr-mf-home dev\"",
5+
"build": "FORCE_COLOR=1 lerna run --scope=ssr-mf-about build && FORCE_COLOR=1 lerna run --scope=ssr-mf-home build",
6+
"start": "concurrently --raw \"lerna run --scope=ssr-mf-about start\" \"lerna run --scope=ssr-mf-home start\""
7+
},
8+
"devDependencies": {
9+
"concurrently": "7.0.0",
10+
"lerna": "4.0.0"
11+
},
12+
"workspaces": {
13+
"packages": [
14+
"ssr-mf-about",
15+
"ssr-mf-home"
16+
]
17+
}
18+
}

genesis/ssr-mf-about/genesis.build.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Build } from '@fmfe/genesis-compiler';
2+
3+
import { ssr } from './genesis';
4+
5+
const start = () => {
6+
const build = new Build(ssr);
7+
return build.start();
8+
};
9+
start();

genesis/ssr-mf-about/genesis.dev.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Watch } from '@fmfe/genesis-compiler';
2+
3+
import { app, ssr, startApp } from './genesis';
4+
5+
const start = async () => {
6+
const watch = new Watch(ssr);
7+
await watch.start();
8+
const renderer = watch.renderer;
9+
app.use(watch.devMiddleware);
10+
app.use(watch.hotMiddleware);
11+
startApp(renderer);
12+
};
13+
start();

genesis/ssr-mf-about/genesis.prod.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import express from 'express';
2+
3+
import { app, ssr, startApp } from './genesis';
4+
5+
const renderer = ssr.createRenderer();
6+
7+
app.use(
8+
renderer.staticPublicPath,
9+
express.static(renderer.staticDir, {
10+
immutable: true,
11+
maxAge: '31536000000'
12+
})
13+
);
14+
15+
startApp(renderer);

genesis/ssr-mf-about/genesis.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { MF, Renderer, SSR } from '@fmfe/genesis-core';
2+
import express from 'express';
3+
import path from 'path';
4+
5+
export const app = express();
6+
7+
export const ssr = new SSR({
8+
name: 'ssr-mf-about',
9+
build: {
10+
extractCSS: false
11+
}
12+
});
13+
14+
export const mf = new MF(ssr, {
15+
exposes: {
16+
'./src/routes': './src/routes.ts'
17+
},
18+
remotes: [
19+
{
20+
name: 'ssr-mf-home',
21+
clientOrigin: 'http://localhost:3001',
22+
serverOrigin: 'http://localhost:3001'
23+
}
24+
],
25+
shared: {
26+
vue: {
27+
singleton: true
28+
},
29+
'vue-router': {
30+
singleton: true
31+
},
32+
'vue-meta': {
33+
singleton: true
34+
}
35+
},
36+
typesDir: path.resolve('./types/ssr-mf-about')
37+
});
38+
39+
app.get(mf.manifestRoutePath, async (req, res, next) => {
40+
const t = Number(req.query.t);
41+
const maxAwait = 1000 * 60;
42+
await mf.exposes.getManifest(t, maxAwait);
43+
next();
44+
});
45+
46+
export const startApp = (renderer: Renderer) => {
47+
mf.remote.init(renderer);
48+
mf.remote.polling();
49+
app.get('/about', async (req, res, next) => {
50+
try {
51+
const result = await renderer.renderHtml({
52+
req,
53+
res
54+
});
55+
res.send(result.data);
56+
} catch (e) {
57+
next(e);
58+
}
59+
});
60+
app.listen(3002, () => console.log(`http://localhost:3002`));
61+
};

genesis/ssr-mf-about/package.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "ssr-mf-about",
3+
"version": "2.0.10",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"private": true,
7+
"scripts": {
8+
"dev": "genesis-ts-node --project=./tsconfig.node.json genesis.dev",
9+
"build": "rm -rf dist types && npm run build:dts && npm run build:vue && npm run build:node",
10+
"build:node": "NODE_ENV=production genesis-tsc --build ./tsconfig.node.json",
11+
"build:vue": "NODE_ENV=production genesis-ts-node --project=./tsconfig.node.json genesis.build",
12+
"build:dts": "genesis-vue-tsc --declaration --emitDeclarationOnly",
13+
"type-check": "genesis-vue-tsc --noEmit",
14+
"start": "NODE_ENV=production node dist/genesis.prod"
15+
},
16+
"devDependencies": {
17+
"@fmfe/genesis-compiler": "2.0.10",
18+
"@types/express": "^4.17.13",
19+
"vue": "^2.6.14",
20+
"vue-meta": "^2.4.0",
21+
"vue-router": "^3.5.3"
22+
},
23+
"dependencies": {
24+
"@fmfe/genesis-core": "2.0.10",
25+
"express": "^4.17.2"
26+
}
27+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from 'ssr-mf-home/src/common/create-app-client';
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { createApp } from 'ssr-mf-home/src/common/create-app';
2+
3+
import { routes } from './routes';
4+
5+
export default createApp(routes);
Lines changed: 26 additions & 0 deletions
Loading

genesis/ssr-mf-about/src/index.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<%- meta %>
6+
<%- title %>
7+
<%- style %>
8+
</head>
9+
10+
<body>
11+
<%-html %>
12+
<%- scriptState %>
13+
<%- script %>
14+
</body>
15+
16+
</html>

genesis/ssr-mf-about/src/routes.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { RouteConfig } from 'vue-router';
2+
3+
export const routes: RouteConfig[] = [
4+
{
5+
path: '/about',
6+
component: () => import('./views/about.vue').then((m) => m.default)
7+
}
8+
];
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
declare module '*.vue' {
2+
import Vue from 'vue';
3+
export default Vue;
4+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<template>
2+
<div>
3+
<h2>ssr-mf-about</h2>
4+
About <br />
5+
<img src="../images/logo.svg" width="60" height="60" alt="logo" />
6+
</div>
7+
</template>
8+
<script lang="ts">
9+
import Vue from 'vue';
10+
11+
export default Vue.extend({
12+
name: 'about',
13+
metaInfo() {
14+
return {
15+
title: 'About'
16+
};
17+
}
18+
});
19+
</script>

genesis/ssr-mf-about/tsconfig.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"compilerOptions": {
3+
"target": "esnext",
4+
"module": "esnext",
5+
"moduleResolution": "node",
6+
"esModuleInterop": true,
7+
"experimentalDecorators": true,
8+
"allowJs": true,
9+
"sourceMap": true,
10+
"strict": true,
11+
"noEmit": false,
12+
"noUnusedLocals": true,
13+
"skipLibCheck": true,
14+
"noImplicitAny": false,
15+
"resolveJsonModule": true,
16+
"baseUrl": "./",
17+
"declaration": true,
18+
"declarationDir": "./types",
19+
"types": [
20+
"@types/node"
21+
],
22+
"allowSyntheticDefaultImports": true
23+
},
24+
"exclude": [
25+
"dist",
26+
"types"
27+
]
28+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"sourceMap": false,
5+
"noEmit": false,
6+
"target": "ES2018",
7+
"module": "CommonJS",
8+
"moduleResolution": "node",
9+
"allowSyntheticDefaultImports": true,
10+
"declaration": false,
11+
"declarationDir": null,
12+
"esModuleInterop": true,
13+
"outDir": "./dist"
14+
},
15+
"exclude": [
16+
"dist",
17+
"types",
18+
"src"
19+
]
20+
}

genesis/ssr-mf-home/genesis.build.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Build } from '@fmfe/genesis-compiler';
2+
3+
import { ssr } from './genesis';
4+
5+
const start = () => {
6+
const build = new Build(ssr);
7+
return build.start();
8+
};
9+
start();

genesis/ssr-mf-home/genesis.dev.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Watch } from '@fmfe/genesis-compiler';
2+
3+
import { app, ssr, startApp } from './genesis';
4+
5+
const start = async () => {
6+
const watch = new Watch(ssr);
7+
await watch.start();
8+
const renderer = watch.renderer;
9+
app.use(watch.devMiddleware);
10+
app.use(watch.hotMiddleware);
11+
startApp(renderer);
12+
};
13+
start();

genesis/ssr-mf-home/genesis.prod.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import express from 'express';
2+
3+
import { app, ssr, startApp } from './genesis';
4+
5+
const renderer = ssr.createRenderer();
6+
7+
app.use(
8+
renderer.staticPublicPath,
9+
express.static(renderer.staticDir, {
10+
immutable: true,
11+
maxAge: '31536000000'
12+
})
13+
);
14+
15+
startApp(renderer);

0 commit comments

Comments
 (0)