问题
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 that
I found the difference on html that i received from server have different css local className and the bundle.css
has different. I really stuck.
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];
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.
回答1:
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'
},
To
const webpack = require('webpack');
const path = require('path');
const config = {
name:"client",
//-----------xxx------------
entry: './App.js',
output: {
path: __dirname+"/.build/assets",
filename: 'bundle.js'
},
Thanks, for your attention :)
来源:https://stackoverflow.com/questions/44049834/server-side-render-css-loader-locals-generate-different-classnames-then-bund