2017-04-05 07:45:39 +02:00
|
|
|
const browserify = require('browserify')
|
|
|
|
const watchify = require('watchify')
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
serveBundle,
|
|
|
|
createBundle,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-05 08:03:47 +02:00
|
|
|
function serveBundle (server, path, bundle) {
|
|
|
|
server.get(path, function (req, res) {
|
2017-04-05 07:45:39 +02:00
|
|
|
res.setHeader('Content-Type', 'application/javascript; charset=UTF-8')
|
|
|
|
res.send(bundle.latest)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-10-05 08:03:47 +02:00
|
|
|
function createBundle (entryPoint) {
|
2017-04-05 07:45:39 +02:00
|
|
|
|
|
|
|
var bundleContainer = {}
|
|
|
|
|
|
|
|
var bundler = browserify({
|
|
|
|
entries: [entryPoint],
|
|
|
|
cache: {},
|
|
|
|
packageCache: {},
|
|
|
|
plugin: [watchify],
|
2018-01-12 03:15:22 +01:00
|
|
|
})
|
|
|
|
.transform('babelify')
|
|
|
|
.transform('uglifyify', { global: true })
|
2017-04-05 07:45:39 +02:00
|
|
|
|
|
|
|
bundler.on('update', bundle)
|
|
|
|
bundle()
|
|
|
|
|
|
|
|
return bundleContainer
|
|
|
|
|
2017-10-05 08:03:47 +02:00
|
|
|
function bundle () {
|
|
|
|
bundler.bundle(function (err, result) {
|
2017-04-05 07:45:39 +02:00
|
|
|
if (err) {
|
|
|
|
console.log(`Bundle failed! (${entryPoint})`)
|
|
|
|
console.error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
console.log(`Bundle updated! (${entryPoint})`)
|
|
|
|
bundleContainer.latest = result.toString()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|