File size: 1,387 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
import curry from 'next/dist/compiled/lodash.curry'
import type { webpack } from 'next/dist/compiled/webpack/webpack'

export const loader = curry(function loader(
  rule: webpack.RuleSetRule,
  config: webpack.Configuration
) {
  if (!config.module) {
    config.module = { rules: [] }
  }

  if (rule.oneOf) {
    const existing = config.module.rules?.find(
      (arrayRule) =>
        arrayRule && typeof arrayRule === 'object' && arrayRule.oneOf
    )
    if (existing && typeof existing === 'object') {
      existing.oneOf!.push(...rule.oneOf)
      return config
    }
  }

  config.module.rules?.push(rule)
  return config
})

export const unshiftLoader = curry(function unshiftLoader(
  rule: webpack.RuleSetRule,
  config: webpack.Configuration
) {
  if (!config.module) {
    config.module = { rules: [] }
  }

  if (rule.oneOf) {
    const existing = config.module.rules?.find(
      (arrayRule) =>
        arrayRule && typeof arrayRule === 'object' && arrayRule.oneOf
    )
    if (existing && typeof existing === 'object') {
      existing.oneOf?.unshift(...rule.oneOf)
      return config
    }
  }

  config.module.rules?.unshift(rule)
  return config
})

export const plugin = curry(function plugin(
  p: webpack.WebpackPluginInstance,
  config: webpack.Configuration
) {
  if (!config.plugins) {
    config.plugins = []
  }
  config.plugins.push(p)
  return config
})