Skip to content

Commit 123a635

Browse files
Guilherme Diegotimneutkens
authored andcommitted
Example to create next application with scoped/external css. (vercel#1340)
* First structure for external css example * Remove: Builded files * Fix: Identation to 2 spaces * Fix example * Fix lint * Fix: Review points
1 parent 13ccf78 commit 123a635

File tree

8 files changed

+100
-0
lines changed

8 files changed

+100
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"presets": ["next/babel"],
3+
"plugins": [
4+
[
5+
"css-modules-transform", {
6+
"extensions": [".css", ".sass"],
7+
"extractCss": "./static/css/bundle.css",
8+
"preprocessCss": "./pre-processor.js"
9+
}
10+
]
11+
]
12+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
static
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
## Scoped Style with external CSS file
2+
The motivation for this example is using scoped css from external files and in the end generate a compiled static `.css` file to use in production..
3+
4+
#### Running That
5+
6+
```
7+
yarn install
8+
yarn start
9+
```
10+
11+
#### Supported Langs
12+
The plugin supports the `less`, `scss` and `css` file extensions. It is possible to add support for another pre-processor by creating a function to compile the code. In the example we use `sass` as our css pre-processor
13+
14+
To edit the types you need to go to `.babelrc`
15+
16+
17+
#### Attention Points
18+
- Next.js doesn't have support for watching `*.css files. So you will have to edit a Javascript file to re-compile the css. In the future this will be fixed by [#823](https://github.com/zeit/next.js/pull/823).
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "with-external-scoped-css",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"license": "ISC",
6+
"author": "",
7+
"scripts": {
8+
"start": "next",
9+
"build": "next build",
10+
"run": "next start"
11+
},
12+
"dependencies":{
13+
"next": "^2.0.0-beta",
14+
"react": "^15.4.2",
15+
"react-dom": "^15.4.2"
16+
},
17+
"devDependencies": {
18+
"babel-plugin-css-modules-transform": "^1.2.1",
19+
"node-sass": "^4.5.0",
20+
}
21+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import React from 'react'
2+
import Document, { Head, Main, NextScript } from 'next/document'
3+
4+
export default class MyDocument extends Document {
5+
static getInitialProps ({ renderPage }) {
6+
const {html, head} = renderPage()
7+
return { html, head }
8+
}
9+
10+
render () {
11+
return (
12+
<html>
13+
<Head>
14+
<link rel='stylesheet' href='/static/css/bundle.css' />
15+
</Head>
16+
<body>
17+
{this.props.customValue}
18+
<Main />
19+
<NextScript />
20+
</body>
21+
</html>
22+
)
23+
}
24+
}
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 ss from './index.sass'
3+
4+
export const IndexPage = () => (
5+
<div className={ss.example}>
6+
This is an example from scoped style in a outside CSS file {'<3'}
7+
</div>
8+
)
9+
10+
export default IndexPage
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.example
2+
font-size: 36px
3+
width: 300px
4+
margin: 100px auto
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const sass = require('node-sass')
2+
3+
module.exports = (data, filename) => {
4+
return sass.renderSync({
5+
data,
6+
file: filename,
7+
indentedSyntax: true,
8+
outputStyle: 'compressed'
9+
}).css.toString('utf8')
10+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy