Skip to content

Commit 38cc82d

Browse files
orlinrauchg
authored andcommitted
With global stylesheet paths (vercel#1327)
* with-global-stylesheet without relative paths and with node_modules * a parenthetical remark about material-components-web not being part of the example
1 parent 40738c6 commit 38cc82d

File tree

7 files changed

+37
-12
lines changed

7 files changed

+37
-12
lines changed

examples/with-global-stylesheet/.babelrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
{
22
"plugins": [
3+
[
4+
"module-resolver", {
5+
"root": ["."],
6+
"alias": {
7+
"styles": "./styles"
8+
},
9+
"cwd": "babelrc"
10+
}],
311
[
412
"wrap-in-js",
513
{

examples/with-global-stylesheet/README.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ Download the example [or clone the repo](https://github.com/zeit/next.js):
99

1010
```bash
1111
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
12+
cd with-global-stylesheet
1313
```
1414

1515
To get this example running you just need to
1616

1717
npm install .
1818
npm run dev
1919

20-
Visit [http://localhost:300](http://localhost:300) and try to modify `pages/style.scss` changing color. Your changes should be picked up instantly.
20+
Visit [http://localhost:300](http://localhost:300) and try to modify `styles/index.scss` changing color. Your changes should be picked up instantly.
2121

2222
Also see it working with plain css here
2323
![example](example.gif)
@@ -31,14 +31,17 @@ now
3131

3232
## The idea behind the example
3333

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)
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).
3535

36-
This project shows how you can set it up. Have a look at
36+
Another babel plugin [module-resolver](https://github.com/tleunen/babel-plugin-module-resolver) enables us to import stylesheets from js (e.g. pages or components) through a `styles` directory alias rather than relative paths.
37+
38+
The `sass-loader` is configured with `includePaths: ['styles', 'node_modules']` so that your scss can `@import` from those places, again without relative paths, for maximum convenience and ability to use npm-published libraries. Furthermore, `glob` paths are also supported, so one could for example add `'node_modules/@material/*'` to the `includePaths`, which would make [material-components-web](https://github.com/material-components/material-components-web) (if you'd like) even easier to work with.
39+
40+
This project shows how you can set it up. Have a look at:
3741
- .babelrc
3842
- next.config.js
39-
- pages/style.scss
4043
- pages/index.js
41-
44+
- styles/index.scss
4245

4346
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
47+
github repository https://github.com/davibe/next.js-css-global-style-test

examples/with-global-stylesheet/next.config.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
const path = require('path')
2+
const glob = require('glob')
3+
14
module.exports = {
25
webpack: (config, { dev }) => {
36
config.module.rules.push(
@@ -11,12 +14,21 @@ module.exports = {
1114
,
1215
{
1316
test: /\.css$/,
14-
loader: 'babel-loader!raw-loader'
17+
use: ['babel-loader', 'raw-loader']
1518
}
1619
,
1720
{
18-
test: /\.scss$/,
19-
loader: 'babel-loader!raw-loader!sass-loader'
21+
test: /\.s(a|c)ss$/,
22+
use: ['babel-loader', 'raw-loader',
23+
{ loader: 'sass-loader',
24+
options: {
25+
includePaths: ['styles', 'node_modules']
26+
.map((d) => path.join(__dirname, d))
27+
.map((g) => glob.sync(g))
28+
.reduce((a, c) => a.concat(c), [])
29+
}
30+
}
31+
]
2032
}
2133
)
2234
return config

examples/with-global-stylesheet/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
"author": "Davide Bertola <dade@dadeb.it>",
1111
"license": "ISC",
1212
"dependencies": {
13+
"babel-plugin-module-resolver": "2.5.0",
1314
"babel-plugin-wrap-in-js": "^1.1.0",
15+
"glob": "7.1.1",
1416
"next": "^2.0.0-beta.18",
1517
"node-sass": "^4.4.0",
1618
"raw-loader": "^0.5.1",

examples/with-global-stylesheet/pages/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import React from 'react'
22

3-
import stylesheet from './style.scss'
3+
import stylesheet from 'styles/index.scss'
44
// or, if you work with plain css
5-
// import stylesheet from './style.css'
5+
// import stylesheet from 'styles/index.css'
66

77
export default () =>
88
<div>

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