Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit 081de61fee88d6fb701de27841de6ab5f5deff93 @mdlawson committed Mar 22, 2013
Showing with 261 additions and 0 deletions.
  1. +16 −0 .gitignore
  2. +7 −0 LICENSE.md
  3. +41 −0 README.md
  4. +109 −0 lib/piping.js
  5. +28 −0 package.json
  6. +60 −0 src/piping.coffee
@@ -0,0 +1,16 @@
+lib-cov
+*.seed
+*.log
+*.csv
+*.dat
+*.out
+*.pid
+*.gz
+
+pids
+logs
+results
+test
+
+npm-debug.log
+node_modules
@@ -0,0 +1,7 @@
+Copyright (C) 2013 Michael Lawson
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,41 @@
+# Piping-browser
+
+There are already wrappers such as [brunch](https://github.com/brunch/brunch/) that offer to watch and rebuild all your client side files, and optionally launch a server for you. I wanted to launch my server and have it watch my client files and rebuild them (as well as itself, see [piping](http://github.com/mdlawson/piping))
+
+Piping-browser uses [browserify](https://github.com/substack/node-browserify) to package up your client side modules using commonjs. Browserify also gives us sourcemaps and support for node modules for free!
+
+## Installation
+```
+npm install piping-browser
+```
+## Usage
+
+Piping-browser is not a binary, so you can continue using your current workflow for running your application ("wooo!"). Basic usage is as follows:
+
+```javascript
+require("piping-browser")({main:"./client/scripts/main.js",out:"./public/application.js"})
+```
+### Options
+
+- __main__ _(path)_: The path to the entry point of your application. this file is automatically executed on load. Relative to the file where piping-browser was required
+- __out__ _(path)_: The path to where you want your bundled code to be written to. Relative to the file where piping-browser was required
+- __ignore__ _(regex)_: Files/paths matching this regex will not be watched. Defaults to `/(\/\.|~$)/`
+- __watch__ _(boolean)_: Whether or not piping should rebuild on changes. Defaults to true, could be set to false for production
+- __debug__ _(boolean)_: Whether browserify should run in debug mode or not. Debug mode turns on source maps. Defaults to true
+- __build__ _(object)_: An object that maps file extensions, eg "coffee" to functions that take a filename and the files data and compiles it to javascript. By default can compile coffeescript files, with sourcemaps .
+
+
+Piping-browser can also be used just by passing two strings. In this case, the strings are taken as the main and out options
+```javascript
+require("piping-browser")("./client/scripts/main.js","./public/application.js")
+```
+
+piping-browser plays nice with piping. To use it, ensure piping-browser is required when piping returns false:
+
+```javascript
+if(require("piping")()){
+ require("piping-browser")("./client/scripts/main.js","./public/application.js")
+ return
+}
+// application logic here
+```

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -0,0 +1,28 @@
+{
+ "name": "piping-browser",
+ "version": "0.1.0",
+ "description": "Keep your code piping hot! Rebuild your client side code on change without binaries",
+ "main": "lib/piping.js",
+ "dependencies": {
+ "chokidar": "0.5.x",
+ "colors": "0.6.x",
+ "browserify": "2.7.x",
+ "convert-source-map": "0.2.x",
+ "through": "2.2.x"
+ },
+ "devDependencies": {
+ "coffee-script": "~1.6.0"
+ },
+ "repository": "git://github.com/mdlawson/piping-browser.git",
+ "keywords": [
+ "live",
+ "code",
+ "reload",
+ "hot",
+ "browserify",
+ "browser",
+ "watch"
+ ],
+ "author": "Michael Lawson",
+ "license": "MIT"
+}
@@ -0,0 +1,60 @@
+path = require "path"
+fs = require "fs"
+colors = require "colors"
+browserify = require "browserify"
+chokidar = require "chokidar"
+through = require "through"
+convertSourceMap = require "convert-source-map"
+
+options =
+ ignore: /(\/\.|~$)/
+ watch: true
+ debug: true
+ build:
+ "coffee": (file,data) ->
+ coffee = require "coffee-script"
+ compiled = coffee.compile data, {sourceMap: true, generatedFile: true, inline: true}
+ comment = convertSourceMap.fromJSON(compiled.v3SourceMap).setProperty("sources", [ file ]).toComment()
+ return compiled.js + "\n" + comment
+
+
+module.exports = (ops,out) ->
+ if (typeof ops is "string" or ops instanceof String) and (typeof out is "string" or out instanceof String)
+ options.main = ops
+ options.out = out
+ else
+ options[key] = value for key,value of ops when key isnt "build"
+ if opts.build
+ options.build[key] = value for key,value of opts.build
+
+ basedir = path.dirname module.parent.filename
+
+ watcher = chokidar.watch path.resolve(basedir,options.main),
+ ignored: options.ignore
+ ignoreInitial: true
+ persistent: true
+ bundle = (i,o) ->
+ start = Date.now()
+ browserify().transform((file) ->
+ watcher.add file
+ for ext,func of options.build
+ if RegExp("\.#{ext}$").test file
+ data = ""
+ write = (buf) -> data += buf
+ end = ->
+ @queue func(file,data)
+ @queue null
+ return through write, end
+ return through()
+ ).require(path.resolve(basedir,i),{entry:true}).bundle(debug:options.debug)
+ .on("error", (err) -> console.log "[piping-browser]".bold.yellow,"Error:",err)
+ .on("end",-> console.log "[piping-browser]".bold.yellow,"Built in",Date.now()-start,"ms")
+ .pipe fs.createWriteStream path.resolve(basedir,o)
+
+
+ watcher.on "change", (file) ->
+ console.log "[piping-browser]".bold.yellow,"File",path.relative(process.cwd(),file),"has changed, rebuilding"
+ if options.watch then bundle options.main, options.out
+
+ bundle options.main, options.out
+

0 comments on commit 081de61

Please sign in to comment.