File size: 1,844 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
// Originated from https://github.com/Timer/cssnano-simple/blob/master/src/index.js

/**
 * This file is the actual "cssnano-simple" implementation, which will eventually be used
 * by "next/src/build/cssnano-simple"
 */

const createSimplePreset = require('./cssnano-preset-simple')

module.exports = (opts = {}, postcss = require('postcss')) => {
  const excludeAll = Boolean(opts && opts.excludeAll)

  const userOpts = Object.assign({}, opts)

  if (excludeAll) {
    for (const userOption in userOpts) {
      if (!userOpts.hasOwnProperty(userOption)) continue
      const val = userOpts[userOption]
      if (!Boolean(val)) {
        continue
      }

      if (Object.prototype.toString.call(val) === '[object Object]') {
        userOpts[userOption] = Object.assign({}, { exclude: false }, val)
      }
    }
  }

  const options = Object.assign(
    {},
    excludeAll ? { rawCache: true } : undefined,
    userOpts
  )

  const plugins = []
  createSimplePreset(options).plugins.forEach((plugin) => {
    if (Array.isArray(plugin)) {
      let [processor, pluginOpts] = plugin
      processor = processor.default || processor

      const isEnabled =
        // No options:
        (!excludeAll && typeof pluginOpts === 'undefined') ||
        // Short-hand enabled:
        (typeof pluginOpts === 'boolean' && pluginOpts) ||
        // Include all plugins:
        (!excludeAll &&
          pluginOpts &&
          typeof pluginOpts === 'object' &&
          !pluginOpts.exclude) ||
        // Exclude all plugins:
        (excludeAll &&
          pluginOpts &&
          typeof pluginOpts === 'object' &&
          pluginOpts.exclude === false)

      if (isEnabled) {
        plugins.push(processor(pluginOpts))
      }
    } else {
      plugins.push(plugin)
    }
  })

  return postcss(plugins)
}

module.exports.postcss = true