Skip to content

Commit 19f1125

Browse files
davibetimneutkens
authored andcommitted
add global stylesheet example (vercel#1016)
* add global stylesheet example * fix avoiding html-escape of stylesheets * update readme * remove .gitignore
1 parent faef6e4 commit 19f1125

File tree

8 files changed

+125
-0
lines changed

8 files changed

+125
-0
lines changed
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"plugins": [
3+
[
4+
"wrap-in-js",
5+
{
6+
"extensions": ["css$", "scss$"]
7+
}
8+
]
9+
],
10+
"presets": [
11+
"next/babel"
12+
],
13+
"ignore": []
14+
}
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Global Stylesheet example
2+
3+
This is an example of how you can include a global stylesheet in a next.js webapp.
4+
5+
6+
## How to use
7+
8+
Download the example [or clone the repo](https://github.com/zeit/next.js):
9+
10+
```bash
11+
curl https://codeload.github.com/zeit/next.js/tar.gz/master | tar -xz --strip=2 next.js-master/examples/with-global-stylesheet
12+
cd with-global-stylesheet
13+
```
14+
15+
To get this example running you just need to
16+
17+
npm install .
18+
npm run dev
19+
20+
Visit [http://localhost:300](http://localhost:300) and try to modify `pages/style.scss` changing color. Your changes should be picked up instantly.
21+
22+
Also see it working with plain css here
23+
![example](example.gif)
24+
25+
Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download))
26+
27+
```bash
28+
now
29+
```
30+
31+
32+
## The idea behind the example
33+
34+
The strategy here is to transpile the stylesheet file to a css-in-js file so that it can be loaded and hot reloaded both on the server and the client. For this purpose i created a babel loader plugin called [babel-loader-wrap-in-js](https://github.com/davibe/babel-plugin-wrap-in-js)
35+
36+
This project shows how you can set it up. Have a look at
37+
- .babelrc
38+
- next.config.js
39+
- pages/style.scss
40+
- pages/index.js
41+
42+
43+
Please, report any issue on enhancement related to this example to its original
44+
github repository https://github.com/davibe/next.js-css-global-style-test
140 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module.exports = {
2+
webpack: (config, { dev }) => {
3+
config.module.rules.push(
4+
{
5+
test: /\.(css|scss)/,
6+
loader: 'emit-file-loader',
7+
options: {
8+
name: 'dist/[path][name].[ext]'
9+
}
10+
}
11+
,
12+
{
13+
test: /\.css$/,
14+
loader: 'babel-loader!raw-loader'
15+
}
16+
,
17+
{
18+
test: /\.scss$/,
19+
loader: 'babel-loader!raw-loader!sass-loader'
20+
}
21+
)
22+
return config
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "next.js-css-global-style-test",
3+
"version": "1.0.0",
4+
"description": "",
5+
"scripts": {
6+
"dev": "next dev",
7+
"build": "next build",
8+
"start": "next start"
9+
},
10+
"author": "Davide Bertola <dade@dadeb.it>",
11+
"license": "ISC",
12+
"dependencies": {
13+
"babel-plugin-wrap-in-js": "^1.1.0",
14+
"next": "^2.0.0-beta.18",
15+
"node-sass": "^4.4.0",
16+
"raw-loader": "^0.5.1",
17+
"sass-loader": "^4.1.1"
18+
},
19+
"devDependencies": {
20+
"now": "^3.1.0"
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React from 'react'
2+
3+
import stylesheet from './style.scss'
4+
// or, if you work with plain css
5+
// import stylesheet from './style.css'
6+
7+
export default () =>
8+
<div>
9+
<style dangerouslySetInnerHTML={{ __html: stylesheet }} />
10+
<p>ciao</p>
11+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
p {
2+
font-size: xx-large;
3+
color: black;
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
$primary-color: black;
2+
3+
p {
4+
font-size: xx-large;
5+
color: $primary-color;
6+
}

0 commit comments

Comments
 (0)