Skip to content

Instantly share code, notes, and snippets.

@shesek
Last active December 11, 2015 19:29
Show Gist options
  • Save shesek/4649068 to your computer and use it in GitHub Desktop.
Save shesek/4649068 to your computer and use it in GitHub Desktop.
Add inline client-side JavaScript with browserify
# Adds a function as an inline entry to browserify
# Useful for short "glue" code that just needs to require a
# bounch of things and initilize them.
get_proto = Object.getPrototypeOf or (o) -> o.__proto__
count = 0
addCode = (b, fn) -> b.addEntry 'main' + (if ++count > 1 then count else '') + '.js', body: addCode.body fn
addCode.body = (fn) -> '(' + fn + ')()'
addCode.install = ->
(get_proto (require 'browserify') {}).addCode = (fn) -> addCode this, fn
this
# there's no sane way to get the Wrap object, I have to call browserify and
# get it from there :(
module.exports = addCode
{
"name": "browserify-add-code",
"version": "0.0.1",
"description": "Add inline client-side JavaScript with browserify",
"main": "browserify-add-code.coffee",
"repository": {
"type": "git",
"url": "https://gist.github.com/4649068.git"
},
"license": "BSD"
}
# Usage
addCode = require 'browserify-add-code'
app.use addCode browserify(), -> (require './client/app').init foo: true, bar: baz
# install() would patch browserify and add an addCode() method
(require 'browserify-add-code').install()
app.use browserify().addCode -> (require './client/app').init foo: true, bar: baz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment