Skip to content
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

feat: SSR in mode vite ssr #5045

Merged
merged 27 commits into from
Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/basic-ssr/src/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ export default {
});
},
}),
};
};
2 changes: 1 addition & 1 deletion examples/basic-ssr/src/store.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createStore } from 'ice';
import user from './models/user';

export default createStore({ user });
export default createStore({ user });
4 changes: 4 additions & 0 deletions examples/basic-vite-ssr/build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"ssr": true,
"vite": true
}
17 changes: 17 additions & 0 deletions examples/basic-vite-ssr/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "basic-vite-ssr",
"version": "0.0.1",
"scripts": {
"start": "../../packages/icejs/bin/ice-cli.js start",
"build": "../../packages/icejs/bin/ice-cli.js build"
},
"dependencies": {
"react": "^17.0.0",
"react-dom": "^17.0.0"
},
"devDependencies": {
"@types/react": "^17.0.0",
"@types/react-dom": "^17.0.0"
},
"license": "MIT"
}
11 changes: 11 additions & 0 deletions examples/basic-vite-ssr/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite App</title>
</head>
<body>
<div id="ice-container"></div>
</body>
</html>
8 changes: 8 additions & 0 deletions examples/basic-vite-ssr/src/Layout/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default ({ children }) => {
return (
<div>
Layout
{children}
</div>
);
};
13 changes: 13 additions & 0 deletions examples/basic-vite-ssr/src/app.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { runApp, IAppConfig } from 'ice';

const appConfig: IAppConfig = {
app: {
rootId: 'ice-container',
errorBoundary: true,
},
router: {
type: 'browser',
},
};

runApp(appConfig);
11 changes: 11 additions & 0 deletions examples/basic-vite-ssr/src/global.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu',
'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}

code {
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', monospace;
}
5 changes: 5 additions & 0 deletions examples/basic-vite-ssr/src/pages/Dashboard/Page1/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default () => {
return (
<div>Page1</div>
);
};
5 changes: 5 additions & 0 deletions examples/basic-vite-ssr/src/pages/Dashboard/Page2/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default () => {
return (
<div>Page2</div>
);
};
8 changes: 8 additions & 0 deletions examples/basic-vite-ssr/src/pages/Dashboard/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default ({ children }) => {
return (
<div>
这是 Dashboard
{children}
</div>
);
};
75 changes: 75 additions & 0 deletions examples/basic-vite-ssr/src/pages/Home/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
:root {
--primay: black;
--bg-primay: white;

--link: #2a0b0b;
}

.App {
text-align: center;
}

.App-logo {
height: 40vmin;
pointer-events: none;
}

p {
margin: 20px 0px;
}

.header {
font-size: 3rem;
}

.body {
margin: 20px 0 10px;
font-size: 0.9rem;
}

button {
outline: none;
border: none;
border-radius: 8px;
padding: 10px 35px;

background: #845ec2;
color: white;
}

@media (prefers-reduced-motion: no-preference) {
.App-logo {
animation: App-logo-spin infinite 20s linear;
}
}

.App-header {
background-color: #282c34;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);

background: var(--bg-primay);
color: var(--primay);
}

.App-link {
color: #61dafb;
color: var(--link);
}

@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}

button {
font-size: calc(10px + 2vmin);
}
39 changes: 39 additions & 0 deletions examples/basic-vite-ssr/src/pages/Home/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { useState } from 'react';
import './index.css';

function App() {
const [count, setCount] = useState<number>(0);

return (
<div className="App">
<header className="App-header">
<div className="body">
<button type="button" onClick={() => setCount((e) => e + 1)}>
🪂 Click me : {count}
</button>

<p> Don&apos;t forget to install <a href="https://appworks.site/">AppWorks</a> in Your Vscode.</p>
<p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer">
Learn React
</a>
{' | '}
<a
className="App-link"
href="https://vitejs.dev/guide/features.html"
target="_blank"
rel="noopener noreferrer">
Vite Docs
</a>
</p>
</div>
</header>
</div>
);
}

export default App;
39 changes: 39 additions & 0 deletions examples/basic-vite-ssr/src/routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { IRouterConfig } from 'ice';
import DashboardLayout from '@/pages/Dashboard';
import Dashboard1 from '@/pages/Dashboard/Page1';
import Dashboard2 from '@/pages/Dashboard/Page2';
import Home from '@/pages/Home';
import Layout from '@/Layout';

const routes: IRouterConfig[] = [
{
path: '/',
component: Home,
exact: true
},
{
path: '/',
component: Layout,
children: [
{
path: '/dashboard',
component: DashboardLayout,
children: [
{
path: '/a',
component: Dashboard1,
exact: true
},
{
path: '/b',
component: Dashboard2,
exact: true
}
]
},
]
},

];

export default routes;
4 changes: 4 additions & 0 deletions examples/basic-vite-ssr/src/typings.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module '*.module.scss' {
const classes: { [key: string]: string };
export default classes;
}
44 changes: 44 additions & 0 deletions examples/basic-vite-ssr/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"compileOnSave": false,
"buildOnSave": false,
"compilerOptions": {
"baseUrl": ".",
"outDir": "build",
"module": "esnext",
"target": "es6",
"jsx": "react-jsx",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"lib": [
"es6",
"dom"
],
"sourceMap": true,
"allowJs": true,
"rootDir": "./",
"forceConsistentCasingInFileNames": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noImplicitAny": false,
"importHelpers": true,
"strictNullChecks": true,
"suppressImplicitAnyIndexErrors": true,
"noUnusedLocals": true,
"skipLibCheck": true,
"types": ["node", "jest", "vite/client"],
"paths": {
"@/*": [
"./src/*"
],
"ice": [
".ice/index.ts"
],
"ice/*": [
".ice/pages/*"
],
"$store": [
"src/store1.ts"
]
Copy link
Collaborator

Choose a reason for hiding this comment

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

后面两个多余的

}
}
}
4 changes: 4 additions & 0 deletions packages/build-app-templates/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.1.1

- [feat] support server check by `global.__IS_SERVER__`

## 1.1.0

- [chore] use `enableRouter` replace `buildConfig.router`
Expand Down
4 changes: 2 additions & 2 deletions packages/build-app-templates/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@builder/app-templates",
"version": "1.1.0",
"version": "1.1.1",
"description": "App templates for ice.js and rax-app",
"author": "ice-admin@alibaba-inc.com",
"homepage": "",
Expand All @@ -10,7 +10,7 @@
"lib": "lib"
},
"dependencies": {
"create-app-shared": "^1.2.0"
"create-app-shared": "^1.2.2"
},
"files": [
"lib",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export function runApp(appConfig?: IAppConfig) {
// set History before GID
initHistory && initHistory(appConfig as any);
<% } %>
if (process.env.__IS_SERVER__) return;
if ((typeof global !== 'undefined' && (global as any).__IS_SERVER__) || process.env.__IS_SERVER__) return;
Copy link
Collaborator

Choose a reason for hiding this comment

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

为什么不复用 process.env.__IS_SERVER__

Copy link
Collaborator

Choose a reason for hiding this comment

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

是不是全部统一到运行时判断:globalThis.IS_SERVER

reactAppRenderer({
appConfig: appConfig as RenderAppConfig,
buildConfig,
Expand Down
4 changes: 4 additions & 0 deletions packages/create-app-shared/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.2.2

- [feat] support server check by `global.__IS_SERVER__`

## 1.2.1

- [fix] miniapp exports field
Expand Down
6 changes: 3 additions & 3 deletions packages/create-app-shared/package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "create-app-shared",
"version": "1.2.1",
"version": "1.2.2",
"description": "",
"author": "ice-admin@alibaba-inc.com",
"homepage": "https://github.com/alibaba/ice#readme",
"license": "MIT",
"main": "lib/index.js",
"dependencies": {
"history": "^4.9.0",
"miniapp-history": "^0.1.0",
"miniapp-history": "^0.1.6",
"query-string": "^6.13.1",
"universal-env": "^3.0.0"
},
Expand Down Expand Up @@ -49,4 +49,4 @@
"@types/rax": "^1.0.6",
"react": "^17.0.2"
}
}
}
2 changes: 1 addition & 1 deletion packages/create-app-shared/src/miniapp/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { setHistory } from '../storage';
import type { CreateHistory, InitHistory } from '../createInitHistory';

const createHistory: CreateHistory = ({ routes }) => {
if (process.env.__IS_SERVER__) {
if ((typeof global !== 'undefined' && (global as any).__IS_SERVER__) || process.env.__IS_SERVER__) {
// miniapp is not support ssr
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/create-app-shared/src/web/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { setHistory } from '../storage';

const createHistory: CreateHistory = ({ type, basename, location }) => {
let history: History;
if (process.env.__IS_SERVER__) {
if ((typeof global !== 'undefined' && (global as any).__IS_SERVER__) || process.env.__IS_SERVER__) {
history = createMemoryHistory();
(history as any).location = location;
} else if (type === 'hash') {
Expand Down