summaryrefslogtreecommitdiff
path: root/node_modules/@tailwindcss/nesting/index.js
blob: 8ff7f63ef499d10d4802b3919348f79b3594be55 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
let postcss = require('postcss')
let postcssNested = require('postcss-nested')

module.exports = (opts = postcssNested) => {
  return {
    postcssPlugin: '@tailwindcss/nesting',
    Once(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 }))
        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 }))
        decl.remove()
      })

      return root
    },
  }
}

module.exports.postcss = true