build-plugins.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #!/usr/bin/env node
  2. /*!
  3. * Script to build our plugins to use them separately.
  4. * Copyright 2020-2021 The Bootstrap Authors
  5. * Copyright 2020-2021 Twitter, Inc.
  6. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
  7. */
  8. 'use strict'
  9. const path = require('path')
  10. const rollup = require('rollup')
  11. const { babel } = require('@rollup/plugin-babel')
  12. const banner = require('./banner.js')
  13. const rootPath = path.resolve(__dirname, '../js/dist/')
  14. const plugins = [
  15. babel({
  16. // Only transpile our source code
  17. exclude: 'node_modules/**',
  18. // Include the helpers in each file, at most one copy of each
  19. babelHelpers: 'bundled'
  20. })
  21. ]
  22. const bsPlugins = {
  23. Data: path.resolve(__dirname, '../js/src/dom/data.js'),
  24. EventHandler: path.resolve(__dirname, '../js/src/dom/event-handler.js'),
  25. Manipulator: path.resolve(__dirname, '../js/src/dom/manipulator.js'),
  26. SelectorEngine: path.resolve(__dirname, '../js/src/dom/selector-engine.js'),
  27. Alert: path.resolve(__dirname, '../js/src/alert.js'),
  28. Base: path.resolve(__dirname, '../js/src/base-component.js'),
  29. Button: path.resolve(__dirname, '../js/src/button.js'),
  30. Carousel: path.resolve(__dirname, '../js/src/carousel.js'),
  31. Collapse: path.resolve(__dirname, '../js/src/collapse.js'),
  32. Dropdown: path.resolve(__dirname, '../js/src/dropdown.js'),
  33. Modal: path.resolve(__dirname, '../js/src/modal.js'),
  34. Offcanvas: path.resolve(__dirname, '../js/src/offcanvas.js'),
  35. Popover: path.resolve(__dirname, '../js/src/popover.js'),
  36. ScrollSpy: path.resolve(__dirname, '../js/src/scrollspy.js'),
  37. Tab: path.resolve(__dirname, '../js/src/tab.js'),
  38. Toast: path.resolve(__dirname, '../js/src/toast.js'),
  39. Tooltip: path.resolve(__dirname, '../js/src/tooltip.js')
  40. }
  41. const defaultPluginConfig = {
  42. external: [
  43. bsPlugins.Data,
  44. bsPlugins.Base,
  45. bsPlugins.EventHandler,
  46. bsPlugins.SelectorEngine
  47. ],
  48. globals: {
  49. [bsPlugins.Data]: 'Data',
  50. [bsPlugins.Base]: 'Base',
  51. [bsPlugins.EventHandler]: 'EventHandler',
  52. [bsPlugins.SelectorEngine]: 'SelectorEngine'
  53. }
  54. }
  55. const getConfigByPluginKey = pluginKey => {
  56. if (
  57. pluginKey === 'Data' ||
  58. pluginKey === 'Manipulator' ||
  59. pluginKey === 'EventHandler' ||
  60. pluginKey === 'SelectorEngine' ||
  61. pluginKey === 'Util' ||
  62. pluginKey === 'Sanitizer' ||
  63. pluginKey === 'Backdrop'
  64. ) {
  65. return {
  66. external: []
  67. }
  68. }
  69. if (pluginKey === 'Alert' || pluginKey === 'Tab' || pluginKey === 'Offcanvas') {
  70. return defaultPluginConfig
  71. }
  72. if (
  73. pluginKey === 'Base' ||
  74. pluginKey === 'Button' ||
  75. pluginKey === 'Carousel' ||
  76. pluginKey === 'Collapse' ||
  77. pluginKey === 'Modal' ||
  78. pluginKey === 'ScrollSpy'
  79. ) {
  80. const config = Object.assign(defaultPluginConfig)
  81. config.external.push(bsPlugins.Manipulator)
  82. config.globals[bsPlugins.Manipulator] = 'Manipulator'
  83. return config
  84. }
  85. if (pluginKey === 'Dropdown' || pluginKey === 'Tooltip') {
  86. const config = Object.assign(defaultPluginConfig)
  87. config.external.push(bsPlugins.Manipulator, '@popperjs/core')
  88. config.globals[bsPlugins.Manipulator] = 'Manipulator'
  89. config.globals['@popperjs/core'] = 'Popper'
  90. return config
  91. }
  92. if (pluginKey === 'Popover') {
  93. return {
  94. external: [
  95. bsPlugins.Data,
  96. bsPlugins.SelectorEngine,
  97. bsPlugins.Tooltip
  98. ],
  99. globals: {
  100. [bsPlugins.Data]: 'Data',
  101. [bsPlugins.SelectorEngine]: 'SelectorEngine',
  102. [bsPlugins.Tooltip]: 'Tooltip'
  103. }
  104. }
  105. }
  106. if (pluginKey === 'Toast') {
  107. return {
  108. external: [
  109. bsPlugins.Data,
  110. bsPlugins.Base,
  111. bsPlugins.EventHandler,
  112. bsPlugins.Manipulator
  113. ],
  114. globals: {
  115. [bsPlugins.Data]: 'Data',
  116. [bsPlugins.Base]: 'Base',
  117. [bsPlugins.EventHandler]: 'EventHandler',
  118. [bsPlugins.Manipulator]: 'Manipulator'
  119. }
  120. }
  121. }
  122. }
  123. const utilObjects = new Set([
  124. 'Util',
  125. 'Sanitizer',
  126. 'Backdrop'
  127. ])
  128. const domObjects = new Set([
  129. 'Data',
  130. 'EventHandler',
  131. 'Manipulator',
  132. 'SelectorEngine'
  133. ])
  134. const build = async plugin => {
  135. console.log(`Building ${plugin} plugin...`)
  136. const { external, globals } = getConfigByPluginKey(plugin)
  137. const pluginFilename = path.basename(bsPlugins[plugin])
  138. let pluginPath = rootPath
  139. if (utilObjects.has(plugin)) {
  140. pluginPath = `${rootPath}/util/`
  141. }
  142. if (domObjects.has(plugin)) {
  143. pluginPath = `${rootPath}/dom/`
  144. }
  145. const bundle = await rollup.rollup({
  146. input: bsPlugins[plugin],
  147. plugins,
  148. external
  149. })
  150. await bundle.write({
  151. banner: banner(pluginFilename),
  152. format: 'umd',
  153. name: plugin,
  154. sourcemap: true,
  155. globals,
  156. file: path.resolve(__dirname, `${pluginPath}/${pluginFilename}`)
  157. })
  158. console.log(`Building ${plugin} plugin... Done!`)
  159. }
  160. const main = async () => {
  161. try {
  162. await Promise.all(Object.keys(bsPlugins).map(plugin => build(plugin)))
  163. } catch (error) {
  164. console.error(error)
  165. process.exit(1)
  166. }
  167. }
  168. main()