服务器端渲染:'css-loader / locals'生成不同的classNames然后bundle.css - webpack

时间:2017-05-18 13:58:43

标签: reactjs webpack isomorphic-style-loader

尝试用我的新项目实现同构渲染,所以我读了许多文章,如css-loader,状态共享等等。因此,经过一番努力,我在服务器端使用css locals渲染我的页面。所以我继续开始构建其他页面因为一切看起来很棒,直到我没有在我的浏览器上禁用javascript 。那 我发现从服务器收到的html的差异有不同的css本地className,bundle.css有不同。我真的卡住了。

这是我的webpack.config。我知道我做错了什么。如果你指出,我很感激。

const webpack = require('webpack');
const path = require('path');
const context = path.resolve(__dirname, 'src');
const nodeExternals = require('webpack-node-externals');
const ExtractTextPlugin = require("extract-text-webpack-plugin");

const config = {
    name:"client",
    context,
    entry: './App.js',
    output: {
        path: __dirname+"/.build/assets",
        filename: 'bundle.js'
    },
    devtool: "source-map",
    module: {
        rules: [
            {
                test:/\.(?:js|jsx)$/,
                exclude: /node_modules/,
                use: 'babel-loader',
            },
            {
                test: /\.(?:css|less)$/,
                use: ExtractTextPlugin.extract({
                    use: [
                        {
                            loader: 'css-loader',
                            options: {
                                sourceMap: true,
                                importLoader: true,
                                localIdentName:'[name]__[local]__[hash:base64:7]'
                            }
                        },
                        {
                            loader: 'less-loader',
                            options: {
                                sourceMap: true,
                                importLoader: true,
                                localIdentName:'[name]__[local]__[hash:base64:7]'
                            }
                        }
                    ],
                    fallback: 'style-loader',
                }),
                exclude: /\.(eot|woff|woff2|ttf|svg)(\?[\s\S]+)?$/
            },
            {
                test: /\.(eot|woff|woff2|ttf|svg)(\?[\s\S]+)?$/,
                loader: 'url-loader?limit=1000&name=./fonts/[name].[ext]?[hash:base64:5]#icomoon',
            }
        ]
    },
    plugins: [
        new ExtractTextPlugin({
            filename: 'bundle.css',
            allChunks: true,
        }),
        new webpack.DefinePlugin({
            "process.env": {
                // Mainly used to require CSS files with webpack, which can happen only on browser
                // Used as `if (process.env.BROWSER)...`
                BROWSER: JSON.stringify(true),

                // Useful to reduce the size of client-side libraries, e.g. react
                NODE_ENV: JSON.stringify("production")

            }
        }),
    ],
    resolve: {
        extensions: ['.jsx', '.js', '.json']
    }
};

const serverConfig = {
    name: 'server',
    target: 'node',
    externals: [nodeExternals()],
    entry: [
        './index.js'
    ],
    output: {
        path: path.join(__dirname, './.build'),
        filename: 'server.build.js',
        publicPath: '.build/',
        libraryTarget: 'commonjs2'
    },
    module: {
        loaders: [
            {
                test:/\.(?:js|jsx)$/,
                exclude: /node_modules/,
                use: 'babel-loader',
            },
            {
                test: /\.(?:css|less)$/,
                use: "css-loader/locals?localIdentName=[name]__[local]__[hash:base64:7]",
                exclude: /\.(eot|woff|woff2|ttf|svg)(\?[\s\S]+)?$/
            },
            {
                test: /\.(eot|woff|woff2|ttf|svg)(\?[\s\S]+)?$/,
                loader: 'url-loader?limit=1000&name=./fonts/[name].[ext]?[hash:base64:5]#icomoon',
            }
        ]
    },
    resolve: {
        extensions: ['.jsx', '.js', '.json']
    }
};

module.exports = [config, serverConfig];

例如:我从服务器端.style__header__1H9xAC9获得了html,在bundle.css中获得了.style__header__2uiLmVi但是如果我启用了JavaScript, 应用程序在客户端再次使用bundle.css包含的相同className进行渲染。

1 个答案:

答案 0 :(得分:1)

在努力解决这个问题后,我发现我的错误在webpack.config,我在客户端使用context但在服务器中没有。 所以我从客户端删除它,一切都很好。

const webpack = require('webpack');
const path = require('path');
const context = path.resolve(__dirname, 'src');

const config = {
    name:"client",
    context,
    entry: './App.js',
    output: {
        path: __dirname+"/.build/assets",
        filename: 'bundle.js'
    },

 const webpack = require('webpack');
 const path = require('path');

 const config = {
     name:"client",
//-----------xxx------------
     entry: './App.js',
     output: {
         path: __dirname+"/.build/assets",
         filename: 'bundle.js'
     },

谢谢你的注意:)