Skip to content

Commit

Permalink
Merge pull request #21 from SmaatoUI/BT-300/feature/no-console-logs
Browse files Browse the repository at this point in the history
[BT-300]: MinifyJs removes console.log statements by default
  • Loading branch information
jenslonkowski committed Jan 28, 2016
2 parents 799814f + 2cd08cc commit dadeba1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 21 deletions.
4 changes: 4 additions & 0 deletions src/minifyJs.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const gulpUglify = require('gulp-uglify');
module.exports = customConfig => {
const config = Object.assign({
src: './dist/js',
dropConsole: true,
mangle: true,
}, customConfig);

Expand All @@ -17,6 +18,9 @@ module.exports = customConfig => {
function minifyJs() {
return gulp.src(`${config.src}/dist.js`)
.pipe(gulpUglify({
compress: {
drop_console: config.dropConsole,
},
mangle: config.mangle,
}))
.pipe(gulpRename('dist.min.js'))
Expand Down
53 changes: 32 additions & 21 deletions src/minifyJs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
const gulp = require('gulp');
const runSequence = require('run-sequence');
const rimraf = require('rimraf');
const lstat = require('fs').lstat;
const fs = require('fs');
const lstat = fs.lstat;
const compileJs = require('../index').compileJs;
const minifyJs = require('../index').minifyJs;

Expand All @@ -20,6 +21,7 @@ describe('minifyJs method', () => {
const result = minifyJs();
expect(result.config).toEqual({
src: './dist/js',
dropConsole: true,
mangle: true,
});
});
Expand All @@ -34,33 +36,42 @@ describe('minifyJs method', () => {
});

describe('gulp task', () => {
gulp.task('testMinifyJs:compile', compileJs({
src: './demo/src/index.js',
dst: './demo/dist/js',
}).task);

gulp.task('testMinifyJs', minifyJs({
src: './demo/dist/js',
}).task);

beforeEach(done => {
// rm -rf the dist folder.
rimraf('./demo/dist', done);
rimraf('./demo/dist', () => {
/**
* Because the Gulp task is async, we need to use runSequence to execute
* the task and then call the `done` async callback.
*/
runSequence('testMinifyJs:compile', 'testMinifyJs', done);
});
});

it('minifies a compiled JS file', (done) => {
gulp.task('testMinifyJs:compile', compileJs({
src: './demo/src/index.js',
dst: './demo/dist/js',
}).task);
expect(gulp.tasks.testMinifyJs.done).toBe(true);
lstat('./demo/dist/js/dist.min.js', (err, stats) => {
if (err) throw err;
expect(stats.isFile()).toBe(true);
done();
});
});

gulp.task('testMinifyJs', minifyJs({
src: './demo/dist/js',
}).task);
it('removes console.log statements by default', () => {
const dist = fs.readFileSync('./demo/dist/js/dist.js');
const distMin = fs.readFileSync('./demo/dist/js/dist.min.js');
const consoleLogStatement = 'console.log(\'Hello, world!\'';

/**
* Because the Gulp task is async, we need to use runSequence to execute
* the task and then call the `done` async callback.
*/
runSequence('testMinifyJs:compile', 'testMinifyJs', () => {
expect(gulp.tasks.testMinifyJs.done).toBe(true);
lstat('./demo/dist/js/dist.min.js', (err, stats) => {
if (err) throw err;
expect(stats.isFile()).toBe(true);
done();
});
});
expect(dist).toContain(consoleLogStatement);
expect(distMin).not.toContain(consoleLogStatement);
});
});
});

0 comments on commit dadeba1

Please sign in to comment.