-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.ts
82 lines (72 loc) · 1.74 KB
/
index.ts
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
export const AS_IS_PKGS = [
'dayjs',
'moment',
'prettier',
'rxjs',
// pkgs which has no umd module actually for now
'tslib',
]
export const UPPER_CAMEL_CASE_PKGS = ['react', 'react-router', 'redux', 'vue']
export const normalizePkg = (pkg: string) => {
if (pkg.startsWith('@')) {
pkg = pkg.split('/').slice(1).join('/')
}
return pkg
}
export interface StringMap {
[key: string]: string
}
export const asIsReducer = (
globals: StringMap | string,
pkg?: string,
): StringMap => ({
...(typeof globals === 'string'
? {
[globals]: normalizePkg(globals),
}
: globals),
...(pkg && {
[pkg]: normalizePkg(pkg),
}),
})
export const upperCamelCase = (pkg: string) =>
// eslint-disable-next-line unicorn/prefer-string-replace-all
pkg.replace(/(^|-)([a-z])/g, (_, _$1: string, $2: string) => $2.toUpperCase())
export const upperCamelCaseReducer = (
globals: StringMap | string,
pkg?: string,
): StringMap => ({
...(typeof globals === 'string'
? {
[globals]: upperCamelCase(normalizePkg(globals)),
}
: globals),
...(pkg && {
[pkg]: upperCamelCase(normalizePkg(pkg)),
}),
})
const GLOBALS = {
lodash: '_',
qrcode: 'QRCode',
qrious: 'QRious',
'react-dom': 'ReactDOM',
underscore: '_',
}
export const getGlobals = ({
asIsPkgs,
upperCamelCasePkgs,
globals,
}: {
asIsPkgs?: string[]
upperCamelCasePkgs?: string[]
globals?: StringMap
} = {}): StringMap => ({
...GLOBALS,
...AS_IS_PKGS.reduce(asIsReducer, {}),
...UPPER_CAMEL_CASE_PKGS.reduce(upperCamelCaseReducer, {}),
...(asIsPkgs ?? []).reduce(asIsReducer, {}),
...(upperCamelCasePkgs ?? []).reduce(upperCamelCaseReducer, {}),
...globals,
})
export const globals = getGlobals()
export { globals as default }