build-plugins.js 4.77 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
26
27
28
29
30
      'defineProperties',
      'createClass',
      'inheritsLoose',
      'defineProperty',
      'objectSpread'
    ]
  })
]
const bsPlugins = {
Johann-S's avatar
Johann-S committed
31
32
33
34
  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'),
  SelectorEngine: path.resolve(__dirname, '../js/src/dom/selectorEngine.js'),
35
36
37
38
39
40
41
42
43
  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'),
44
  Toast: path.resolve(__dirname, '../js/src/toast.js'),
45
  Tooltip: path.resolve(__dirname, '../js/src/tooltip.js')
46
}
XhmikosR's avatar
XhmikosR committed
47
const rootPath = TEST ? '../js/coverage/dist/' : '../js/dist/'
48

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

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

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

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

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

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

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

138
139
140
141
142
143
144
145
146
147
148
149
const utilObjects = [
  'Util',
  'Sanitizer'
]

const domObjects = [
  'Data',
  'EventHandler',
  'Manipulator',
  'SelectorEngine'
]

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

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

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

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

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

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