Skip to content
This repository has been archived by the owner on Jun 10, 2018. It is now read-only.

Add optional support for compressing with jsmin #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 25 additions & 14 deletions lib/stitch.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@
, "devDependencies" :
{ "coffee-script" : ">= 1.0.0"
, "nodeunit" : ">= 0.5.0"
, "jsmin" : ">= 1.0.0"
}
}
12 changes: 10 additions & 2 deletions src/stitch.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,18 @@ try
module._compile content, filename
catch err

try
jsmin = require('jsmin').jsmin
catch err


exports.Package = class Package
constructor: (config) ->
@identifier = config.identifier ? 'require'
@paths = config.paths ? ['lib']
@dependencies = config.dependencies ? []
@compilers = _.extend {}, compilers, config.compilers
@compress = (config.compress and jsmin) or false

@cache = config.cache ? true
@mtimeCache = {}
Expand All @@ -39,9 +44,12 @@ exports.Package = class Package
async.parallel [
@compileDependencies
@compileSources
], (err, parts) ->
], (err, parts) =>
if err then callback err
else callback null, parts.join("\n")
else
sources = parts.join("\n")
sources = jsmin sources if @compress
callback null, sources

compileDependencies: (callback) =>
async.map @dependencies, fs.readFile, (err, dependencySources) =>
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/compress/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function() {
console.log("hello, world!");
}
27 changes: 21 additions & 6 deletions test/test_stitch.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ sys = require "sys"
fs = require "fs"
stitch = require "stitch"

fixtureRoot = __dirname + "/fixtures"
fixtures = fixtureRoot + "/default"
altFixtures = fixtureRoot + "/alternate"
addlFixtures = fixtureRoot + "/additional"
ecoFixtures = fixtureRoot + "/eco"
linkFixtures = fixtureRoot + "/link"
fixtureRoot = __dirname + "/fixtures"
fixtures = fixtureRoot + "/default"
altFixtures = fixtureRoot + "/alternate"
addlFixtures = fixtureRoot + "/additional"
ecoFixtures = fixtureRoot + "/eco"
linkFixtures = fixtureRoot + "/link"
compressFixtures = fixtureRoot + "/compress"
fixtureCount = 15

defaultOptions =
Expand Down Expand Up @@ -44,6 +45,12 @@ linkOptions =
paths: [linkFixtures]
linkPackage = stitch.createPackage linkOptions

compressOptions =
identifier: "testRequire"
paths: [compressFixtures]
compress: true
compressPackage = stitch.createPackage compressOptions


load = (source, callback) ->
(-> eval source).call module = {}
Expand Down Expand Up @@ -187,6 +194,14 @@ module.exports =
test.same "biz", module.baz
test.done()

"compile and compress with jsmin": (test) ->
test.expect 2

compressPackage.compile (err, sources) ->
test.ok !err
test.ok sources.indexOf('function(){console.log("hello, world!");}') > 0
test.done()

"runtime require only loads files once": (test) ->
test.expect 3

Expand Down