build-plugins.js 4.75 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
8
9
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
 */

10
11
'use strict'

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

const plugins = [
  babel({
XhmikosR's avatar
XhmikosR committed
20
21
22
    // Only transpile our source code
    exclude: 'node_modules/**',
    // Include only required helpers
23
    externalHelpersWhitelist: babelHelpers
24
25
26
  })
]
const bsPlugins = {
Johann-S's avatar
Johann-S committed
27
  Data: path.resolve(__dirname, '../js/src/dom/data.js'),
28
  EventHandler: path.resolve(__dirname, '../js/src/dom/event-handler.js'),
Johann-S's avatar
Johann-S committed
29
  Manipulator: path.resolve(__dirname, '../js/src/dom/manipulator.js'),
30
  Polyfill: path.resolve(__dirname, '../js/src/dom/polyfill.js'),
31
  SelectorEngine: path.resolve(__dirname, '../js/src/dom/selector-engine.js'),
32
33
34
35
36
37
38
39
40
41
42
  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')
43
}
44
const rootPath = path.resolve(__dirname, '../js/dist/')
45

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

59
const getConfigByPluginKey = pluginKey => {
Johann-S's avatar
Johann-S committed
60
61
62
  if (
    pluginKey === 'Data' ||
    pluginKey === 'Manipulator' ||
Johann-S's avatar
Johann-S committed
63
    pluginKey === 'EventHandler' ||
64
    pluginKey === 'Polyfill' ||
Johann-S's avatar
Johann-S committed
65
    pluginKey === 'SelectorEngine' ||
66
67
    pluginKey === 'Util' ||
    pluginKey === 'Sanitizer'
Johann-S's avatar
Johann-S committed
68
69
  ) {
    return {
70
71
72
73
      external: [bsPlugins.Polyfill],
      globals: {
        [bsPlugins.Polyfill]: 'Polyfill'
      }
Johann-S's avatar
Johann-S committed
74
75
76
77
78
79
    }
  }

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

Johann-S's avatar
Johann-S committed
81
82
83
84
85
86
87
88
89
90
91
  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
92
  }
93

Johann-S's avatar
Johann-S committed
94
95
96
97
98
99
  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
100
  }
101

Johann-S's avatar
Johann-S committed
102
103
104
105
106
  if (pluginKey === 'Popover') {
    return {
      external: [
        bsPlugins.Data,
        bsPlugins.SelectorEngine,
107
        bsPlugins.Tooltip
Johann-S's avatar
Johann-S committed
108
109
110
111
      ],
      globals: {
        [bsPlugins.Data]: 'Data',
        [bsPlugins.SelectorEngine]: 'SelectorEngine',
112
        [bsPlugins.Tooltip]: 'Tooltip'
Johann-S's avatar
Johann-S committed
113
114
      }
    }
XhmikosR's avatar
XhmikosR committed
115
  }
116
117
118
119
120
121

  if (pluginKey === 'Toast') {
    return {
      external: [
        bsPlugins.Data,
        bsPlugins.EventHandler,
122
        bsPlugins.Manipulator
123
124
125
126
      ],
      globals: {
        [bsPlugins.Data]: 'Data',
        [bsPlugins.EventHandler]: 'EventHandler',
127
        [bsPlugins.Manipulator]: 'Manipulator'
128
129
130
      }
    }
  }
Johann-S's avatar
Johann-S committed
131
132
}

133
134
135
136
137
138
139
140
141
const utilObjects = [
  'Util',
  'Sanitizer'
]

const domObjects = [
  'Data',
  'EventHandler',
  'Manipulator',
142
  'Polyfill',
143
144
145
  'SelectorEngine'
]

146
const build = async plugin => {
Johann-S's avatar
Johann-S committed
147
148
  console.log(`Building ${plugin} plugin...`)

XhmikosR's avatar
XhmikosR committed
149
  const { external, globals } = getConfigByPluginKey(plugin)
150
  const pluginFilename = path.basename(bsPlugins[plugin])
151
152
153
154
155
156
157
158
159
  let pluginPath = rootPath

  if (utilObjects.includes(plugin)) {
    pluginPath = `${rootPath}/util/`
  }

  if (domObjects.includes(plugin)) {
    pluginPath = `${rootPath}/dom/`
  }
160

161
  const bundle = await rollup.rollup({
XhmikosR's avatar
XhmikosR committed
162
163
164
    input: bsPlugins[plugin],
    plugins,
    external
165
  })
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186

  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
187
188
}

189
main()