File size: 1,447 Bytes
bc20498
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// contains synchronous API only so it can be exported as CJS and ESM

/** @type {import('..').isDepIncluded} */
function isDepIncluded(dep, optimizeDepsInclude) {
  return optimizeDepsInclude.some((id) => parseIncludeStr(id) === dep)
}

/** @type {import('..').isDepExcluded} */
function isDepExcluded(dep, optimizeDepsExclude) {
  dep = parseIncludeStr(dep)
  return optimizeDepsExclude.some(
    (id) => id === dep || dep.startsWith(`${id}/`)
  )
}

/** @type {import('..').isDepNoExternaled} */
function isDepNoExternaled(dep, ssrNoExternal) {
  if (ssrNoExternal === true) {
    return true
  } else {
    return isMatch(dep, ssrNoExternal)
  }
}

/** @type {import('..').isDepExternaled} */
function isDepExternaled(dep, ssrExternal) {
  return ssrExternal.includes(dep)
}

/**
 * @param {string} raw could be "foo" or "foo > bar" etc
 */
function parseIncludeStr(raw) {
  const lastArrow = raw.lastIndexOf('>')
  return lastArrow === -1 ? raw : raw.slice(lastArrow + 1).trim()
}

/**
 * @param {string} target
 * @param {string | RegExp | (string | RegExp)[]} pattern
 */
function isMatch(target, pattern) {
  if (Array.isArray(pattern)) {
    return pattern.some((p) => isMatch(target, p))
  } else if (typeof pattern === 'string') {
    return target === pattern
  } else if (pattern instanceof RegExp) {
    return pattern.test(target)
  }
}

module.exports = {
  isDepIncluded,
  isDepExcluded,
  isDepNoExternaled,
  isDepExternaled
}