【问题标题】:How do I render a pug/jade template with React, Node and Webpack?如何使用 React、Node 和 Webpack 渲染 pug/jade 模板?
【发布时间】:2017-04-19 03:09:43
【问题描述】:

我有一个 React/Node/Express 应用程序,我正在使用 Webpack 来构建它。

目前目录结构如下:

node_modules
public
    app
        main.js
        main.map.js
    index.html
src
    client
        components
            Home.js
            Header.js
            Root.js
            User.js
        main.js
    server
        views
            index.html
        index.js
package.json
webpack.config.js

这是我的 webpack.config.js

var path = require("path");

var DIST_DIR = path.resolve(__dirname, "public");

var SRC_DIR = path.resolve(__dirname, "src");

var config = {
    entry: SRC_DIR + "/client/main.js",
    output: {
        path: DIST_DIR + "/app",
        filename: "main.js",
        publicPath: "/app"
    },
    module:{
        loaders: [
            {
                test: /\.js?/,
                include: SRC_DIR,
                loader: "babel-loader",
                query:{
                    presets:["react", "es2015", "stage-2"]
                }
            }
        ]
    }
};

module.exports = config;

这是我的 package.json

{
  "name": "react-webpack",
  "version": "1.0.0",
  "description": "",
  "main": "/src/server/index.js",
  "scripts": {
    "start": "npm run build",
    "build": "webpack -d && xcopy \"src/server/views/index.html\" \"public\" /F /Y && webpack-dev-server --content-base src/ --inline --hot --history-api-fallback",
    "build:prod": "webpack -p && xcopy \"src/server/views/index.html\" \"public/\" /F /Y"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "react": "^15.4.1",
    "react-dom": "^15.4.1",
    "react-router": "^3.0.0"
  },
  "devDependencies": {
    "babel-core": "^6.18.2",
    "babel-loader": "^6.2.8",
    "babel-preset-es2015": "^6.18.0",
    "babel-preset-react": "^6.16.0",
    "babel-preset-stage-2": "^6.18.0",
    "body-parser": "^1.15.2",
    "express": "^4.14.0",
    "jquery": "^3.1.1",
    "lodash": "^4.17.2",
    "morgan": "^1.7.0",
    "pug": "^2.0.0-beta6",
    "webpack": "^1.13.3",
    "webpack-dev-server": "^1.16.2"
  }
}

这行得通。我的 index.html 是这样的:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>React Basics</title>
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<div id="app"></div>
<script src="/app/main.js"></script>
<!--Everything will be bundled in app/mainjs -->
</body>
</html>

当我执行 npm start 时,应用程序构建,我可以转到 localhost:8080 并查看 index.html,并加载了子组件。加载的脚本是“app/main.js”,如下:

import React from 'react';
import { render } from 'react-dom';
import {Router, Route, browserHistory, IndexRoute} from "react-router";

//import newly created components
import {Root} from "./components/Root";
import {Home} from "./components/Home";
import {User} from "./components/User";

class App extends React.Component {


    render() {
        return (
            <Router history={browserHistory}>
                <Route path={"/"} component={Root}>
                    <IndexRoute component={Home}/>
                    <Route path={"user/:id"} component={User}/>
                    <Route path={"home"} component={Home}/>
                </Route>
                <Route path={"home"} component={Home}/>
            </Router>
        );
    }
}

render(<App />, window.document.getElementById('app'));

我现在想要做的不是拥有 index.html,而是拥有一个与 index.html 内容完全相同的 index.pug,并从服务器文件 index.js 提供 pug 模板。你能告诉我我该怎么做吗?我尝试了一些东西,但它把事情搞砸了,所以恢复到原来的状态。

【问题讨论】:

    标签: node.js reactjs webpack pug


    【解决方案1】:

    您可以使用 pug-as-jsx :https://github.com/bluewings/pug-as-jsx-loader

    您将在此处找到执行此操作的步骤:我可以将 pug (ex-jade) 与 react 框架一起使用吗?

    关于此的博客:https://medium.com/nishantsblog/pug-templates-for-react-presentation-components-7610967954a

    【讨论】:

      【解决方案2】:

      在 webpack.config.js 中观察你的 babel 加载器测试。

      test: /\.js?/,
      

      应该是

      test: /\.jsx?/,
      

      您的正则表达式中的? 表示前面的字符是可选的。在我们的例子中,我们希望 babel 在 *.js*.jsx 上运行。

      【讨论】:

        【解决方案3】:

        在根目录下:

        $ npm install pug --save
        

        服务器/index.js:

        var app = require('express')();
        app.set('view engine', 'pug');
        app.get('/', function (req, res) {
          res.render('index');
        });
        

        服务器/视图/index.pug:

        doctype html
        html(lang='en')
        
          head
            meta(charset='UTF-8')
            title React Basics
            //- Latest compiled and minified CSS
            link(rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css' integrity='sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u' crossorigin='anonymous')
        
          body
            #app
            script(src='/app/main.js')
            //- Everything will be bundled in app/mainjs
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-11-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-06-10
          相关资源
          最近更新 更多