aboutsummaryrefslogtreecommitdiff
path: root/build/build-plugins.js
blob: 8d7744ed2a0fcb0679e7ada3baa33625da16feb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env node

/*!
 * Script to build our plugins to use them separately.
 * Copyright 2020-2021 The Bootstrap Authors
 * Copyright 2020-2021 Twitter, Inc.
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
 */

'use strict'

const path = require('path')
const rollup = require('rollup')
const glob = require('glob')
const { babel } = require('@rollup/plugin-babel')
const banner = require('./banner.js')

const srcPath = path.resolve(__dirname, '../js/src/')

const paths = glob.sync(srcPath + '/**/*.js')

function filenameToEntity(filename) {
  return filename.replace(/(?:^|-)[a-z]/g, char => char.slice(-1).toUpperCase())
}

const resolved = {}
for (const filePath of paths) {
  resolved[filenameToEntity(path.basename(filePath, '.js'))] = {
    src: filePath.replace('.js', ''),
    dist: filePath.replace('src', 'dist'),
    name: path.relative(srcPath, filePath)
  }
}

const plugins = [
  babel({
    // Only transpile our source code
    exclude: 'node_modules/**',
    // Include the helpers in each file, at most one copy of each
    babelHelpers: 'bundled'
  })
]

const build = async pluginKey => {
  const plugin = resolved[pluginKey]

  const globals = {}
  const bundle = await rollup.rollup({
    input: plugin.src,
    plugins,
    external: source => {
      const pattern = /^(\.+)\// // replace starting with ./ or ../

      if (!pattern.test(source)) { // is probably a node plugin
        globals[source] = source
        return true
      }

      // eslint-disable-next-line no-unused-vars
      const usedPlugin = Object.entries(resolved).find(([key, p]) => {
        return p.src.includes(source.replace(pattern, ''))
      })

      if (!usedPlugin) {
        console.warn(`Source ${source} is not mapped`)
        return false
      }

      globals[path.normalize(usedPlugin[1].src)] = usedPlugin[0]
      return true
    }
  })

  await bundle.write({
    banner: banner(plugin.name),
    format: 'umd',
    name: pluginKey,
    sourcemap: true,
    globals,
    file: plugin.dist
  })

  console.log(`Building ${pluginKey} plugin... Done!`)
}

(async () => {
  try {
    const basename = path.basename(__filename)
    const timeLabel = `[${basename}] finished`

    console.log('Building individual plugins...')
    console.time(timeLabel)

    await Promise.all(Object.keys(resolved).map(plugin => build(plugin)))

    console.timeEnd(timeLabel)
  } catch (error) {
    console.error(error)
    process.exit(1)
  }
})()