github地址:github.com/bbwlfx/ts-b…javascript
配置完成以后,接下来就要考虑打包启动以及先后端同构的架构方面的问题了。css
首先个人总体思路是:根据webpack.ssr.config.js配置文件,将前端代码打包进node层供node作SSR使用,而后前端正常启动webpack-dev-server服务器便可。html
"startfe": "run-p client ssr",
"client": "BABEL_ENV=client NODE_ENV=development webpack-dev-server --config public/webpack.dev.config.js",
"ssr": "BABEL_ENV=ssr NODE_ENV=development webpack --watch --config public/webpack.ssr.config.js",
复制代码
将前端代码打包进node以后,在正常启动node服务器便可:前端
"start": "BABEL_ENV=server NODE_ENV=development nodemon src/app.ts --exec babel-node --extensions '.ts,.tsx'",
复制代码
这样基本上webpack总体的打包思路就清晰了。java
最终生产模式中,咱们只须要将整个前端代码经过webpack打包进src
目录,而后将整个src
目录通过babel转义以后输出到output
目录,最终咱们的生产模式只须要启动output/app.js
便可。node
"buildfe": "run-p client:prod ssr:prod",
"build": "BABEL_ENV=server NODE_ENV=production babel src -D -d output/src --extensions '.ts,.tsx'",
"ssr:prod": "BABEL_ENV=ssr NODE_ENV=production webpack --config public/webpack.ssr.config.js",
"client:prod": "BABEL_ENV=client NODE_ENV=production webpack --progess --config public/webpack.prod.config.js",
复制代码
$ node output/app.js // 启动生产模式
复制代码
在客户端的打包中,咱们须要使用webpack-manifest-plugin
插件。这个插件能够将webpack打包以后全部文件的路径写入一个manifest.json
的文件中,咱们只须要读取这个文件就能够找到全部资源的正确路径了。react
const ManifestPlugin = require("webpack-manifest-plugin");
module.exports = merge(baseConfig, {
// ...
plugins: [
new ManifestPlugin(),
// ...
]
});
复制代码
Mapping loaded modules to bundles
In order to make sure that the client loads all the modules that were rendered server-side, we'll need to map them to the bundles that Webpack created.webpack
咱们的客户端渲染使用了react-loadable
,须要知道该模块是否提早通过了服务端渲染,不然会出现重复加载的问题。所以须要将webpack打包后的bundles
生成一个map文件,而后在ssr的时候传入react-loadable
。这里咱们使用react-loadable/webpack
插件便可。git
import { ReactLoadablePlugin } from 'react-loadable/webpack';
const outputDir = path.resolve(__dirname, "../src/public/buildPublic");
plugins: [
// ...
new ReactLoadablePlugin({
filename: path.resolve(outputDir, "react-loadable.json")
})
// ...
],
复制代码
接下来是webpack打包产物的资源路径问题。github
生产模式通常都是将输出的文件上传到cdn上,所以咱们只须要在pubicPath的地方使用cdn地址便可。
mode: "production",
output: {
filename: "[name].[chunkhash].js",
publicPath: "//cdn.address.com",
chunkFilename: "chunk.[name].[chunkhash].js"
},
复制代码
开发环境中咱们只须要读取manifest.json
文件中相对应模块的地址便可。
{
"home.js": "http://127.0.0.1:4999/static/home.js",
"home.css": "http://127.0.0.1:4999/static/home.css",
"home.js.map": "http://127.0.0.1:4999/static/home.js.map",
"home.css.map": "http://127.0.0.1:4999/static/home.css.map"
}
复制代码
解决了打包问题以后,咱们须要考虑ssr的问题了。
其实总体思路比较简单:咱们经过打包,已经有了manifest.json
文件储存静态资源路径,有react-loadable.json
文件储存打包输出的各个模块的信息,只须要在ssr的地方读出js、css路径,而后将被<Loadable.Capture />
包裹的组件renderToString
一下,填入pug模板中便可。
function getScript(src) {
return `<script type="text/javascript" src="${src}"></script>`;
}
function getStyle(src) {
return `<link rel="stylesheet" href="${src}" />`;
}
export { getScript, getStyle };
复制代码
import { getBundles } from "react-loadable/webpack";
import React from "react";
import { getScript, getStyle } from "./bundle";
import { renderToString } from "react-dom/server";
import Loadable from "react-loadable";
export default async function getPage({ store, url, Component, page }) {
const manifest = require("../public/buildPublic/manifest.json");
const mainjs = getScript(manifest[`${page}.js`]);
const maincss = getStyle(manifest[`${page}.css`]);
let modules: string[] = [];
const dom = (
<Loadable.Capture
report={moduleName => {
modules.push(moduleName);
}}
>
<Component url={url} store={store} />
</Loadable.Capture>
);
const html = renderToString(dom);
const stats = require("../public/buildPublic/react-loadable.json");
let bundles: any[] = getBundles(stats, modules);
const _styles = bundles
.filter(bundle => bundle && bundle.file.endsWith(".css"))
.map(bundle => getStyle(bundle.publicPath))
.concat(maincss);
const styles = [...new Set(_styles)].join("\n");
const _scripts = bundles
.filter(bundle => bundle && bundle.file.endsWith(".js"))
.map(bundle => getScript(bundle.publicPath))
.concat(mainjs);
const scripts = [...new Set(_scripts)].join("\n");
return {
html,
__INIT_STATES__: JSON.stringify(store.getState()),
scripts,
styles
};
}
复制代码
路径说明:
src/public
目录存放全部前端打包过来的文件,src/public/buildPublic
存放webpack.client.config.js
打包的前端代码,src/public/buildServer
存放webpack.ssr.config.js
打包的服务端渲染的代码。
这样服务端渲染的部分就基本完成了。
其余node层启动代码能够直接查看src/server.ts
文件便可。
接下来就要编写前端的业务代码来测试一下服务端渲染是否生效。
这里咱们要保证使用最少的代码完成先后端同构的功能。
首先咱们须要在webpack中定义个变量IS_NODE
,在代码中根据这个变量就能够区分ssr部分的代码和客户端部分的代码了。
plugins: [
// ...
new webpack.DefinePlugin({
IS_NODE: false
})
// ...
]
复制代码
接下来编写前端页面的入口文件,入口这里要对ssr和client作区别渲染:
import React, { Component } from "react";
import { Provider } from "react-redux";
import ReactDOM from "react-dom";
import Loadable from "react-loadable";
import { BrowserRouter, StaticRouter } from "react-router-dom";
// server side render
const SSR = App =>
class SSR extends Component<{
store: any;
url: string;
}> {
render() {
const context = {};
return (
<Provider store={this.props.store} context={context}> <StaticRouter location={this.props.url}> <App /> </StaticRouter> </Provider>
);
}
};
// client side render
const CLIENT = configureState => Component => {
const initStates = window.__INIT_STATES__;
const store = configureState(initStates);
Loadable.preloadReady().then(() => {
ReactDOM.hydrate(
<Provider store={store}> <BrowserRouter> <Component /> </BrowserRouter> </Provider>,
document.getElementById("root")
);
});
};
export default function entry(configureState) {
return IS_NODE ? SSR : CLIENT(configureState);
}
复制代码
这里entry参数中的configureState
是咱们store的声明文件。
import { init } from "@rematch/core";
import immerPlugin from "@rematch/immer";
import * as models from "./index";
const immer = immerPlugin();
export default function configure(initStates) {
const store = init({
models,
plugins: [immer]
});
for (const model of Object.keys(models)) {
store.dispatch({
type: `${model}/@init`,
payload: initStates[model]
});
}
return store;
}
复制代码
这样就万事俱备了,接下来只须要约定咱们单页的入口便可。
这里我将单页的入口都统一放到public/js/entry
目录下面,每个单页都是一个目录,好比个人项目中只有一个单页,所以我只建立了一个home
目录。
每个目录下面都有一个index.tsx
文件和一个routes.tsx
文件,分为是单页的总体入口代码,已经路由定义代码。
例如:
import Loadable from "react-loadable";
import * as Path from "constants/path";
import Loading from "components/loading";
export default [
{
name: "demo",
path: Path.Demo,
component: Loadable({
loader: () => import("containers/demo"),
loading: Loading
}),
exact: true
},
{
name: "todolist",
path: Path.Todolist,
component: Loadable({
loader: () => import("containers/todolist"),
loading: Loading
}),
exact: true
}
];
复制代码
import React, { Component } from "react";
import configureStore from "models/configure";
import entry from "decorators/entry";
import { Route } from "react-router-dom";
import Layout from "components/layout";
import routes from "./routes";
class Home extends Component {
render() {
return (
<Layout> {routes.map(({ path, component: Component, exact = true }) => { return ( <Route path={path} component={Component} key={path} exact={exact} /> ); })} </Layout> ); } } const Entry = entry(configureStore)(Home); export { Entry as default, Entry, configureStore }; 复制代码
Layout
组件是存放全部页面的公共部分,好比Nav导航条、Footer等。
这样全部的准备工做就已经作完了,剩下的工做就只有编写组件代码以及首屏数据加载了。
系列文章: