File size: 3,014 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import postcss from 'postcss'
import type { PluginCreator } from 'postcss'

// Since the cssnano-preset-simple.js will be bundled into cssnano-simple
// during pre-compilation, we need to test against the source file directly
// @ts-expect-error -- untyped file
import cssnanoPresetSimple from './cssnano-preset-simple'

const cssnanoPlugin: PluginCreator<any> = (options = {}) => {
  const plugins: any[] = []
  const nanoPlugins = cssnanoPresetSimple(options).plugins
  for (const nanoPlugin of nanoPlugins) {
    if (Array.isArray(nanoPlugin)) {
      let [processor, opts] = nanoPlugin
      if (
        typeof opts === 'undefined' ||
        (typeof opts === 'object' && !opts.exclude) ||
        (typeof opts === 'boolean' && opts === true)
      ) {
        plugins.push(processor(opts))
      }
    } else {
      plugins.push(nanoPlugin)
    }
  }
  return postcss(plugins)
}
cssnanoPlugin.postcss = true

function noopTemplate(strings: TemplateStringsArray, ...keys: string[]) {
  const lastIndex = strings.length - 1
  return (
    strings.slice(0, lastIndex).reduce((p, s, i) => p + s + keys[i], '') +
    strings[lastIndex]
  )
}

describe('https://github.com/Timer/cssnano-preset-simple/issues/1', () => {
  test('evaluates without error', () => {
    cssnanoPresetSimple()
  })
})

describe('cssnano accepts plugin configuration', () => {
  test('should not remove all comments', async () => {
    const input = noopTemplate`
      p {
        /*! heading */
        color: yellow;
      }
    `

    const res = await postcss([cssnanoPlugin({ discardComments: {} })]).process(
      input,
      {
        from: 'input.css',
        to: 'output.css',
      }
    )

    expect(res.css).not.toBe('p{color:#ff0}')
  })

  test('should remove all comments', async () => {
    const input = noopTemplate`
      p {
        /*! heading */
        color: yellow;
      }
    `

    const res = await postcss([
      cssnanoPlugin({ discardComments: { removeAll: true } }),
    ]).process(input, {
      from: 'input.css',
      to: 'output.css',
    })

    expect(res.css).toBe('p{color:#ff0}')
  })
})

describe('property sorting', () => {
  test('should result in correct border width', async () => {
    const input = noopTemplate`
      p {
        border: 1px solid var(--test);
        border-radius: var(--test);
        border-width: 0;
      }
    `

    const res = await postcss([cssnanoPlugin]).process(input, {
      from: 'input.css',
      to: 'output.css',
    })

    expect(res.css).toMatchInlineSnapshot(
      `"p{border-width:1px;border-radius:var(--test);border:0 solid var(--test)}"`
    )
  })
})

describe('with escaped selector', () => {
  test('should not misplace the backslash', async () => {
    const input = `
      .foo\\,2 {
        background: blue;
      }
    `

    const res = await postcss([cssnanoPlugin]).process(input, {
      from: 'input.css',
      to: 'output.css',
    })

    expect(res.css).toMatchInlineSnapshot(`".foo\\,2{background:blue}"`)
  })
})