build-plugins.js 4.51 KB
Newer Older
1
2
#!/usr/bin/env node

3
4
/*!
 * Script to build our plugins to use them separately.
5
6
 * Copyright 2020 The Bootstrap Authors
 * Copyright 2020 Twitter, Inc.
7
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
8
9
 */

10
11
'use strict'

XhmikosR's avatar
XhmikosR committed
12
13
const path = require('path')
const rollup = require('rollup')
14
const { babel } = require('@rollup/plugin-babel')
XhmikosR's avatar
XhmikosR committed
15
const banner = require('./banner.js')
16
17
18

const plugins = [
  babel({
XhmikosR's avatar
XhmikosR committed
19
20
    // Only transpile our source code
    exclude: 'node_modules/**',
21
22
    // Inline the required helpers in each file
    babelHelpers: 'inline'
23
24
25
  })
]
const bsPlugins = {
Johann-S's avatar
Johann-S committed
26
  Data: path.resolve(__dirname, '../js/src/dom/data.js'),
27
  EventHandler: path.resolve(__dirname, '../js/src/dom/event-handler.js'),
Johann-S's avatar
Johann-S committed
28
  Manipulator: path.resolve(__dirname, '../js/src/dom/manipulator.js'),
29
  SelectorEngine: path.resolve(__dirname, '../js/src/dom/selector-engine.js'),
30
31
32
33
34
35
36
37
38
39
40
  Alert: path.resolve(__dirname, '../js/src/alert.js'),
  Button: path.resolve(__dirname, '../js/src/button.js'),
  Carousel: path.resolve(__dirname, '../js/src/carousel.js'),
  Collapse: path.resolve(__dirname, '../js/src/collapse.js'),
  Dropdown: path.resolve(__dirname, '../js/src/dropdown.js'),
  Modal: path.resolve(__dirname, '../js/src/modal.js'),
  Popover: path.resolve(__dirname, '../js/src/popover.js'),
  ScrollSpy: path.resolve(__dirname, '../js/src/scrollspy.js'),
  Tab: path.resolve(__dirname, '../js/src/tab.js'),
  Toast: path.resolve(__dirname, '../js/src/toast.js'),
  Tooltip: path.resolve(__dirname, '../js/src/tooltip.js')
41
}
42
const rootPath = path.resolve(__dirname, '../js/dist/')
43

Johann-S's avatar
Johann-S committed
44
45
46
47
const defaultPluginConfig = {
  external: [
    bsPlugins.Data,
    bsPlugins.EventHandler,
48
    bsPlugins.SelectorEngine
Johann-S's avatar
Johann-S committed
49
50
51
52
  ],
  globals: {
    [bsPlugins.Data]: 'Data',
    [bsPlugins.EventHandler]: 'EventHandler',
53
    [bsPlugins.SelectorEngine]: 'SelectorEngine'
Johann-S's avatar
Johann-S committed
54
55
56
  }
}

57
const getConfigByPluginKey = pluginKey => {
Johann-S's avatar
Johann-S committed
58
59
60
  if (
    pluginKey === 'Data' ||
    pluginKey === 'Manipulator' ||
Johann-S's avatar
Johann-S committed
61
62
    pluginKey === 'EventHandler' ||
    pluginKey === 'SelectorEngine' ||
63
64
    pluginKey === 'Util' ||
    pluginKey === 'Sanitizer'
Johann-S's avatar
Johann-S committed
65
66
  ) {
    return {
67
      external: []
Johann-S's avatar
Johann-S committed
68
69
70
71
72
73
    }
  }

  if (pluginKey === 'Alert' || pluginKey === 'Tab') {
    return defaultPluginConfig
  }
74

Johann-S's avatar
Johann-S committed
75
76
77
78
79
80
81
82
83
84
85
  if (
    pluginKey === 'Button' ||
    pluginKey === 'Carousel' ||
    pluginKey === 'Collapse' ||
    pluginKey === 'Modal' ||
    pluginKey === 'ScrollSpy'
  ) {
    const config = Object.assign(defaultPluginConfig)
    config.external.push(bsPlugins.Manipulator)
    config.globals[bsPlugins.Manipulator] = 'Manipulator'
    return config
XhmikosR's avatar
XhmikosR committed
86
  }
87

Johann-S's avatar
Johann-S committed
88
89
90
91
92
93
  if (pluginKey === 'Dropdown' || pluginKey === 'Tooltip') {
    const config = Object.assign(defaultPluginConfig)
    config.external.push(bsPlugins.Manipulator, 'popper.js')
    config.globals[bsPlugins.Manipulator] = 'Manipulator'
    config.globals['popper.js'] = 'Popper'
    return config
XhmikosR's avatar
XhmikosR committed
94
  }
95

Johann-S's avatar
Johann-S committed
96
97
98
99
100
  if (pluginKey === 'Popover') {
    return {
      external: [
        bsPlugins.Data,
        bsPlugins.SelectorEngine,
101
        bsPlugins.Tooltip
Johann-S's avatar
Johann-S committed
102
103
104
105
      ],
      globals: {
        [bsPlugins.Data]: 'Data',
        [bsPlugins.SelectorEngine]: 'SelectorEngine',
106
        [bsPlugins.Tooltip]: 'Tooltip'
Johann-S's avatar
Johann-S committed
107
108
      }
    }
XhmikosR's avatar
XhmikosR committed
109
  }
110
111
112
113
114
115

  if (pluginKey === 'Toast') {
    return {
      external: [
        bsPlugins.Data,
        bsPlugins.EventHandler,
116
        bsPlugins.Manipulator
117
118
119
120
      ],
      globals: {
        [bsPlugins.Data]: 'Data',
        [bsPlugins.EventHandler]: 'EventHandler',
121
        [bsPlugins.Manipulator]: 'Manipulator'
122
123
124
      }
    }
  }
Johann-S's avatar
Johann-S committed
125
126
}

XhmikosR's avatar
XhmikosR committed
127
const utilObjects = new Set([
128
129
  'Util',
  'Sanitizer'
XhmikosR's avatar
XhmikosR committed
130
])
131

XhmikosR's avatar
XhmikosR committed
132
const domObjects = new Set([
133
134
135
136
  'Data',
  'EventHandler',
  'Manipulator',
  'SelectorEngine'
XhmikosR's avatar
XhmikosR committed
137
])
138

139
const build = async plugin => {
Johann-S's avatar
Johann-S committed
140
141
  console.log(`Building ${plugin} plugin...`)

XhmikosR's avatar
XhmikosR committed
142
  const { external, globals } = getConfigByPluginKey(plugin)
143
  const pluginFilename = path.basename(bsPlugins[plugin])
144
145
  let pluginPath = rootPath

XhmikosR's avatar
XhmikosR committed
146
  if (utilObjects.has(plugin)) {
147
148
149
    pluginPath = `${rootPath}/util/`
  }

XhmikosR's avatar
XhmikosR committed
150
  if (domObjects.has(plugin)) {
151
152
    pluginPath = `${rootPath}/dom/`
  }
153

154
  const bundle = await rollup.rollup({
XhmikosR's avatar
XhmikosR committed
155
156
157
    input: bsPlugins[plugin],
    plugins,
    external
158
  })
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179

  await bundle.write({
    banner: banner(pluginFilename),
    format: 'umd',
    name: plugin,
    sourcemap: true,
    globals,
    file: path.resolve(__dirname, `${pluginPath}/${pluginFilename}`)
  })

  console.log(`Building ${plugin} plugin... Done!`)
}

const main = async () => {
  try {
    await Promise.all(Object.keys(bsPlugins).map(plugin => build(plugin)))
  } catch (error) {
    console.error(error)

    process.exit(1)
  }
XhmikosR's avatar
XhmikosR committed
180
181
}

182
main()