Comparing version 1.0.3 to 1.0.5
var Minifier | ||
, through = require('through') | ||
, optimize = require('./optimize'); | ||
, optimize = require('./optimize') | ||
, concat = require('concat-stream'); | ||
Minifier = function (opts) { | ||
Minifier = function (opts, cb) { | ||
var write | ||
@@ -10,2 +11,9 @@ , end | ||
if(typeof opts == 'function') { | ||
cb = opts; | ||
opts = { | ||
map: 'bundle.map.json' | ||
}; | ||
} | ||
write = function (data) { | ||
@@ -16,8 +24,25 @@ buff += data; | ||
end = function (data) { | ||
var result = optimize(buff, opts); | ||
try { | ||
var result = optimize(buff, opts) | ||
, decoupled; | ||
} | ||
catch(e) { | ||
if(typeof cb == 'function') | ||
return cb(e); | ||
} | ||
// Put the sourcemap inline | ||
result.code = result.code.replace(/\s?;;;[^\t]\/\/@ sourceMappingURL[^\t]*$/, '') + '\n//@ sourceMappingURL=data:application/json;base64,' | ||
+ (new Buffer(result.map)).toString('base64'); | ||
// Strip out existing map | ||
result.code = result.code.replace(/\s?;;;[^\t]\/\/(@|#) sourceMappingURL[^\t]*$/, ''); | ||
if(typeof cb == 'function') { | ||
// Append the URL to the map | ||
result.code += '\n//@ sourceMappingURL=' + opts.map; | ||
cb(null, result.code, result.map); | ||
} | ||
else { | ||
// Append the inline sourcemap | ||
result.code += '\n//@ sourceMappingURL=data:application/json;base64,' | ||
+ (new Buffer(result.map)).toString('base64'); | ||
} | ||
this.queue(result.code); | ||
@@ -24,0 +49,0 @@ this.queue(null); |
@@ -14,3 +14,3 @@ { | ||
], | ||
"version": "1.0.3", | ||
"version": "1.0.5", | ||
"repository": { | ||
@@ -17,0 +17,0 @@ "type": "git", |
@@ -39,2 +39,14 @@ Minifyify | ||
You can also use callbacks to get your code and map in separate files | ||
```js | ||
bundle('entry.js') | ||
.bundle({debug: true}) | ||
.pipe(minifyify(options, function (err, src, map) { | ||
assert.ifError(err); | ||
fs.writeFileSync('bundle.min.js', src); | ||
fs.writeFileSync('bundle.min.map.json', map); | ||
})) | ||
``` | ||
## Options | ||
@@ -57,2 +69,8 @@ | ||
* Wait.. Why did my bundle get BIGGER?? | ||
It's not immediately obvious, but the more you minify code, the bigger the sourcemap gets. Browserify can get away with merely mapping lines to lines because it is going from uncompressed code to uncompressed code. Minifyify squishes multiple lines together, so the sourcemap has to carry more information. | ||
**Pull the sourcemap out into a separate file and link to it from the minified source!** | ||
* How does this work? | ||
@@ -59,0 +77,0 @@ |
@@ -28,3 +28,3 @@ var _ = require('lodash') | ||
compileApp = function (appname, next) { | ||
compileApp = function (appname, method, next) { | ||
var bundle = new browserify() | ||
@@ -41,10 +41,22 @@ , deps = {} | ||
bundle | ||
.transform(require('hbsfy')) | ||
.bundle({debug: true}) | ||
.pipe(minifyify(opts)) | ||
.pipe(concat(function (data) { | ||
var decoupled = decouple(data, {noConsumer: true, map: opts.map}); | ||
next(decoupled.code, decoupled.map); | ||
})); | ||
bundle = bundle | ||
.transform(require('hbsfy')) | ||
.bundle({debug: true}) | ||
if(method == 'stream') { | ||
bundle | ||
.pipe(minifyify(opts)) | ||
.pipe(concat(function (data) { | ||
var decoupled = decouple(data, {noConsumer: true, map: opts.map}); | ||
next(decoupled.code, decoupled.map); | ||
})); | ||
} | ||
// Callback | ||
else { | ||
bundle | ||
.pipe(minifyify(opts, function (err, src, map) { | ||
assert.ifError(err); | ||
next(src, map); | ||
})); | ||
} | ||
}; | ||
@@ -55,3 +67,3 @@ | ||
*/ | ||
testApp = function(appname, cb) { | ||
testApp = function(appname, method, cb) { | ||
var encAppname = encodeURIComponent(appname) | ||
@@ -65,3 +77,3 @@ , appDir = path.join(fixtures.buildDir, appname) | ||
// Compile lib | ||
compileApp(appname, function (min, map) { | ||
compileApp(appname, method, function (min, map) { | ||
// Write to the build dir | ||
@@ -88,21 +100,21 @@ var appdir = path.join(fixtures.buildDir, 'apps', appname); | ||
tests['simple file'] = function (next) { | ||
testApp('simple file', next); | ||
testApp('simple file', 'stream', next); | ||
}; | ||
tests['complex file'] = function (next) { | ||
testApp('complex file', next); | ||
testApp('complex file', 'cb', next); | ||
}; | ||
tests['native libs'] = function (next) { | ||
testApp('native libs', next); | ||
testApp('native libs', 'stream', next); | ||
}; | ||
tests['backbone app'] = function (next) { | ||
testApp('backbone app', next); | ||
testApp('backbone app', 'cb', next); | ||
}; | ||
tests['transformed app'] = function (next) { | ||
testApp('transformed app', next); | ||
testApp('transformed app', 'stream', next); | ||
}; | ||
module.exports = tests; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
631393
15842
114