Skip to content

feat: genesis #1722

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 1 commit into from
Mar 10, 2022
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ This repository is to showcase examples of how Webpack 5's new Module Federation
- [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)
- [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
- [x] [Vue.js](./vue3-demo/README.md) — Simple host/remote (render function / sfc) example using Vue 3.0.
- [x] [Vue2 SSR](./genesis/README.md) — This example demonstrates module as a service

# Notes

Expand Down
12 changes: 12 additions & 0 deletions genesis/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Vue Genesis Example

[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)

- `ssr-mf-about` About service
- `ssr-mf-home` Home service

# Running Demo
Run `yarn && yarn dev` or `yarn && yarn build && yarn start`

- [localhost:3001](http://localhost:3001) (ssr-mf-home)
- [localhost:3002](http://localhost:3002/about) (ssr-mf-about)
5 changes: 5 additions & 0 deletions genesis/lerna.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"version": "0.0.0",
"npmClient": "yarn",
"useWorkspaces": true
}
18 changes: 18 additions & 0 deletions genesis/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"private": true,
"scripts": {
"dev": "concurrently --raw \"lerna run --scope=ssr-mf-about dev\" \"lerna run --scope=ssr-mf-home dev\"",
"build": "FORCE_COLOR=1 lerna run --scope=ssr-mf-about build && FORCE_COLOR=1 lerna run --scope=ssr-mf-home build",
"start": "concurrently --raw \"lerna run --scope=ssr-mf-about start\" \"lerna run --scope=ssr-mf-home start\""
},
"devDependencies": {
"concurrently": "7.0.0",
"lerna": "4.0.0"
},
"workspaces": {
"packages": [
"ssr-mf-about",
"ssr-mf-home"
]
}
}
9 changes: 9 additions & 0 deletions genesis/ssr-mf-about/genesis.build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Build } from '@fmfe/genesis-compiler';

import { ssr } from './genesis';

const start = () => {
const build = new Build(ssr);
return build.start();
};
start();
13 changes: 13 additions & 0 deletions genesis/ssr-mf-about/genesis.dev.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Watch } from '@fmfe/genesis-compiler';

import { app, ssr, startApp } from './genesis';

const start = async () => {
const watch = new Watch(ssr);
await watch.start();
const renderer = watch.renderer;
app.use(watch.devMiddleware);
app.use(watch.hotMiddleware);
startApp(renderer);
};
start();
15 changes: 15 additions & 0 deletions genesis/ssr-mf-about/genesis.prod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import express from 'express';

import { app, ssr, startApp } from './genesis';

const renderer = ssr.createRenderer();

app.use(
renderer.staticPublicPath,
express.static(renderer.staticDir, {
immutable: true,
maxAge: '31536000000'
})
);

startApp(renderer);
61 changes: 61 additions & 0 deletions genesis/ssr-mf-about/genesis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { MF, Renderer, SSR } from '@fmfe/genesis-core';
import express from 'express';
import path from 'path';

export const app = express();

export const ssr = new SSR({
name: 'ssr-mf-about',
build: {
extractCSS: false
}
});

export const mf = new MF(ssr, {
exposes: {
'./src/routes': './src/routes.ts'
},
remotes: [
{
name: 'ssr-mf-home',
clientOrigin: 'http://localhost:3001',
serverOrigin: 'http://localhost:3001'
}
],
shared: {
vue: {
singleton: true
},
'vue-router': {
singleton: true
},
'vue-meta': {
singleton: true
}
},
typesDir: path.resolve('./types/ssr-mf-about')
});

app.get(mf.manifestRoutePath, async (req, res, next) => {
const t = Number(req.query.t);
const maxAwait = 1000 * 60;
await mf.exposes.getManifest(t, maxAwait);
next();
});

export const startApp = (renderer: Renderer) => {
mf.remote.init(renderer);
mf.remote.polling();
app.get('/about', async (req, res, next) => {
try {
const result = await renderer.renderHtml({
req,
res
});
res.send(result.data);
} catch (e) {
next(e);
}
});
app.listen(3002, () => console.log(`http://localhost:3002`));
};
27 changes: 27 additions & 0 deletions genesis/ssr-mf-about/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "ssr-mf-about",
"version": "2.0.10",
"main": "index.js",
"license": "MIT",
"private": true,
"scripts": {
"dev": "genesis-ts-node --project=./tsconfig.node.json genesis.dev",
"build": "rm -rf dist types && npm run build:dts && npm run build:vue && npm run build:node",
"build:node": "NODE_ENV=production genesis-tsc --build ./tsconfig.node.json",
"build:vue": "NODE_ENV=production genesis-ts-node --project=./tsconfig.node.json genesis.build",
"build:dts": "genesis-vue-tsc --declaration --emitDeclarationOnly",
"type-check": "genesis-vue-tsc --noEmit",
"start": "NODE_ENV=production node dist/genesis.prod"
},
"devDependencies": {
"@fmfe/genesis-compiler": "2.0.10",
"@types/express": "^4.17.13",
"vue": "^2.6.14",
"vue-meta": "^2.4.0",
"vue-router": "^3.5.3"
},
"dependencies": {
"@fmfe/genesis-core": "2.0.10",
"express": "^4.17.2"
}
}
1 change: 1 addition & 0 deletions genesis/ssr-mf-about/src/entry-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from 'ssr-mf-home/src/common/create-app-client';
5 changes: 5 additions & 0 deletions genesis/ssr-mf-about/src/entry-server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { createApp } from 'ssr-mf-home/src/common/create-app';

import { routes } from './routes';

export default createApp(routes);
26 changes: 26 additions & 0 deletions genesis/ssr-mf-about/src/images/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions genesis/ssr-mf-about/src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>

<head>
<%- meta %>
<%- title %>
<%- style %>
</head>

<body>
<%-html %>
<%- scriptState %>
<%- script %>
</body>

</html>
8 changes: 8 additions & 0 deletions genesis/ssr-mf-about/src/routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { RouteConfig } from 'vue-router';

export const routes: RouteConfig[] = [
{
path: '/about',
component: () => import('./views/about.vue').then((m) => m.default)
}
];
4 changes: 4 additions & 0 deletions genesis/ssr-mf-about/src/shims-vue.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module '*.vue' {
import Vue from 'vue';
export default Vue;
}
19 changes: 19 additions & 0 deletions genesis/ssr-mf-about/src/views/about.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<template>
<div>
<h2>ssr-mf-about</h2>
About <br />
<img src="../images/logo.svg" width="60" height="60" alt="logo" />
</div>
</template>
<script lang="ts">
import Vue from 'vue';

export default Vue.extend({
name: 'about',
metaInfo() {
return {
title: 'About'
};
}
});
</script>
28 changes: 28 additions & 0 deletions genesis/ssr-mf-about/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"moduleResolution": "node",
"esModuleInterop": true,
"experimentalDecorators": true,
"allowJs": true,
"sourceMap": true,
"strict": true,
"noEmit": false,
"noUnusedLocals": true,
"skipLibCheck": true,
"noImplicitAny": false,
"resolveJsonModule": true,
"baseUrl": "./",
"declaration": true,
"declarationDir": "./types",
"types": [
"@types/node"
],
"allowSyntheticDefaultImports": true
},
"exclude": [
"dist",
"types"
]
}
20 changes: 20 additions & 0 deletions genesis/ssr-mf-about/tsconfig.node.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"sourceMap": false,
"noEmit": false,
"target": "ES2018",
"module": "CommonJS",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"declaration": false,
"declarationDir": null,
"esModuleInterop": true,
"outDir": "./dist"
},
"exclude": [
"dist",
"types",
"src"
]
}
9 changes: 9 additions & 0 deletions genesis/ssr-mf-home/genesis.build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Build } from '@fmfe/genesis-compiler';

import { ssr } from './genesis';

const start = () => {
const build = new Build(ssr);
return build.start();
};
start();
13 changes: 13 additions & 0 deletions genesis/ssr-mf-home/genesis.dev.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Watch } from '@fmfe/genesis-compiler';

import { app, ssr, startApp } from './genesis';

const start = async () => {
const watch = new Watch(ssr);
await watch.start();
const renderer = watch.renderer;
app.use(watch.devMiddleware);
app.use(watch.hotMiddleware);
startApp(renderer);
};
start();
15 changes: 15 additions & 0 deletions genesis/ssr-mf-home/genesis.prod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import express from 'express';

import { app, ssr, startApp } from './genesis';

const renderer = ssr.createRenderer();

app.use(
renderer.staticPublicPath,
express.static(renderer.staticDir, {
immutable: true,
maxAge: '31536000000'
})
);

startApp(renderer);
Loading