Skip to content

Commit

Permalink
Extracted styles to make an example with css file and styled componen…
Browse files Browse the repository at this point in the history
…ts on ssr.
  • Loading branch information
federico-hv committed Jun 12, 2020
1 parent b3f33e2 commit 9f2913a
Show file tree
Hide file tree
Showing 9 changed files with 794 additions and 47 deletions.
15 changes: 14 additions & 1 deletion common/App.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import React, { Component } from 'react'
import styled from 'styled-components';
import './styles.css';

const StyledBox = styled.div`
height: 100px;
background: black;
color: yellow;
font-size: 40px;
`;


class App extends Component {

Expand Down Expand Up @@ -26,10 +36,13 @@ class App extends Component {

render(){
return (
<div style={{ background: 'purple', color: 'white', fontSize: '30px' }}>
<div className="container">
<div>Message from the server: {this.state.message}</div>
<br/>
<button onClick={this.getMessage}>Get Message</button>
<StyledBox>
This is just a box that will bring styles from server and also hot reload its styles on every change
</StyledBox>
</div>
)
}
Expand Down
Empty file added common/empty.js
Empty file.
5 changes: 5 additions & 0 deletions common/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.container {
background: green;
color: white;
font-size: 30px;
}
740 changes: 717 additions & 23 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,20 @@
"koa": "^2.7.0",
"koa-router": "^7.4.0",
"npm-run-all": "^4.1.5",
"react": "^16.7.0",
"react-dom": "^16.7.0",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-hot-loader": "^4.5.3",
"styled-components": "^5.1.1",
"webpack-dev-server": "^3.1.14"
},
"devDependencies": {
"babel-core": "^6.26.3",
"babel-loader": "^7.1.5",
"babel-preset-env": "^1.7.0",
"css-loader": "^3.5.3",
"mini-css-extract-plugin": "^0.9.0",
"start-server-webpack-plugin": "^2.2.5",
"style-loader": "^1.2.1",
"webpack": "^4.29.0",
"webpack-cli": "^3.2.1",
"webpack-node-externals": "^1.7.2"
Expand Down
4 changes: 2 additions & 2 deletions server/routes/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import api from './api';
// import hotroute from './hotroute';
import hotroute from './hotroute';

function applyRoutes(app) {
app.use(api.routes()).use(api.allowedMethods());
// app.use(hotroute.routes()).use(hotroute.allowedMethods());
app.use(hotroute.routes()).use(hotroute.allowedMethods());
}

export default applyRoutes;
10 changes: 8 additions & 2 deletions server/server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import express from 'express'
import React from 'react'
import { renderToString } from 'react-dom/server'
import { ServerStyleSheet } from 'styled-components'
import App from '../common/App'
import Koa from 'koa';
import applyRoutes from './routes';
Expand All @@ -10,8 +11,10 @@ const app = new Koa();
applyRoutes(app);

app.use(ctx => {
let application = renderToString(<App />)

const sheet = new ServerStyleSheet()
const application = renderToString(sheet.collectStyles(<App />))
const styleTags = sheet.getStyleTags()

ctx.body = `<!doctype html>
<html class="no-js" lang="">
<head>
Expand All @@ -21,12 +24,15 @@ app.use(ctx => {
<meta name="description" content="">
<meta name="viewport"
content="width=device-width, initial-scale=1">
${styleTags}
</head>
<body>
<div id="root">${application}</div>
<link rel="stylesheet" href="http://localhost:3001/shit.css">
<script src="http://localhost:3001/client.js"></script>
</body>
</html>`;
sheet.seal();
});

export default app
56 changes: 40 additions & 16 deletions webpack.config.client.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const webpack = require('webpack')
const path = require('path')
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

module.exports = {

Expand All @@ -13,24 +14,47 @@ module.exports = {
],
target: 'web',
module: {
rules: [{
test: /\.js?$/,
use: 'babel-loader',
include: [
path.join(__dirname, 'client'),
path.join(__dirname, 'common')
]
}]
rules: [
{
test: /\.(less|css)$/,
use : [
{
loader: MiniCssExtractPlugin.loader,
options: {
// you can specify a publicPath here
// by default it uses publicPath in webpackOptions.output
publicPath: 'http://localhost:3001/',
hmr: true // Evaluate here if it's dev environment or not
},
},
// 'style-loader',
'css-loader'
]
},
{
test: /\.js?$/,
use: 'babel-loader',
include: [
path.join(__dirname, 'client'),
path.join(__dirname, 'common')
]
}
]
},
plugins: [
new webpack.NamedModulesPlugin(),
new webpack.HotModuleReplacementPlugin(),
new webpack.NoEmitOnErrorsPlugin(),
new webpack.DefinePlugin({
"process.env": {
"BUILD_TARGET": JSON.stringify("client")
}
})
new MiniCssExtractPlugin({
filename : 'shit.css',
// disable : false,
// allChunks: true
}),
new webpack.NamedModulesPlugin(),
new webpack.HotModuleReplacementPlugin(),
new webpack.NoEmitOnErrorsPlugin(),
new webpack.DefinePlugin({
"process.env": {
"BUILD_TARGET": JSON.stringify("client")
}
})
],
devServer: {
host: 'localhost',
Expand Down
3 changes: 2 additions & 1 deletion webpack.config.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ module.exports = {
],
},
plugins: [
new webpack.NormalModuleReplacementPlugin(/\.(css|less)$/, './empty.js'),
new StartServerPlugin("server.js"),
new webpack.NamedModulesPlugin(),
new webpack.HotModuleReplacementPlugin(),
new webpack.NoEmitOnErrorsPlugin(),
new webpack.DefinePlugin({
"process.env": { BUILD_TARGET: JSON.stringify("server") },
}),
})
],
output: {
path: path.join(__dirname, ".build"),
Expand Down

0 comments on commit 9f2913a

Please sign in to comment.