PHPを使用してnode.jsにデータを投稿し、reactjsコンポーネントのサーバー側を表示しています。reactjs es6ファイルのcssスクリプトをインポートする際に予期しないトークンILLEGALエラーが発生しました
変数component
がundefined
を返すと、デバッグコンソールにエラーUnexpected token ILLEGAL
が表示されます。
React = require('react'),
ReactDOM = require('react-dom/server'),
require('babel-core/register');
app.post('/something', function (req, res) {
try {
var view = './src/ItemPage';
var component = require(view); <<<====== Error
var props = req.body || null;
res.status(200).send(
ReactDOM.renderToString(
React.createElement(component, props)
)
);
} catch (err) {
res.status(500).send(err.message);
}
});
問題がbundle.js
でCSSを含めるために、グロメットのUIライブラリからCSSのスクリプトをインポートによるものです。そのスタイルのインポート行を削除すると、スクリプトは正常に実行されています。なぜ、次のクラスはノードで未定義を返すのですか?構文エラーはありますか?それはwebpackでうまくコンパイルされています。
ItemPageクラス:
import React from 'react';
import Tabs from 'grommet/components/Tabs';
import Tab from 'grommet/components/Tab';
import 'grommet/grommet.min.css'; <<=== Error !!!!
export default class ItemPage extends React.Component {
render(){ return <Tabs>
<Tab title="First Title">
<h3>First Tab</h3>
<p>Contents of the first tab !</p>
</Tab>
<Tab title="Second Title">
<h3>Second Tab</h3>
<p>Contents of the second tab</p>
</Tab>
<Tab title="Third Title">
<h3>Third Tab!</h3>
<p>Contents of the third tab</p>
</Tab>
</Tabs>
}
}
module.exports = ItemPage;
Node.JSはcssファイルを要求できません。 webpackを使用する場合、https://github.com/webpack/css-loaderでrequire( 'style.css')を処理します サーバー上でwebpackを使用しないでください。 可能なソリューションの1つ:webpackを使用してバンドルをビルドし、サーバー上で実行します。 – Yozi
どのようにあなたのアプリケーションを構築していますか? webpackを使用しますか? –
@ Yozi、CSSをjsにバンドルするのはいいですか?htmlドキュメントにminify cssを含めるだけですか? – RedGiant