-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make sure nesting folder is available
- Loading branch information
1 parent
52905b0
commit c8f2809
Showing
4 changed files
with
96 additions
and
4 deletions.
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
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,42 @@ | ||
# tailwindcss/nesting | ||
|
||
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`. | ||
|
||
Add it to your PostCSS configuration, somewhere before Tailwind itself: | ||
|
||
```js | ||
// postcss.config.js | ||
module.exports = { | ||
plugins: [ | ||
require('postcss-import'), | ||
require('tailwindcss/nesting'), | ||
require('tailwindcss'), | ||
require('autoprefixer'), | ||
] | ||
} | ||
``` | ||
|
||
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). | ||
|
||
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: | ||
|
||
```shell | ||
npm install postcss-nesting | ||
``` | ||
|
||
Then pass the plugin itself as an argument to `tailwindcss/nesting` in your PostCSS configuration: | ||
|
||
```js | ||
// postcss.config.js | ||
module.exports = { | ||
plugins: [ | ||
require('postcss-import'), | ||
require('tailwindcss/nesting')(require('postcss-nesting')), | ||
require('tailwindcss'), | ||
require('autoprefixer'), | ||
] | ||
} | ||
``` | ||
|
||
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. | ||
|
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 @@ | ||
let nesting = require('./plugin') | ||
|
||
module.exports = (opts) => { | ||
return { | ||
postcssPlugin: 'tailwindcss/nesting', | ||
Once(root, { result }) { | ||
return nesting(opts)(root, result) | ||
}, | ||
} | ||
} | ||
|
||
module.exports.postcss = true |
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,41 @@ | ||
let postcss = require('postcss') | ||
let postcssNested = require('postcss-nested') | ||
|
||
module.exports = function nesting(opts = postcssNested) { | ||
return (root, result) => { | ||
root.walkAtRules('screen', (rule) => { | ||
rule.name = 'media' | ||
rule.params = `screen(${rule.params})` | ||
}) | ||
|
||
root.walkAtRules('apply', (rule) => { | ||
rule.before(postcss.decl({ prop: '__apply', value: rule.params, source: rule.source })) | ||
rule.remove() | ||
}) | ||
|
||
let plugin = (() => { | ||
if (typeof opts === 'function') { | ||
return opts | ||
} | ||
|
||
if (typeof opts === 'string') { | ||
return require(opts) | ||
} | ||
|
||
if (Object.keys(opts).length <= 0) { | ||
return postcssNested | ||
} | ||
|
||
throw new Error('tailwindcss/nesting should be loaded with a nesting plugin.') | ||
})() | ||
|
||
postcss([plugin]).process(root, result.opts).sync() | ||
|
||
root.walkDecls('__apply', (decl) => { | ||
decl.before(postcss.atRule({ name: 'apply', params: decl.value, source: decl.source })) | ||
decl.remove() | ||
}) | ||
|
||
return root | ||
} | ||
} |