1 1
export const AS_IS_PKGS = [
2
  'dayjs',
3
  'moment',
4
  'prettier',
5
  'rxjs',
6
  // pkgs which has no umd module actually for now
7
  'tslib',
8
]
9

10 1
export const UPPER_CAMEL_CASE_PKGS = ['react', 'react-router', 'redux', 'vue']
11

12 1
export const normalizePkg = (pkg: string) => {
13 1
  if (pkg.startsWith('@')) {
14 1
    pkg = pkg.split('/').slice(1).join('/')
15
  }
16 1
  return pkg
17
}
18

19
export interface StringMap {
20
  [key: string]: string
21
}
22

23 1
export const asIsReducer = (
24
  globals: string | StringMap,
25
  pkg?: string,
26 1
): StringMap => ({
27
  ...(typeof globals === 'string'
28 1
    ? {
29
        [globals]: normalizePkg(globals),
30
      }
31
    : globals),
32 1
  ...(pkg && {
33
    [pkg]: normalizePkg(pkg),
34
  }),
35
})
36

37 1
export const upperCamelCase = (pkg: string) =>
38 1
  pkg.replace(/(^|-)([a-z])/g, (_, _$1: string, $2: string) => $2.toUpperCase())
39

40 1
export const upperCamelCaseReducer = (
41
  globals: string | StringMap,
42
  pkg?: string,
43 1
): StringMap => ({
44
  ...(typeof globals === 'string'
45 1
    ? {
46
        [globals]: upperCamelCase(normalizePkg(globals)),
47
      }
48
    : globals),
49 1
  ...(pkg && {
50
    [pkg]: upperCamelCase(normalizePkg(pkg)),
51
  }),
52
})
53

54 1
const GLOBALS = {
55
  lodash: '_',
56
  qrcode: 'QRCode',
57
  qrious: 'QRious',
58
  'react-dom': 'ReactDOM',
59
  underscore: '_',
60
}
61

62 1
export const getGlobals = ({
63 1
  asIsPkgs,
64 1
  upperCamelCasePkgs,
65 1
  globals,
66
}: {
67
  asIsPkgs?: string[]
68
  upperCamelCasePkgs?: string[]
69
  globals?: StringMap
70 1
} = {}): StringMap => {
71 1
  return {
72
    ...GLOBALS,
73
    ...AS_IS_PKGS.reduce(asIsReducer, {}),
74
    ...UPPER_CAMEL_CASE_PKGS.reduce(upperCamelCaseReducer, {}),
75 1
    ...(asIsPkgs || []).reduce(asIsReducer, {}),
76 1
    ...(upperCamelCasePkgs || []).reduce(upperCamelCaseReducer, {}),
77
    ...globals,
78
  }
79
}
80

81 1
export const globals = getGlobals()
82

83 1
export { globals as default }

Read our documentation on viewing source code .

Loading