Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gulp 3 - Makes js files 3 times bigger then normal files

Having a problem with a gulp and js file minifications, gulp makes 3 times bigger files.

For example lightgallery.min.js - 49kb (downloaded from GitHub) then I download the same file via npm and required in js file (same if I insert downloaded file content from github)

global.lightgallery = require('lightgallery');

and run gulp it makes file 133kb

GULP TASK

gulp.task('scripts', function() {
    gulp.src( SOURCEPATHS.jsSource )
        .pipe( browserify() )
        .pipe( uglify() )
        .pipe( rename({ extname: '.min.js' }) )
        .pipe( gulp.dest(APPPATH.js) );
});

Not using any sourcemaps.

Maybe someone was having the same problem?

like image 691
Kintamasis Avatar asked Mar 23 '19 19:03

Kintamasis


People also ask

What is gulp js file?

A gulpfile is a file in your project directory titled gulpfile. js (or capitalized as Gulpfile. js , like Makefile), that automatically loads when you run the gulp command.

What was used by the gulp file js or JSON?

Gulp used JavaScript configuration code which was less verbose, easier to read, simpler to modify, and provided better flexibility. Gulp was faster because it used Node. js streams to pass data through a series of piped plugins. Files were only written at the end of each task.

What is Gulp clean?

Declare Dependencies and Create Tasks gulp. task('clean:build', function() { return del. sync('build'); }); The above task will clean entire build. The clean task clears any image catches and removes any old files present in build.


1 Answers

The package lightgallery has a dependency of jQuery as specified on the npm description and in its package.json. When Browserify does its thing, it checks the dependency graph of the package and pulls everything in. jQuery v3.3.1 minified is around ~85kb which should account for the discrepancy.

In case you already have jQuery somewhere else, you can usually get Browserify to ignore that particular package. For example, using the gulp-browserify package:

.pipe(browserify({ ignore: 'jquery' }))

Update

You can selectively apply this to files using the gulp-if plugin:

.pipe(
    gulpif(
        'jquery.js',
        browserify(),
        browserify({ ignore: ['jquery'] })
    )
)
like image 121
Zwei Avatar answered Oct 06 '22 00:10

Zwei