-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
67 lines (59 loc) · 1.37 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
export type ModuleDefinition =
| string
| {
target: string
exact?: boolean
}
export type ModuleDefinitions = Record<string, ModuleDefinition>
export const MODULE_DEFINITIONS: ModuleDefinitions = {
dayjs: {
target: 'dayjs/esm',
exact: false,
},
lodash: {
target: 'lodash-es',
exact: false,
},
}
const defEntries = Object.entries(MODULE_DEFINITIONS)
/**
* `entries` option for `rollup-plugin-alias`
*/
export const entries = defEntries.reduce<
Array<{
find: RegExp | string
replacement: string
}>
>((acc, [module, definition]) => {
/* istanbul ignore next */
const { target, exact } =
typeof definition === 'string'
? { target: definition, exact: true }
: definition
acc.push({
find: module,
replacement: target,
})
if (!exact) {
acc.push({
find: new RegExp('^' + module + '/(.*)$'),
replacement: target + '/$1',
})
}
return acc
}, [])
/**
* `alias` option for `resolve` property of `webpack` configuration
*/
export const alias = defEntries.reduce<Record<string, string>>(
(acc, [module, definition]) => {
/* istanbul ignore next */
const { target, exact } =
typeof definition === 'string'
? { target: definition, exact: true }
: definition
acc[/* istanbul ignore next */ exact ? module + '$' : module] = target
return acc
},
{},
)