aboutsummaryrefslogtreecommitdiffstats
path: root/library/server.js
blob: c7fea085fce0d5ec104d541212c02b1bf83f697c (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
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
const express = require('express')
const browserify = require('browserify')
const watchify = require('watchify')
const babelify = require('babelify')
const path = require('path')

const zeroBundle = createBundle('./index.js')
const controllerBundle = createBundle('./controller.js')
const appBundle = createBundle('./example/index.js')

//
// Iframe Server
//

// beefy frame.js:bundle.js 9001 --live -- -t [ babelify --global --presets [ es2015 ] ]

const iframeServer = express()

// serve controller bundle
iframeServer.get('/controller.js', function(req, res){
  res.send(controllerBundle.latest)
})

// serve static
iframeServer.use(express.static('./server'))

iframeServer.listen('9001')


//
// Dapp Server
//

// beefy example/index.js:bundle.js index.js:zero.js --cwd example/ 9002 --live --open -- -t [ babelify --global --presets [ es2015 ] ]

const dappServer = express()


// serve metamask-lib bundle
dappServer.get('/zero.js', function(req, res){
  res.send(zeroBundle.latest)
})

// serve dapp bundle
dappServer.get('/app.js', function(req, res){
  res.send(appBundle.latest)
})

// serve static
dappServer.use(express.static('./example'))


dappServer.listen('9002')


function createBundle(entryPoint){

  var bundleContainer = {}

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

  var bablePreset = path.resolve(__dirname, '../node_modules/babel-preset-es2015')

  bundler.transform(babelify, {
    global: true,
    presets: [bablePreset],
  })
  

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

  return bundleContainer

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

}