Skip to content

Commit

Permalink
browserify!
Browse files Browse the repository at this point in the history
  • Loading branch information
jnordberg committed Apr 26, 2012
1 parent ac2c44a commit 6eae241
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 1 deletion.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
wintersmith-browserify
======================

browserify plugin for wintersmith
[browserify](https://github.com/substack/node-browserify) plugin for [wintersmith](https://github.com/jnordberg/wintersmith)

install:

`npm install wintersmith-browserify -g`
then add `wintersmith-browserify` to plugins in your wintersmith config

![browserify!](http://substack.net/images/browserify/browserify.png)

:-D
3 changes: 3 additions & 0 deletions example/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": ["./../"]
}
3 changes: 3 additions & 0 deletions example/contents/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"template": "index.jade"
}
1 change: 1 addition & 0 deletions example/contents/message.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello from message.txt
5 changes: 5 additions & 0 deletions example/contents/scripts/foo/bar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

exports.messages = [
'hello from bar.js',
require('./baz')
];
2 changes: 2 additions & 0 deletions example/contents/scripts/foo/baz.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

module.exports = 'hello from baz.coffee'
2 changes: 2 additions & 0 deletions example/contents/scripts/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

window.addEventListener('load', require('./test').main, false);
22 changes: 22 additions & 0 deletions example/contents/scripts/test.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

bar = require './foo/bar'
http = require 'http'
url = require 'url'

exports.main = ->
log = (message) ->
@el ?= document.getElementById 'log'
@el.innerHTML += "#{ message }\n"

# log messages comming from other files
log message for message in bar.messages

# setup a http request using browserify's http module (which works exactly like nodes)
request = http.get
path: '/message.txt'
, (response) ->
buffer = []
response.on 'data', (chunk) ->
buffer.push chunk
response.on 'end', () ->
log buffer.join ''
8 changes: 8 additions & 0 deletions example/templates/index.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
!!! 5
html(lang='en')
head
meta(charset='utf-8')
title Browserify!
script(src='scripts/main.js')
body
pre#log
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require('coffee-script');
module.exports = require('./plugin');
11 changes: 11 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "wintersmith-browserify",
"version": "0.1.0",
"author": "Johan Nordberg <its@johan-nordberg.com>",
"description": "browserify for witnersmith",
"license": "MIT",
"dependencies": {
"browserify": ">= 0.0.1",
"coffee-script": ">= 1.1.0"
}
}
35 changes: 35 additions & 0 deletions plugin.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

browserify = require 'browserify'
path = require 'path'

module.exports = (wintersmith, callback) ->

class BrowserifyPlugin extends wintersmith.ContentPlugin

constructor: (@_filename, @_base) ->

getFilename: ->
@_filename

render: (locals, contents, templates, callback) ->
bundle = browserify
cache: false
watch: false

bundle.addListener 'syntaxError', (error) ->
callback error
# unset callback so we don't call it twice
callback = null

# wrap in try catch since coffeescript parse errors will throw..
try
bundle.addEntry path.join(@_base, @_filename)
callback? null, new Buffer bundle.bundle()
catch error
callback? error

BrowserifyPlugin.fromFile = (filename, base, callback) ->
callback null, new BrowserifyPlugin filename, base

wintersmith.registerContentPlugin 'scripts', '**/*.js', BrowserifyPlugin
callback()

0 comments on commit 6eae241

Please sign in to comment.