Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6e5d57d

Browse files
thomaslindstromtimneutkens
authored andcommitted
Add example with-scoped-stylesheets-and-postcss (vercel#1146)
* Add example `with-external-stylesheets-and-postcss` * 🔥 Remove extra semi-colon * 📝 Attribute `with-global-stylesheet` * Force a new test * 📝 Update README.md * Rename `external` -> `scoped`
1 parent 45de5b8 commit 6e5d57d

File tree

7 files changed

+152
-0
lines changed

7 files changed

+152
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"presets": [
3+
"next/babel"
4+
],
5+
"plugins": [
6+
["wrap-in-js", {
7+
"extensions": ["css$"]
8+
}]
9+
]
10+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Scoped stylesheets with PostCSS example
2+
3+
This is an example of using scoped stylesheets and PostCSS, heavily influenced by @davibe's [`with-global-stylesheet`](https://github.com/zeit/next.js/tree/master/examples/with-global-stylesheet).
4+
5+
## How to use
6+
7+
Download the example [or clone the repo](https://github.com/zeit/next.js):
8+
9+
```bash
10+
curl https://codeload.github.com/zeit/next.js/tar.gz/master | tar -xz --strip=2 next.js-master/examples/with-scoped-stylesheets-and-postcss
11+
cd with-scoped-stylesheets-and-postcss
12+
```
13+
14+
To get this example running you must
15+
16+
npm install .
17+
npm run dev
18+
19+
Visit [http://localhost:3000](http://localhost:3000) and try edit `pages/styles.css`. Your changes should be picked up instantly.
20+
21+
Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download))
22+
23+
```bash
24+
now
25+
```
26+
27+
# Why
28+
29+
Scoped CSS is neat and keeps your JS clean. PostCSS is amazing for extended features, such as nesting. CSS Modules keep your class names “local”.
30+
31+
# Known bugs
32+
33+
There's a bug, possibly within `next.js`, making composition between files unuseable. Consider the following:
34+
35+
*`styles.css`*
36+
```css
37+
.paragraph {
38+
composes: font-sans from '../global.css';
39+
}
40+
```
41+
42+
*`global.css`*
43+
```css
44+
.font-sans {
45+
font-family: georgia; /* ;) */
46+
}
47+
```
48+
49+
The following error is thrown:
50+
51+
```
52+
Module build failed: Error: Cannot find module '-!./../node_modules/css-loader/index.js??ref--6-4!./../node_modules/postcss-loader/index.js!../global.css'
53+
```
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
module.exports = {
2+
webpack: (config) => {
3+
config.module.rules.push(
4+
{
5+
test: /\.css$/,
6+
use: [
7+
{
8+
loader: 'emit-file-loader',
9+
options: {
10+
name: 'dist/[path][name].[ext]'
11+
}
12+
},
13+
'raw-loader',
14+
'val-loader',
15+
{
16+
loader: 'skeleton-loader',
17+
options: {
18+
procedure: (content) => (
19+
`${content} \n` + ['module.exports = {',
20+
'stylesheet: module.exports.toString(),',
21+
'classNames: exports.locals',
22+
'}'
23+
].join('')
24+
)
25+
}
26+
},
27+
{
28+
loader: 'css-loader',
29+
options: {
30+
modules: true,
31+
minimize: true,
32+
importLoaders: 1,
33+
localIdentName: '[local]-[hash:base64:5]'
34+
}
35+
},
36+
'postcss-loader'
37+
]
38+
}
39+
)
40+
41+
return config
42+
}
43+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "next.js-with-scoped-stylesheets-and-postcss",
3+
"version": "1.0.0",
4+
"description": "",
5+
"scripts": {
6+
"dev": "next dev",
7+
"build": "next build",
8+
"start": "next start"
9+
},
10+
"author": "Thomas Lindstrøm <[email protected]>",
11+
"license": "ISC",
12+
"dependencies": {
13+
"babel-plugin-wrap-in-js": "^1.1.1",
14+
"css-loader": "^0.26.1",
15+
"next": "^2.0.0-beta.26",
16+
"postcss-cssnext": "^2.9.0",
17+
"postcss-loader": "^1.3.0",
18+
"raw-loader": "^0.5.1",
19+
"react": "^15.4.2",
20+
"react-dom": "^15.4.2",
21+
"skeleton-loader": "0.0.7",
22+
"val-loader": "^0.5.0"
23+
},
24+
"devDependencies": {
25+
"now": "^3.1.0"
26+
}
27+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import React from 'react'
2+
import Head from 'next/head'
3+
import {stylesheet, classNames} from './styles.css'
4+
5+
export default () => (
6+
<p className={classNames.paragraph}>
7+
<Head><style dangerouslySetInnerHTML={{__html: stylesheet}} /></Head>
8+
bazinga
9+
</p>
10+
)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.paragraph {
2+
font-size: 20px;
3+
color: red;
4+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
plugins: [
3+
require('postcss-cssnext')()
4+
]
5+
}

0 commit comments

Comments
 (0)