Skip to content

Commit f2b779b

Browse files
committed
feat(composer): implement new API for composing deps
BREAKING CHANGE: Replace the old "minifier" entry point with a new "composer" entry point. Fixes #291.
1 parent dbbba30 commit f2b779b

File tree

8 files changed

+103
-90
lines changed

8 files changed

+103
-90
lines changed

LICENSE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
> Copyright (c) 2013-2014 Terin Stock <[email protected]>
1+
> Copyright (c) 2013-2017 Terin Stock <[email protected]>
22
>
33
> Permission is hereby granted, free of charge, to any person obtaining
44
> a copy of this software and associated documentation files (the

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,24 +59,24 @@ To see useful error messages, see [Why Use Pump?](docs/why-use-pump/README.md#wh
5959

6060
## Using a Different UglifyJS
6161

62-
**This API may change before the v3 release. Consider it deprecated.**
63-
6462
By default, `gulp-uglify` uses the version of UglifyJS installed as a dependency.
65-
It's possible to configure the use of a different version using the "minifier" entry point.
63+
It's possible to configure the use of a different version using the "composer" entry point.
6664

6765
```javascript
6866
var uglifyjs = require('uglify-js'); // can be a git checkout
6967
// or another module (such as `uglify-es` for ES6 support)
70-
var minifier = require('gulp-uglify/minifier');
68+
var composer = require('gulp-uglify/composer');
7169
var pump = require('pump');
7270

71+
var minify = composer(uglifyjs, console);
72+
7373
gulp.task('compress', function (cb) {
7474
// the same options as described above
7575
var options = {};
7676

7777
pump([
7878
gulp.src('lib/*.js'),
79-
minifier(options, uglifyjs),
79+
minify(options),
8080
gulp.dest('dist')
8181
],
8282
cb

composer.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
var through = require('through2');
3+
var minify = require('./lib/minify');
4+
5+
module.exports = function(uglify, logger) {
6+
return function(opts) {
7+
return through.obj(minify(uglify, logger)(opts));
8+
};
9+
};

index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
'use strict';
22
var uglify = require('uglify-js');
3-
var minifier = require('./minifier');
3+
var compose = require('./composer');
44
var GulpUglifyError = require('./lib/gulp-uglify-error');
5+
var logger = require('./lib/log');
56

67
module.exports = function(opts) {
7-
return minifier(opts, uglify);
8+
return compose(uglify, logger)(opts);
89
};
910

1011
module.exports.GulpUglifyError = GulpUglifyError;

lib/minify.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
'use strict';
2+
var applySourceMap = require('vinyl-sourcemaps-apply');
3+
var isObject = require('lodash/fp/isObject');
4+
var defaultsDeep = require('lodash/fp/defaultsDeep');
5+
var createError = require('./create-error');
6+
7+
module.exports = function(uglify, log) {
8+
function setup(opts) {
9+
if (opts && !isObject(opts)) {
10+
log.warn('gulp-uglify expects an object, non-object provided');
11+
opts = {};
12+
}
13+
14+
return defaultsDeep(
15+
{
16+
output: {}
17+
},
18+
opts
19+
);
20+
}
21+
22+
return function(opts) {
23+
return function(file, encoding, callback) {
24+
var options = setup(opts || {});
25+
var hasSourceMaps = Boolean(file.sourceMap);
26+
27+
if (file.isNull()) {
28+
return callback(null, file);
29+
}
30+
31+
if (file.isStream()) {
32+
return callback(createError(file, 'Streaming not supported', null));
33+
}
34+
35+
if (hasSourceMaps) {
36+
options.sourceMap = {
37+
filename: file.sourceMap.file,
38+
includeSources: true
39+
};
40+
41+
// UglifyJS generates broken source maps if the input source map
42+
// does not contain mappings.
43+
if (file.sourceMap.mappings) {
44+
options.sourceMap.content = file.sourceMap;
45+
}
46+
}
47+
48+
var fileMap = {};
49+
fileMap[file.relative] = String(file.contents);
50+
51+
var mangled = uglify.minify(fileMap, options);
52+
53+
if (mangled.error) {
54+
return callback(
55+
createError(file, 'unable to minify JavaScript', mangled.error)
56+
);
57+
}
58+
59+
if (mangled.warnings) {
60+
mangled.warnings.forEach(function(warning) {
61+
log.warn('gulp-uglify [%s]: %s', file.relative, warning);
62+
});
63+
}
64+
65+
file.contents = new Buffer(mangled.code);
66+
67+
if (hasSourceMaps) {
68+
var sourceMap = JSON.parse(mangled.map);
69+
applySourceMap(file, sourceMap);
70+
}
71+
72+
callback(null, file);
73+
};
74+
};
75+
};

minifier.js

Lines changed: 0 additions & 77 deletions
This file was deleted.

test/injectable.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var assert = require('power-assert');
44
var Vinyl = require('vinyl');
55
var td = require('testdouble');
66
var mississippi = require('mississippi');
7-
var minifer = require('../minifier');
7+
var composer = require('../composer');
88

99
var pipe = mississippi.pipe;
1010
var from = mississippi.from;
@@ -25,6 +25,8 @@ describe('injecting mocha', function() {
2525
});
2626

2727
var uglifyjs = td.object(['minify']);
28+
var logger = td.object(['warn']);
29+
var minify = composer(uglifyjs, logger);
2830

2931
td
3032
.when(
@@ -45,8 +47,9 @@ describe('injecting mocha', function() {
4547
pipe(
4648
[
4749
from.obj([testFile]),
48-
minifer({injecting: true}, uglifyjs),
50+
minify({injecting: true}),
4951
to.obj(function(newFile, enc, next) {
52+
td.verify(logger.warn(), {times: 0, ignoreExtraArgs: true});
5053
assert.ok(newFile, 'emits a file');
5154
assert.ok(newFile.path, 'file has a path');
5255
assert.ok(newFile.relative, 'file has relative path information');

test/minify.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ var uglifyjs = require('uglify-js');
88
var mississippi = require('mississippi');
99
var version = require('semver')(process.version);
1010

11+
var composer = require('../composer');
12+
1113
var pipe = mississippi.pipe;
1214
var to = mississippi.to;
1315
var from = mississippi.from;
@@ -22,7 +24,7 @@ describe('minify', function() {
2224
var testContentsExpected = uglifyjs.minify(testContentsInput).code;
2325

2426
beforeEach(function() {
25-
this.log = td.replace('../lib/log');
27+
this.log = td.object(['warn']);
2628

2729
this.testFile = new Vinyl({
2830
cwd: '/home/terin/broken-promises/',
@@ -38,7 +40,7 @@ describe('minify', function() {
3840
pipe(
3941
[
4042
from.obj([this.testFile]),
41-
require('../minifier')({}, uglifyjs),
43+
composer(uglifyjs, log)({}),
4244
to.obj(function(newFile, enc, next) {
4345
td.verify(log.warn(), {
4446
times: 0,
@@ -71,7 +73,7 @@ describe('minify', function() {
7173
pipe(
7274
[
7375
from.obj([this.testFile]),
74-
require('../minifier')('build.min.js', uglifyjs),
76+
composer(uglifyjs, log)('build.min.js'),
7577
to.obj(function(newFile, enc, next) {
7678
td.verify(
7779
log.warn('gulp-uglify expects an object, non-object provided')

0 commit comments

Comments
 (0)