Skip to content

Commit

Permalink
perf(lib): boost performance by minifying the published javascript fi…
Browse files Browse the repository at this point in the history
…les in the lib directory
  • Loading branch information
jyounce committed Apr 1, 2016
1 parent 7f7fd95 commit 9b2a6a0
Showing 1 changed file with 57 additions and 29 deletions.
86 changes: 57 additions & 29 deletions scripts/create-lib/create-lib.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,46 +3,74 @@
module.exports = (rbRoot) -> module.exports = (rbRoot) ->
gulp = require 'gulp' gulp = require 'gulp'
coffee = require 'gulp-coffee' coffee = require 'gulp-coffee'
minifyJs = require 'gulp-uglify'
async = require 'asyncawait/async' async = require 'asyncawait/async'
await = require 'asyncawait/await' await = require 'asyncawait/await'
Promise = require 'bluebird' Promise = require 'bluebird'
fse = Promise.promisifyAll require 'fs-extra' fse = Promise.promisifyAll require 'fs-extra'


SRC = "#{rbRoot}/src" SRC = "#{rbRoot}/src"
LIB = "#{rbRoot}/lib" # lib is created from src LIB = "#{rbRoot}/lib" # lib is created from src
LIB_GLOB = "#{LIB}/**/*.coffee" glob =
lib:
js: "#{LIB}/**/*.js"
coffee: "#{LIB}/**/*.coffee"


# tasks # tasks
# ===== # =====
cleanLib = -> tasks =
fse.removeAsync LIB cleanLib: ->

fse.removeAsync(LIB).then ->
copySrc = -> console.log 'lib cleaned'.info
fse.copyAsync SRC, LIB

copySrc: ->
compileLibCoffee = -> fse.copyAsync(SRC, LIB).then ->
src = LIB_GLOB console.log 'copied src to lib'.info
dest = LIB
opts = bare: true compileLibCoffee: ->
new Promise (resolve, reject) -> src = glob.lib.coffee
gulp.src src dest = LIB
.pipe coffee opts opts = bare: true
.on 'error', (e) -> promise = new Promise (resolve, reject) ->
e.message += "\nFile: #{e.filename}" gulp.src src
reject e .pipe coffee opts
.pipe gulp.dest dest .on 'error', (e) ->
.on 'end', -> resolve() e.message += "\nFile: #{e.filename}"

reject e
cleanLibCoffeeFiles = -> .pipe gulp.dest dest
fse.removeAsync LIB_GLOB .on 'end', ->
resolve()
promise.then ->
console.log 'compiled lib coffee files'.info

cleanLibCoffeeFiles: ->
fse.removeAsync(glob.lib.coffee).then ->
console.log 'cleaned lib coffee files'.info

minifyJs: ->
src = glob.lib.js
dest = LIB
opts = mangle: true
promise = new Promise (resolve, reject) ->
gulp.src src
.pipe minifyJs opts
.on 'error', (e) ->
e.message += "\nFile: #{e.filename}"
reject e
.pipe gulp.dest dest
.on 'end', ->
resolve()
promise.then ->
console.log 'minified lib js files'.info


# run tasks (in order) # run tasks (in order)
# ========= # ====================
runTasks = async -> runTasks = async ->
await cleanLib().then -> console.log 'lib cleaned'.info await tasks.cleanLib()
await copySrc().then -> console.log 'copied src to lib'.info await tasks.copySrc()
await compileLibCoffee().then -> console.log 'compiled lib coffee files'.info await tasks.compileLibCoffee()
await cleanLibCoffeeFiles().then -> console.log 'cleaned lib coffee files'.info await tasks.cleanLibCoffeeFiles()
await tasks.minifyJs()


# run it! # run it!
# ======= # =======
Expand Down

0 comments on commit 9b2a6a0

Please sign in to comment.