-
Notifications
You must be signed in to change notification settings - Fork 27k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Example to create next application with scoped/external css. #1340
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,12 @@ | ||
{ | ||
"presets": ["next/babel"], | ||
"plugins": [ | ||
[ | ||
"css-modules-transform", { | ||
"extensions": [".css", ".sass"], | ||
"extractCss": "./static/css/bundle.css", | ||
"preprocessCss": "./pre-processor.js" | ||
} | ||
] | ||
] | ||
} |
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 @@ | ||
static |
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,18 @@ | ||
## Scoped Style with external CSS file | ||
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.. | ||
|
||
#### Running That | ||
|
||
``` | ||
yarn install | ||
yarn start | ||
``` | ||
|
||
#### Supported Langs | ||
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 | ||
|
||
To edit the types you need to go to `.babelrc` | ||
|
||
|
||
#### Attention Points | ||
- 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). |
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,21 @@ | ||
{ | ||
"name": "with-external-scoped-css", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"license": "ISC", | ||
"author": "", | ||
"scripts": { | ||
"start": "next", | ||
"build": "next build", | ||
"run": "next start" | ||
}, | ||
"dependencies":{ | ||
"next": "^2.0.0-beta", | ||
"react": "^15.4.2", | ||
"react-dom": "^15.4.2" | ||
}, | ||
"devDependencies": { | ||
"babel-plugin-css-modules-transform": "^1.2.1", | ||
"node-sass": "^4.5.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,24 @@ | ||
import React from 'react' | ||
import Document, { Head, Main, NextScript } from 'next/document' | ||
|
||
export default class MyDocument extends Document { | ||
static getInitialProps ({ renderPage }) { | ||
const {html, head} = renderPage() | ||
return { html, head } | ||
} | ||
|
||
render () { | ||
return ( | ||
<html> | ||
<Head> | ||
<link rel='stylesheet' href='/static/css/bundle.css' /> | ||
</Head> | ||
<body> | ||
{this.props.customValue} | ||
<Main /> | ||
<NextScript /> | ||
</body> | ||
</html> | ||
) | ||
} | ||
} |
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,10 @@ | ||
import React from 'react' | ||
import ss from './index.sass' | ||
|
||
export const IndexPage = () => ( | ||
<div className={ss.example}> | ||
This is an example from scoped style in a outside CSS file {'<3'} | ||
</div> | ||
) | ||
|
||
export default IndexPage |
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 @@ | ||
.example | ||
font-size: 36px | ||
width: 300px | ||
margin: 100px auto |
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,10 @@ | ||
const sass = require('node-sass') | ||
|
||
module.exports = (data, filename) => { | ||
return sass.renderSync({ | ||
data, | ||
file: filename, | ||
indentedSyntax: true, | ||
outputStyle: 'compressed' | ||
}).css.toString('utf8') | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you make this file line with https://github.com/zeit/next.js/blob/master/examples/basic-css/package.json
name
same as example directorylicense
ISCauthor
emptyAlso
next
,react
andreact-dom
should be inside thedependencies
. NotdevDependencies
."next": "^2.0.0-beta.17"
can be changed to"next": "^2.0.0-beta"