React + Nunjucks 静态页面前端渲染

Webpack 根据 HTML 模板 直接构建成静态 HTML 页面,然后通过 Nunjucks 执行页面渲染.

  • ${root}/app/controller/render.ts
  1. import { Controller } from 'egg';
  2. export default class ReactController extends Controller {
  3. public async reactNunjucksRender() {
  4. const { ctx } = this;
  5. await ctx.render('react-nunjucks-render.tpl', {
  6. title: 'Nunjucks Render',
  7. data: JSON.stringify({ text: '基于 Egg + React + Nunjucks + TypeScript + Mobx + Webpack Client Side Render' })
  8. });
  9. }
  10. }
  • 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'
    })
  ]
};