反应服务器端渲染 babel 配置



我正在尝试服务器渲染一个反应应用程序,我用 preset-env 配置 babel 以允许使用 import 语句,但它似乎不起作用,当我启动服务器时我收到此错误:SyntaxError: Cannot use import statement outside a module

package.json :

"babel": {
"presets": [
"react",
"env",
"stage-2"
]
}, 
"devDependencies": {
"babel-eslint": "^7.2.3",
"eslint": "^3.19.0",
"eslint-plugin-react": "^6.10.3",
"jest": "^19.0.2"
},
"dependencies": {
"babel-cli": "^6.24.1",
"babel-loader": "^7.0.0",
"babel-polyfill": "^6.23.0",
"babel-preset-env": "^1.4.0",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-2": "^6.24.1",
"ejs": "^2.5.6",
"express": "^4.15.2",
"pm2": "^2.4.6",
"react": "^15.5.4",
"react-dom": "^15.5.4",
"webpack": "^2.5.0"

服务器.js :

app.use("/", (req,res)=>{
const initialContent = serverRender();
res.render("index",{initialContent}); 
});

服务器渲染.js :

import React from "react";
import ReactDOMServer from  "react-dom/server";
import App from "./components/app";
const serverRender =()=>{
return ReactDOMServer.renderToString(<App/>);
};
export default serverRender;

我在服务器端使用 babel 我的 package.json 有那些与 babel 相关的软件包。

"scripts": {  
"babel-node": "babel-node ",     
"start": "nodemon --exec npm run babel-node -- ./app.js",
},
"devDependencies": {
"babel-polyfill": "^6.26.0",
"babel-cli": "^6.26.0",
"babel-eslint": "^8.2.2",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.7.0",
"babel-preset-latest": "^6.24.1",
"babel-register": "^6.26.0",
}

对于预设,我已经创建了.babelrc文件

{
"presets": ["env"],
"plugins": [
[
"transform-runtime",
{
"polyfill": false,
"regenerator": true
}
],   
]
}

希望对您有所帮助

相关内容

  • 没有找到相关文章

最新更新