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

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

50
51
52
53
54
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
55
56
57
58
const defaultPluginConfig = {
  external: [
    bsPlugins.Data,
    bsPlugins.EventHandler,
59
    bsPlugins.SelectorEngine
Johann-S's avatar
Johann-S committed
60
61
62
63
  ],
  globals: {
    [bsPlugins.Data]: 'Data',
    [bsPlugins.EventHandler]: 'EventHandler',
64
    [bsPlugins.SelectorEngine]: 'SelectorEngine'
Johann-S's avatar
Johann-S committed
65
66
67
68
69
70
71
  }
}

function getConfigByPluginKey(pluginKey) {
  if (
    pluginKey === 'Data' ||
    pluginKey === 'Manipulator' ||
Johann-S's avatar
Johann-S committed
72
    pluginKey === 'EventHandler' ||
73
    pluginKey === 'Polyfill' ||
Johann-S's avatar
Johann-S committed
74
    pluginKey === 'SelectorEngine' ||
75
76
    pluginKey === 'Util' ||
    pluginKey === 'Sanitizer'
Johann-S's avatar
Johann-S committed
77
78
  ) {
    return {
79
80
81
82
      external: [bsPlugins.Polyfill],
      globals: {
        [bsPlugins.Polyfill]: 'Polyfill'
      }
Johann-S's avatar
Johann-S committed
83
84
85
86
87
88
    }
  }

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

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

Johann-S's avatar
Johann-S committed
103
104
105
106
107
108
  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
109
  }
110

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

  if (pluginKey === 'Toast') {
    return {
      external: [
        bsPlugins.Data,
        bsPlugins.EventHandler,
131
        bsPlugins.Manipulator
132
133
134
135
      ],
      globals: {
        [bsPlugins.Data]: 'Data',
        [bsPlugins.EventHandler]: 'EventHandler',
136
        [bsPlugins.Manipulator]: 'Manipulator'
137
138
139
      }
    }
  }
Johann-S's avatar
Johann-S committed
140
141
}

142
143
144
145
146
147
148
149
150
const utilObjects = [
  'Util',
  'Sanitizer'
]

const domObjects = [
  'Data',
  'EventHandler',
  'Manipulator',
151
  'Polyfill',
152
153
154
  'SelectorEngine'
]

Johann-S's avatar
Johann-S committed
155
156
157
function build(plugin) {
  console.log(`Building ${plugin} plugin...`)

XhmikosR's avatar
XhmikosR committed
158
  const { external, globals } = getConfigByPluginKey(plugin)
159
  const pluginFilename = path.basename(bsPlugins[plugin])
160
161
162
163
164
165
166
167
168
  let pluginPath = rootPath

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

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

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

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