Skip to content

Commit df48ea1

Browse files
committed
make sure nesting folder is available
1 parent 65492fe commit df48ea1

File tree

4 files changed

+96
-4
lines changed

4 files changed

+96
-4
lines changed

.gitignore

+1-4
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@ index.html
1010
yarn.lock
1111
yarn-error.log
1212

13-
# "External" plugins
14-
/nesting
15-
1613
# Perf related files
1714
isolate*.log
1815

1916
# Generated files
20-
/src/corePluginList.js
17+
/src/corePluginList.js

nesting/README.md

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# tailwindcss/nesting
2+
3+
This is a PostCSS plugin that wraps [postcss-nested](https://github.com/postcss/postcss-nested) or [postcss-nesting](https://github.com/jonathantneal/postcss-nesting) and acts as a compatibility layer to make sure your nesting plugin of choice properly understands Tailwind's custom syntax like `@apply` and `@screen`.
4+
5+
Add it to your PostCSS configuration, somewhere before Tailwind itself:
6+
7+
```js
8+
// postcss.config.js
9+
module.exports = {
10+
plugins: [
11+
require('postcss-import'),
12+
require('tailwindcss/nesting'),
13+
require('tailwindcss'),
14+
require('autoprefixer'),
15+
]
16+
}
17+
```
18+
19+
By default, it uses the [postcss-nested](https://github.com/postcss/postcss-nested) plugin under the hood, which uses a Sass-like syntax and is the plugin that powers nesting support in the [Tailwind CSS plugin API](https://tailwindcss.com/docs/plugins#css-in-js-syntax).
20+
21+
If you'd rather use [postcss-nesting](https://github.com/jonathantneal/postcss-nesting) (which is based on the work-in-progress [CSS Nesting](https://drafts.csswg.org/css-nesting-1/) specification), first install the plugin alongside:
22+
23+
```shell
24+
npm install postcss-nesting
25+
```
26+
27+
Then pass the plugin itself as an argument to `tailwindcss/nesting` in your PostCSS configuration:
28+
29+
```js
30+
// postcss.config.js
31+
module.exports = {
32+
plugins: [
33+
require('postcss-import'),
34+
require('tailwindcss/nesting')(require('postcss-nesting')),
35+
require('tailwindcss'),
36+
require('autoprefixer'),
37+
]
38+
}
39+
```
40+
41+
This can also be helpful if for whatever reason you need to use a very specific version of `postcss-nested` and want to override the version we bundle with `tailwindcss/nesting` itself.
42+

nesting/index.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
let nesting = require('./plugin')
2+
3+
module.exports = (opts) => {
4+
return {
5+
postcssPlugin: 'tailwindcss/nesting',
6+
Once(root, { result }) {
7+
return nesting(opts)(root, result)
8+
},
9+
}
10+
}
11+
12+
module.exports.postcss = true

nesting/plugin.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
let postcss = require('postcss')
2+
let postcssNested = require('postcss-nested')
3+
4+
module.exports = function nesting(opts = postcssNested) {
5+
return (root, result) => {
6+
root.walkAtRules('screen', (rule) => {
7+
rule.name = 'media'
8+
rule.params = `screen(${rule.params})`
9+
})
10+
11+
root.walkAtRules('apply', (rule) => {
12+
rule.before(postcss.decl({ prop: '__apply', value: rule.params, source: rule.source }))
13+
rule.remove()
14+
})
15+
16+
let plugin = (() => {
17+
if (typeof opts === 'function') {
18+
return opts
19+
}
20+
21+
if (typeof opts === 'string') {
22+
return require(opts)
23+
}
24+
25+
if (Object.keys(opts).length <= 0) {
26+
return postcssNested
27+
}
28+
29+
throw new Error('tailwindcss/nesting should be loaded with a nesting plugin.')
30+
})()
31+
32+
postcss([plugin]).process(root, result.opts).sync()
33+
34+
root.walkDecls('__apply', (decl) => {
35+
decl.before(postcss.atRule({ name: 'apply', params: decl.value, source: decl.source }))
36+
decl.remove()
37+
})
38+
39+
return root
40+
}
41+
}

0 commit comments

Comments
 (0)