1
export type ModuleDefinition =
2
  | string
3
  | {
4
      target: string
5
      exact?: boolean
6
    }
7

8
// eslint-disable-next-line @typescript-eslint/no-type-alias
9
export type ModuleDefinitions = Record<string, ModuleDefinition>
10

11 1
export const MODULE_DEFINITIONS: ModuleDefinitions = {
12
  dayjs: {
13
    target: 'dayjs/esm',
14
    exact: false,
15
  },
16
  lodash: {
17
    target: 'lodash-es',
18
    exact: false,
19
  },
20
  marked: 'marked/lib/marked.esm',
21
  systemjs: 'systemjs/dist/system',
22
}
23

24 1
const defEntries = Object.entries(MODULE_DEFINITIONS)
25

26
/**
27
 * `entries` option for `rollup-plugin-alias`
28
 */
29 1
export const entries = defEntries.reduce<
30
  Array<{
31
    find: string | RegExp
32
    replacement: string
33
  }>
34 1
>((acc, [module, definition]) => {
35 1
  const { target, exact } =
36 1
    typeof definition === 'string'
37 1
      ? { target: definition, exact: true }
38
      : definition
39

40 1
  acc.push({
41
    find: module,
42
    replacement: target,
43
  })
44

45 1
  if (!exact) {
46 1
    acc.push({
47
      find: new RegExp('^' + module + '/(.*)$'),
48
      replacement: target + '/$1',
49
    })
50
  }
51

52 1
  return acc
53
}, [])
54

55
/**
56
 * `alias` option for `resolve` property of `webpack` configuration
57
 */
58 1
export const alias = defEntries.reduce<Record<string, string>>(
59 1
  (acc, [module, definition]) => {
60 1
    const { target, exact } =
61 1
      typeof definition === 'string'
62 1
        ? { target: definition, exact: true }
63
        : definition
64 1
    acc[exact ? module + '$' : module] = target
65 1
    return acc
66
  },
67
  {},
68
)

Read our documentation on viewing source code .

Loading