动态导入不适用于组合的 CRA 和 SSR

Dynamic import doesn't work with combined CRA and SSR

我已经使用 create-react-app 创建了一个网站,现在我想使用服务器端渲染因为 seo。

经过长时间的研究,我找到了一个简单的工具 here

它工作正常,但在我的代码中我有一个动态导入 await import("localization/" + newLanguage + "/" + newLanguage + ".json");,因此我得到一个 SyntaxError: Unexpected token import 错误。

我也试过使用 System.import。这对 SSR 有效,但 CRA 说 'System.import' is restricted from being used. Please use import() instead..

我该如何解决这个问题?我不想弹出或类似并保持轻松。

我的index.js:

require('ignore-styles');
require('babel-register')({
  ignore: /\/(build|node_modules)\//,
  presets: ['env','react-app'],
  plugins: ["syntax-dynamic-import"]
});

require('./server');

这是我的服务器文件:

import express from 'express';
import http from 'http';
import React from 'react';
import ReactDOMServer from 'react-dom/server';
import { StaticRouter } from "react-router";
import App from "./src/App";

const port = 3000;
const app = express();
const server = http.createServer(app);

app.use((req, res) => {

    const context = {}
    const html = ReactDOMServer.renderToString(
        <StaticRouter location={req.url} context={context}>
            <App />
        </StaticRouter>
    );

    if(context.url){
        res.status(302).redirect(context.url);
        return;
    }

    res.status(200).send(html);
});

server.listen(port, function(){
    console.log('Node server running on port ' + port + '.');
    console.log("Time: " + new Date(Date.now()));
});

好的。我知道了。

我用过爱彼迎的 syntax-dynamic-import plugin from babel. With dynamic-import-node,效果很好。