Skip to content

Commit

Permalink
Step 3: Add styles
Browse files Browse the repository at this point in the history
Now lets learn to deal with styles. We configure webpack loaders to
support loading CSS files. This is cool, but there comes one problem
with server-side rendering: styles won't be loaded until all of JS loads,
so no styles for devices without JS.
Let's fix this problem with webpack's ExtractTextPlugin plugin: it
extracts all CSS styles into one CSS file that we can serve to our client,
so our page will instantly look perfectly styled, even without JS.
  • Loading branch information
dimaip committed May 12, 2016
1 parent fad6b64 commit 53d2ffa
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 4 deletions.
5 changes: 5 additions & 0 deletions Hello.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.Hello {
font-size: 3em;
font-family: sans-serif;
color: rebeccapurple;
}
5 changes: 4 additions & 1 deletion Hello.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import React from 'react';
if (process.env.BROWSER) {
require('Hello.css');
}

const Hello = React.createClass({
render: function() {
return <div>Hello {this.props.name}</div>;
return <div className="Hello">Hello {this.props.name}</div>;
}
});

Expand Down
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<html lang="en">
<head>
<title>Server-side rendering tutorial</title>
<link rel="stylesheet" href="/built/styles.css" />
</head>
<body>
<div id="app"></div>
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
"react-dom": "^0.14.8"
},
"devDependencies": {
"webpack": "^1.12.14",
"babel-cli": "^6.6.5",
"babel-loader": "^6.2.4",
"babel-preset-es2015": "^6.6.0",
"babel-preset-react": "^6.5.0"
"babel-preset-react": "^6.5.0",
"css-loader": "^0.23.1",
"extract-text-webpack-plugin": "^1.0.1",
"style-loader": "^0.13.1",
"webpack": "^1.12.14"
}
}
8 changes: 7 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');

const config = {
entry: ['client.js'],
Expand All @@ -14,9 +15,14 @@ const config = {
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel'
}
},
{
test: /\.css$/,
loader: ExtractTextPlugin.extract('style', 'css')
},
]
},
plugins: [new ExtractTextPlugin('styles.css')],
resolve: {
root: __dirname
}
Expand Down

0 comments on commit 53d2ffa

Please sign in to comment.