0

试图用我的新项目实现同构渲染,所以我读了很多文章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 包含的相同类名。

4

1 回答 1

2

在解决了这个问题之后,我发现我的错误在于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'
     },

感谢您的关注 :)

于 2017-05-22T04:21:14.210 回答