因此,我正在尝试为我的应用程序(通过Create React App引导)进行服务器端渲染。
这是我的bootstrap.js的样子
require("ignore-styles");
require("babel-register")({
ignore: [/(node_modules)/],
presets: ["es2015", "react-app"]
});
require("./index");
我的index.js看起来像这样
import express from "express";
// we'll talk about this in a minute:
import serverRenderer from "./middleware/renderer";
const PORT = 3000;
const path = require("path");
// initialize the application and create the routes
const app = express();
const router = express.Router();
// root (/) should always serve our server rendered page
router.use("^/$", serverRenderer);
// other static resources should just be served as they are
router.use(
express.static(path.resolve(__dirname, "..", "build"), { maxAge: "30d" })
);
// tell the app to use the above rules
app.use(router);
// start the app
app.listen(PORT, error => {
if (error) {
return console.log("something bad happened", error);
}
console.log("listening on " + PORT + "...");
});
现在,当我像这样运行服务器时
NODE_ENV=production node bootstrap.js
我不断收到这样的错误
/node_modules/emotion/dist/index.cjs.js:49
tag.setAttribute('data-emotion', '');
^
TypeError: tag.setAttribute is not a function
为什么会这样?我认为node_modules在编译时会被忽略。
我无法控制其他库做什么,那么在这种情况下如何实现服务器端渲染?
我尝试了以下评论中的建议,但出现此错误
/node_modules/babel-core/lib/transformation/file/options/option-manager.js:328
throw e;
^
Error: Options {"loose":true} passed to /node_modules/babel-preset-react-app/node_modules/babel-preset-env/lib/index.js which does not accept options.
所以看来有一个翻译问题...这没有意义,因为我使用的是CRA,它无论如何都使用babel进行翻译,而且效果很好。