aboutsummaryrefslogtreecommitdiffstats
path: root/mascara/server/util.js
blob: af2daddb96ad021c562e0c09a55860fcebde65d4 (plain) (blame)
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
const browserify = require('browserify')
const watchify = require('watchify')

module.exports = {
  serveBundle,
  createBundle,
}


function serveBundle (server, path, bundle) {
  server.get(path, function (req, res) {
    res.setHeader('Content-Type', 'application/javascript; charset=UTF-8')
    res.send(bundle.latest)
  })
}

function createBundle (entryPoint) {

  var bundleContainer = {}

  var bundler = browserify({
    entries: [entryPoint],
    cache: {},
    packageCache: {},
    plugin: [watchify],
  }).transform('babelify')

  bundler.on('update', bundle)
  bundle()

  return bundleContainer

  function bundle () {
    bundler.bundle(function (err, result) {
      if (err) {
        console.log(`Bundle failed! (${entryPoint})`)
        console.error(err)
        return
      }
      console.log(`Bundle updated! (${entryPoint})`)
      bundleContainer.latest = result.toString()
    })
  }

}