forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add global stylesheet example (vercel#1016)
* add global stylesheet example * fix avoiding html-escape of stylesheets * update readme * remove .gitignore
- Loading branch information
1 parent
faef6e4
commit 19f1125
Showing
8 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,14 @@ | ||
{ | ||
"plugins": [ | ||
[ | ||
"wrap-in-js", | ||
{ | ||
"extensions": ["css$", "scss$"] | ||
} | ||
] | ||
], | ||
"presets": [ | ||
"next/babel" | ||
], | ||
"ignore": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,44 @@ | ||
# Global Stylesheet example | ||
|
||
This is an example of how you can include a global stylesheet in a next.js webapp. | ||
|
||
|
||
## How to use | ||
|
||
Download the example [or clone the repo](https://github.com/zeit/next.js): | ||
|
||
```bash | ||
curl https://codeload.github.com/zeit/next.js/tar.gz/master | tar -xz --strip=2 next.js-master/examples/with-global-stylesheet | ||
cd with-global-stylesheet | ||
``` | ||
|
||
To get this example running you just need to | ||
|
||
npm install . | ||
npm run dev | ||
|
||
Visit [http://localhost:300](http://localhost:300) and try to modify `pages/style.scss` changing color. Your changes should be picked up instantly. | ||
|
||
Also see it working with plain css here | ||
![example](example.gif) | ||
|
||
Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download)) | ||
|
||
```bash | ||
now | ||
``` | ||
|
||
|
||
## The idea behind the example | ||
|
||
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) | ||
|
||
This project shows how you can set it up. Have a look at | ||
- .babelrc | ||
- next.config.js | ||
- pages/style.scss | ||
- pages/index.js | ||
|
||
|
||
Please, report any issue on enhancement related to this example to its original | ||
github repository https://github.com/davibe/next.js-css-global-style-test |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,24 @@ | ||
module.exports = { | ||
webpack: (config, { dev }) => { | ||
config.module.rules.push( | ||
{ | ||
test: /\.(css|scss)/, | ||
loader: 'emit-file-loader', | ||
options: { | ||
name: 'dist/[path][name].[ext]' | ||
} | ||
} | ||
, | ||
{ | ||
test: /\.css$/, | ||
loader: 'babel-loader!raw-loader' | ||
} | ||
, | ||
{ | ||
test: /\.scss$/, | ||
loader: 'babel-loader!raw-loader!sass-loader' | ||
} | ||
) | ||
return config | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,22 @@ | ||
{ | ||
"name": "next.js-css-global-style-test", | ||
"version": "1.0.0", | ||
"description": "", | ||
"scripts": { | ||
"dev": "next dev", | ||
"build": "next build", | ||
"start": "next start" | ||
}, | ||
"author": "Davide Bertola <[email protected]>", | ||
"license": "ISC", | ||
"dependencies": { | ||
"babel-plugin-wrap-in-js": "^1.1.0", | ||
"next": "^2.0.0-beta.18", | ||
"node-sass": "^4.4.0", | ||
"raw-loader": "^0.5.1", | ||
"sass-loader": "^4.1.1" | ||
}, | ||
"devDependencies": { | ||
"now": "^3.1.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,11 @@ | ||
import React from 'react' | ||
|
||
import stylesheet from './style.scss' | ||
// or, if you work with plain css | ||
// import stylesheet from './style.css' | ||
|
||
export default () => | ||
<div> | ||
<style dangerouslySetInnerHTML={{ __html: stylesheet }} /> | ||
<p>ciao</p> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,4 @@ | ||
p { | ||
font-size: xx-large; | ||
color: black; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,6 @@ | ||
$primary-color: black; | ||
|
||
p { | ||
font-size: xx-large; | ||
color: $primary-color; | ||
} |