React + Nunjucks 静态页面前端渲染
Webpack 根据 HTML 模板 直接构建成静态 HTML 页面,然后通过 Nunjucks 执行页面渲染.
- ${root}/app/controller/render.ts
import { Controller } from 'egg';
export default class ReactController extends Controller {
public async reactNunjucksRender() {
const { ctx } = this;
await ctx.render('react-nunjucks-render.tpl', {
title: 'Nunjucks Render',
data: JSON.stringify({ text: '基于 Egg + React + Nunjucks + TypeScript + Mobx + Webpack Client Side Render' })
});
}
}
- config/res.config.js
// https://www.yuque.com/easy-team/egg-react/config
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const resolve = (filepath) => path.resolve(__dirname, '..', filepath);
module.exports = {
entry: {
'react-nunjucks-render': 'app/web/page/react-nunjucks-render/index.tsx',
},
plugins: [
new HtmlWebpackPlugin({
chunks: ['runtime', 'common', 'react-nunjucks-render'],
filename: '../app/view/react-nunjucks-render.tpl',
template: './app/web/view/index.tpl'
})
]
};