本文介绍了服务器端渲染:'css-loader/locals' 生成不同的 classNames 然后 bundle.css - webpack的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Trying to implement isomorphic rendering with my new project, so i read lots of articles fancy things like css-loader, state sharing etc,etc. So after some struggle somehow i render my page on server side with css locals.So i move on and start building other pages coz everything looks great, until i didn't disable the javascript on my browser.After thatI found the difference on html that i received from server have different css local className and the bundle.css has different. I really stuck.

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

Here is my webpack.config.I know i am doing something wrong.I appreciate if you point out.

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];

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

For Example : i got .style__header__1H9xAC9 from server-side html and in bundle.css i got .style__header__2uiLmVi but if i enable JavaScript,App render again in client side with same className that bundle.css contains.

推荐答案

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

After struggling with this issue, i found my mistake is in webpack.config, i am using context in client side but not in server.so i remove that from client and everything works great.

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'
     },

谢谢您的关注:)

这篇关于服务器端渲染:'css-loader/locals' 生成不同的 classNames 然后 bundle.css - webpack的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 05:26