build-plugins.js 5.09 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'

Johann-S's avatar
Johann-S 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

XhmikosR's avatar
XhmikosR committed
15
const TEST    = process.env.NODE_ENV === 'test'
16
17
18
19
20
21
22
23
24
25
26
27
28
const plugins = [
  babel({
    exclude: 'node_modules/**', // Only transpile our source code
    externalHelpersWhitelist: [ // Include only required helpers
      'defineProperties',
      'createClass',
      'inheritsLoose',
      'defineProperty',
      'objectSpread'
    ]
  })
]
const bsPlugins = {
Johann-S's avatar
Johann-S committed
29
30
31
32
33
  Data: path.resolve(__dirname, '../js/src/dom/data.js'),
  EventHandler: path.resolve(__dirname, '../js/src/dom/eventHandler.js'),
  Manipulator: path.resolve(__dirname, '../js/src/dom/manipulator.js'),
  Polyfill: path.resolve(__dirname, '../js/src/dom/polyfill.js'),
  SelectorEngine: path.resolve(__dirname, '../js/src/dom/selectorEngine.js'),
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'),
43
  Toast: path.resolve(__dirname, '../js/src/toast.js'),
44
  Tooltip: path.resolve(__dirname, '../js/src/tooltip.js')
45
}
XhmikosR's avatar
XhmikosR committed
46
const rootPath = TEST ? '../js/coverage/dist/' : '../js/dist/'
47

48
49
50
51
52
if (TEST) {
  bsPlugins.Util = path.resolve(__dirname, '../js/src/util/index.js')
  bsPlugins.Sanitizer = path.resolve(__dirname, '../js/src/util/sanitizer.js')
}

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

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

  if (pluginKey === 'EventHandler' || pluginKey === 'SelectorEngine') {
    return {
      external: [
83
        bsPlugins.Polyfill
Johann-S's avatar
Johann-S committed
84
85
      ],
      globals: {
86
        [bsPlugins.Polyfill]: 'Polyfill'
Johann-S's avatar
Johann-S committed
87
88
89
90
91
92
93
      }
    }
  }

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

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

Johann-S's avatar
Johann-S committed
108
109
110
111
112
113
  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
114
  }
115

Johann-S's avatar
Johann-S committed
116
117
118
119
120
  if (pluginKey === 'Popover') {
    return {
      external: [
        bsPlugins.Data,
        bsPlugins.SelectorEngine,
121
        bsPlugins.Tooltip
Johann-S's avatar
Johann-S committed
122
123
124
125
      ],
      globals: {
        [bsPlugins.Data]: 'Data',
        [bsPlugins.SelectorEngine]: 'SelectorEngine',
126
        [bsPlugins.Tooltip]: 'Tooltip'
Johann-S's avatar
Johann-S committed
127
128
      }
    }
XhmikosR's avatar
XhmikosR committed
129
  }
130
131
132
133
134
135

  if (pluginKey === 'Toast') {
    return {
      external: [
        bsPlugins.Data,
        bsPlugins.EventHandler,
136
        bsPlugins.Manipulator
137
138
139
140
      ],
      globals: {
        [bsPlugins.Data]: 'Data',
        [bsPlugins.EventHandler]: 'EventHandler',
141
        [bsPlugins.Manipulator]: 'Manipulator'
142
143
144
      }
    }
  }
Johann-S's avatar
Johann-S committed
145
146
147
148
149
150
151
152
}

function build(plugin) {
  console.log(`Building ${plugin} plugin...`)

  const config = getConfigByPluginKey(plugin)
  const external = config.external
  const globals = config.globals
153
154
155
156
157
158
  let pluginPath = rootPath

  const utilObjects = [
    'Util',
    'Sanitizer'
  ]
Johann-S's avatar
Johann-S committed
159

160
  const domObjects = [
Johann-S's avatar
Johann-S committed
161
162
163
164
165
    'Data',
    'EventHandler',
    'Manipulator',
    'Polyfill',
    'SelectorEngine'
166
167
168
169
170
171
172
173
174
  ]

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

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

XhmikosR's avatar
XhmikosR committed
176
  const pluginFilename = `${plugin.toLowerCase()}.js`
177

XhmikosR's avatar
XhmikosR committed
178
179
180
181
182
183
184
185
186
187
188
  rollup.rollup({
    input: bsPlugins[plugin],
    plugins,
    external
  }).then((bundle) => {
    bundle.write({
      banner: banner(pluginFilename),
      format: 'umd',
      name: plugin,
      sourcemap: true,
      globals,
Johann-S's avatar
Johann-S committed
189
      file: path.resolve(__dirname, `${pluginPath}${pluginFilename}`)
190
    })
XhmikosR's avatar
XhmikosR committed
191
192
      .then(() => console.log(`Building ${plugin} plugin... Done!`))
      .catch((err) => console.error(`${plugin}: ${err}`))
193
  })
XhmikosR's avatar
XhmikosR committed
194
195
196
}

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