Skip to content

Instantly share code, notes, and snippets.

@radiosilence
Last active December 20, 2015 16:09
Show Gist options
  • Save radiosilence/6159565 to your computer and use it in GitHub Desktop.
Save radiosilence/6159565 to your computer and use it in GitHub Desktop.
fs = require 'fs'
{exec} = require 'child_process'
util = require 'util'
browserify = require 'browserify'
colors = require 'colors'
UglifyJS = require "uglify-js"
mold = require 'mold-source-map'
path = require 'path'
jsRoot = path.join(__dirname)
bundleBase = 'hodor'
bundlePath = "#{bundleBase}.js"
mapFilePath = "#{bundlePath}.map"
bundleMinPath = "#{bundleBase}.min.js"
mapFileMinPath = "#{bundleMinPath}.map"
task 'build', 'Prepare project for distribution.', ->
mapPathRelativeTo = (root) ->
map = (file) ->
path.relative(root, file)
mapFileUrlComment = (sourcemap, cb) ->
sourcemap.mapSources mapPathRelativeTo(jsRoot)
sourcemap.file mapFilePath
# write map file and return a sourceMappingUrl that points to it
fs.writeFile mapFilePath, sourcemap.toJSON(2), "utf-8", (err) ->
return util.error(err) if err
cb "//@ sourceMappingURL=#{path.basename(mapFilePath)}"
minify = ->
util.log "Uglifying..."
# exec "uglifyjs --in-source-map #{mapFilePath} --source-map hodor.min.js.map -o hodor.min.js #{bundlePath}"
minified = UglifyJS.minify bundlePath,
outSourceMap: mapFileMinPath
inSourceMap: mapFilePath
warnings: true
mangle: true
compress: true
fs.writeFile bundleMinPath, """#{minified.code}
//@ sourceMappingURL=#{path.basename(mapFileMinPath)}
;
"""
fs.writeFile mapFileMinPath, minified.map
bundle = ->
util.log "Bundling..."
b = browserify()
b.transform('coffeeify')
b.add "./#{bundleBase}.coffee"
b.bundle
debug: true
.on('error', (error) ->
util.error "#{error}".red)
.pipe(mold.transform mapFileUrlComment)
.pipe(fs.createWriteStream(bundlePath))
.on('finish', minify)
bundle()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment