build-plugins.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*!
  2. * Script to build our plugins to use them separately.
  3. * Copyright 2019 The Bootstrap Authors
  4. * Copyright 2019 Twitter, Inc.
  5. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
  6. */
  7. 'use strict'
  8. const path = require('path')
  9. const rollup = require('rollup')
  10. const babel = require('rollup-plugin-babel')
  11. const banner = require('./banner.js')
  12. const TEST = process.env.NODE_ENV === 'test'
  13. const plugins = [
  14. babel({
  15. exclude: 'node_modules/**', // Only transpile our source code
  16. externalHelpersWhitelist: [ // Include only required helpers
  17. 'defineProperties',
  18. 'createClass',
  19. 'inheritsLoose',
  20. 'defineProperty',
  21. 'objectSpread'
  22. ]
  23. })
  24. ]
  25. const bsPlugins = {
  26. Alert: path.resolve(__dirname, '../js/src/alert.js'),
  27. Button: path.resolve(__dirname, '../js/src/button.js'),
  28. Carousel: path.resolve(__dirname, '../js/src/carousel.js'),
  29. Collapse: path.resolve(__dirname, '../js/src/collapse.js'),
  30. Dropdown: path.resolve(__dirname, '../js/src/dropdown.js'),
  31. Modal: path.resolve(__dirname, '../js/src/modal.js'),
  32. Popover: path.resolve(__dirname, '../js/src/popover.js'),
  33. ScrollSpy: path.resolve(__dirname, '../js/src/scrollspy.js'),
  34. Tab: path.resolve(__dirname, '../js/src/tab.js'),
  35. Toast: path.resolve(__dirname, '../js/src/toast.js'),
  36. Tooltip: path.resolve(__dirname, '../js/src/tooltip.js'),
  37. Util: path.resolve(__dirname, '../js/src/util.js')
  38. }
  39. const rootPath = TEST ? '../js/coverage/dist/' : '../js/dist/'
  40. function build(plugin) {
  41. console.log(`Building ${plugin} plugin...`)
  42. const external = ['jquery', 'popper.js']
  43. const globals = {
  44. jquery: 'jQuery', // Ensure we use jQuery which is always available even in noConflict mode
  45. 'popper.js': 'Popper'
  46. }
  47. // Do not bundle Util in plugins
  48. if (plugin !== 'Util') {
  49. external.push(bsPlugins.Util)
  50. globals[bsPlugins.Util] = 'Util'
  51. }
  52. // Do not bundle Tooltip in Popover
  53. if (plugin === 'Popover') {
  54. external.push(bsPlugins.Tooltip)
  55. globals[bsPlugins.Tooltip] = 'Tooltip'
  56. }
  57. const pluginFilename = `${plugin.toLowerCase()}.js`
  58. rollup.rollup({
  59. input: bsPlugins[plugin],
  60. plugins,
  61. external
  62. }).then((bundle) => {
  63. bundle.write({
  64. banner: banner(pluginFilename),
  65. format: 'umd',
  66. name: plugin,
  67. sourcemap: true,
  68. globals,
  69. file: path.resolve(__dirname, `${rootPath}${pluginFilename}`)
  70. })
  71. .then(() => console.log(`Building ${plugin} plugin... Done!`))
  72. .catch((err) => console.error(`${plugin}: ${err}`))
  73. })
  74. }
  75. Object.keys(bsPlugins).forEach((plugin) => build(plugin))