build-plugins.js 4.71 KB
Newer Older
1
2
/*!
 * Script to build our plugins to use them separately.
XhmikosR's avatar
XhmikosR committed
3
4
 * Copyright 2019 The Bootstrap Authors
 * Copyright 2019 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
15
16

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

Johann-S's avatar
Johann-S committed
49
50
51
52
const defaultPluginConfig = {
  external: [
    bsPlugins.Data,
    bsPlugins.EventHandler,
53
    bsPlugins.SelectorEngine
Johann-S's avatar
Johann-S committed
54
55
56
57
  ],
  globals: {
    [bsPlugins.Data]: 'Data',
    [bsPlugins.EventHandler]: 'EventHandler',
58
    [bsPlugins.SelectorEngine]: 'SelectorEngine'
Johann-S's avatar
Johann-S committed
59
60
61
62
63
64
65
  }
}

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

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

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

Johann-S's avatar
Johann-S committed
97
98
99
100
101
102
  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
103
  }
104

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

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

136
137
138
139
140
141
142
143
144
const utilObjects = [
  'Util',
  'Sanitizer'
]

const domObjects = [
  'Data',
  'EventHandler',
  'Manipulator',
145
  'Polyfill',
146
147
148
  'SelectorEngine'
]

Johann-S's avatar
Johann-S committed
149
150
151
function build(plugin) {
  console.log(`Building ${plugin} plugin...`)

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

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

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

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

182
183
Object.keys(bsPlugins)
  .forEach(plugin => build(plugin))