我正在尝试使用 SSR 将 CSS 添加到我在 React 中构建的组件中,但我无法这样做。
我看过的东西:
- https://www.npmjs.com/package/isomorphic-style-loader
- https://cssinjs.org/server-side-rendering/?v=v10.0.0-alpha.22
- webpack 加载器
但没有一个过程是简单的或明确提到的。我尝试了很多是同构加载器,它看起来很有希望,但是在我的 CSS 文件中设置它之后它给出了一些模糊的错误:
意外令牌 (1:0) 您可能需要适当的加载程序来处理此文件类型。
这是我的样板包 - https://github.com/alexnm/react-ssr
如何向我的 React SSR 代码添加样式。
更新
const dev = process.env.NODE_ENV !== "production";
const path = require( "path" );
const { BundleAnalyzerPlugin } = require( "webpack-bundle-analyzer" );
const FriendlyErrorsWebpackPlugin = require( "friendly-errors-webpack-plugin" );
const plugins = [
new FriendlyErrorsWebpackPlugin(),
];
if ( !dev ) {
plugins.push( new BundleAnalyzerPlugin( {
analyzerMode: "static",
reportFilename: "webpack-report.html",
openAnalyzer: false,
} ) );
}
module.exports = {
mode: dev ? "development" : "production",
context: path.join( __dirname, "src" ),
devtool: dev ? "none" : "source-map",
entry: {
app: "./client.js",
},
resolve: {
modules: [
path.resolve( "./src" ),
"node_modules",
],
},
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
loader: "babel-loader",
},
],
},
output: {
path: path.resolve( __dirname, "dist" ),
filename: "[name].bundle.js",
},
plugins,
};