build-plugins.js 4.73 KB
Newer Older
1
2
/*!
 * Script to build our plugins to use them separately.
3
4
 * Copyright 2020 The Bootstrap Authors
 * Copyright 2020 Twitter, Inc.
5
6
7
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
 */

8
9
'use strict'

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

const plugins = [
  babel({
XhmikosR's avatar
XhmikosR committed
18
19
20
    // Only transpile our source code
    exclude: 'node_modules/**',
    // Include only required helpers
21
    externalHelpersWhitelist: babelHelpers
22
23
24
  })
]
const bsPlugins = {
Johann-S's avatar
Johann-S committed
25
  Data: path.resolve(__dirname, '../js/src/dom/data.js'),
26
  EventHandler: path.resolve(__dirname, '../js/src/dom/event-handler.js'),
Johann-S's avatar
Johann-S committed
27
  Manipulator: path.resolve(__dirname, '../js/src/dom/manipulator.js'),
28
  Polyfill: path.resolve(__dirname, '../js/src/dom/polyfill.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
58
59
60
  }
}

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

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

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

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

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

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

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

const domObjects = [
  'Data',
  'EventHandler',
  'Manipulator',
140
  'Polyfill',
141
142
143
  'SelectorEngine'
]

Johann-S's avatar
Johann-S committed
144
145
146
function build(plugin) {
  console.log(`Building ${plugin} plugin...`)

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

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

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

XhmikosR's avatar
XhmikosR committed
159
160
161
162
  rollup.rollup({
    input: bsPlugins[plugin],
    plugins,
    external
XhmikosR's avatar
XhmikosR committed
163
  }).then(bundle => {
XhmikosR's avatar
XhmikosR committed
164
165
166
167
168
169
    bundle.write({
      banner: banner(pluginFilename),
      format: 'umd',
      name: plugin,
      sourcemap: true,
      globals,
170
      file: path.resolve(__dirname, `${pluginPath}/${pluginFilename}`)
171
    })
XhmikosR's avatar
XhmikosR committed
172
      .then(() => console.log(`Building ${plugin} plugin... Done!`))
XhmikosR's avatar
XhmikosR committed
173
      .catch(error => console.error(`${plugin}: ${error}`))
174
  })
175
  .catch(error => console.error(`${plugin}: ${error}`))
XhmikosR's avatar
XhmikosR committed
176
177
}

178
179
Object.keys(bsPlugins)
  .forEach(plugin => build(plugin))