Skip to content

Commit

Permalink
feat: use browserify to bundle hoodie client
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Dietz committed Jul 24, 2016
1 parent 8a7effe commit d715624
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@hoodie/store": "^1.0.1",
"async": "^2.0.0",
"boom": "^3.1.2",
"browserify": "^13.0.1",
"good": "^6.6.3",
"good-squeeze": "^4.0.0",
"h2o2": "^5.1.0",
Expand Down
21 changes: 14 additions & 7 deletions server/plugins/client/bundle.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = bundleClient

var EventEmitter = require('events').EventEmitter
var fs = require('fs')

var parallel = require('async').parallel

/**
Expand Down Expand Up @@ -52,15 +52,22 @@ function getModifiedTime (path, callback) {
}

function buildBundle (config, hoodieClientPath, callback) {
fs.readFile(hoodieClientPath, function (error, clientBuffer) {
/* istanbul ignore if */
if (error) {
return callback(error)
}
var browserify = require('browserify')([], {
standalone: 'Hoodie'
})

browserify.require(hoodieClientPath)

var bundleEE = new EventEmitter()

bundleEE.on('done', function (error, buffer) {
if (error) throw error

var options = config.url ? '{url: "' + config.url + '"}' : ''
var initBuffer = Buffer('\n\nhoodie = new Hoodie(' + options + ')')

callback(null, Buffer.concat([clientBuffer, initBuffer]))
callback(null, Buffer.concat([buffer, initBuffer]))
})

browserify.bundle(bundleEE.emit.bind(bundleEE, 'done'))
}
2 changes: 1 addition & 1 deletion server/plugins/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var log = require('npmlog')

function register (server, options, next) {
var hoodieClientModulePath = path.dirname(require.resolve('@hoodie/client/package.json'))
var hoodieClientPath = path.join(hoodieClientModulePath, 'dist/hoodie.js')
var hoodieClientPath = path.join(hoodieClientModulePath, 'index.js')
var bundleTargetPath = path.join(options.config.paths.data, 'client.js')
var bundlePromise

Expand Down
26 changes: 20 additions & 6 deletions test/unit/plugins-client-bundle-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,17 @@ test('bundle client', function (group) {
})

group.test('bundle does not exist', function (t) {
var readFileMock = simple.stub().callbackWith(null, new Buffer('hoodie client content'))
var bundleMock = simple.stub().callbackWith(null, new Buffer('hoodie client content'))
var requireMock = simple.stub()

var bundleClient = proxyquire('../../server/plugins/client/bundle', {
browserify: function () {
return {
bundle: bundleMock,
require: requireMock
}
},
fs: {
readFile: readFileMock,
stat: simple.stub().callFn(function (path, callback) {
if (path === 'client.js') {
return callback(null, {mtime: new Date()})
Expand All @@ -40,19 +47,26 @@ test('bundle client', function (group) {
bundleClient('client.js', 'bundle.js', {}, function (error, buffer) {
t.error(error)

t.is(readFileMock.callCount, 1, 'readFile called once')
t.is(readFileMock.lastCall.arg, 'client.js', 'read bundle')
t.is(requireMock.lastCall.arg, 'client.js', 'require client')
t.is(bundleMock.callCount, 1, 'bundle called once')
t.is(buffer.toString(), 'hoodie client content\n\nhoodie = new Hoodie()')

t.end()
})
})

group.test('with client options', function (t) {
var readFileMock = simple.stub().callbackWith(null, new Buffer('hoodie client content'))
var bundleMock = simple.stub().callbackWith(null, new Buffer('hoodie client content'))
var requireMock = simple.stub()

var bundleClient = proxyquire('../../server/plugins/client/bundle', {
browserify: function () {
return {
bundle: bundleMock,
require: requireMock
}
},
fs: {
readFile: readFileMock,
stat: simple.stub().callFn(function (path, callback) {
if (path === 'client.js') {
return callback(null, {mtime: new Date()})
Expand Down

0 comments on commit d715624

Please sign in to comment.