You can leverage gulp-babel as such...

const gulp = require('gulp');
const babel = require('gulp-babel');
const uglify = require('gulp-uglify');

gulp.task('minify', () => {
  return gulp.src('src/**/*.js')
    .pipe(babel({
      presets: ['es2015']
    }))
    .pipe(uglify())
    // [...]
});

This will transpile your es6 early in the pipeline and churn out as widely supported "plain" javascript by the time you minify.


May be important to note - as pointed out in comments - the core babel compiler ships as a peer dependency in this plugin. In case the core lib is not being pulled down via another dep in your repo, ensure this is installed on your end.

Looking at the peer dependency in gulp-babel the author is specifying @babel/core (7.x). Though, the slightly older babel-core (6.x) will work as well. My guess is the author (who is the same for both projects) is in the midsts of reorganizing their module naming. Either way, both npm installation endpoints point to https://github.com/babel/babel/tree/master/packages/babel-core, so you'll be fine with either of the following...

npm install babel-core --save-dev

or

npm install @babel/core --save-dev
Answer from scniro on Stack Overflow
🌐
npm
npmjs.com › package › gulp-uglify
gulp-uglify - npm
March 1, 2019 - Minify files with UglifyJS.. Latest version: 3.0.2, last published: 7 years ago. Start using gulp-uglify in your project by running `npm i gulp-uglify`. There are 2680 other projects in the npm registry using gulp-uglify.
      » npm install gulp-uglify
    
Published   Mar 01, 2019
Version   3.0.2
Author   Terin Stock
Top answer
1 of 9
59

You can leverage gulp-babel as such...

const gulp = require('gulp');
const babel = require('gulp-babel');
const uglify = require('gulp-uglify');

gulp.task('minify', () => {
  return gulp.src('src/**/*.js')
    .pipe(babel({
      presets: ['es2015']
    }))
    .pipe(uglify())
    // [...]
});

This will transpile your es6 early in the pipeline and churn out as widely supported "plain" javascript by the time you minify.


May be important to note - as pointed out in comments - the core babel compiler ships as a peer dependency in this plugin. In case the core lib is not being pulled down via another dep in your repo, ensure this is installed on your end.

Looking at the peer dependency in gulp-babel the author is specifying @babel/core (7.x). Though, the slightly older babel-core (6.x) will work as well. My guess is the author (who is the same for both projects) is in the midsts of reorganizing their module naming. Either way, both npm installation endpoints point to https://github.com/babel/babel/tree/master/packages/babel-core, so you'll be fine with either of the following...

npm install babel-core --save-dev

or

npm install @babel/core --save-dev
2 of 9
46

The accepted answer doesn't really answer how to minify straight es6. If you want to minify es6 without transpiling, gulp-uglify v3.0.0 makes that possible:

Update March 2019

Using my original answer, you definitely want to replace the uglify-es package with terser, as it seems uglify-es is no longer being maintained.

Original answer, still works:

1.) First, upgrade your gulp-uglify package to > 3.0.0 If you're using yarn and want to update to the latest version:

yarn upgrade gulp-uglify --latest

2.) Now you can use uglify-es, the "es6 version" of uglify, as so:

const uglifyes = require('uglify-es');
const composer = require('gulp-uglify/composer');
const uglify = composer(uglifyes, console);

gulp.task('compress', function () {
    return gulp.src('src/*.js')
    .pipe(uglify())
    .pipe(gulp.dest('dist'))
});

For more info: https://www.npmjs.com/package/gulp-uglify

Discussions

Code breaks after using gulp-uglify
Hi guys, I’m just new to Ionicframework and new to using gulp. I am currently setting up my workflow and I use gulp-uglify to minify my app files. After using it the code breaks so I checked on the console and got this error. Uncaught Error: [$injector:modulerr] Failed to instantiate module ... More on forum.ionicframework.com
🌐 forum.ionicframework.com
0
0
June 19, 2015
Newest 'gulp-uglify' Questions - Stack Overflow
Stack Overflow | The World’s Largest Online Community for Developers More on stackoverflow.com
🌐 stackoverflow.com
Gulp and uglify - Stack Overflow
Most of the questions and answers on this topic are two or more years old. Are developers minifying js another way now? I keep getting errors when running my gulp script (below) - I did install each More on stackoverflow.com
🌐 stackoverflow.com
Error with gulp-uglify
Hello campers, I need help with my gulp config. I use gulp-uglify for minify my JS files (see whole gulpfile.js file below), but now it's throwing error - it is my fault or gulp's one? Thanks for any advice and help :s… More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
0
March 22, 2017
🌐
npm
npmjs.com › package › gulp-uglify-es
gulp-uglify-es - npm
July 26, 2021 - let gulp = require("gulp"); let rename = require("gulp-rename"); var sourcemaps = require('gulp-sourcemaps'); let uglify = require('gulp-uglify-es').default; gulp.task("uglify", function () { return gulp.src("lib/bundle.js") .pipe(rename("bundle.min.js")) .pipe(sourcemaps.init()) .pipe(uglify()) .pipe(sourcemaps.write()) // Inline source maps.
      » npm install gulp-uglify-es
    
Published   Jul 26, 2021
Version   3.0.0
Author   Itay Ronen
🌐
EDUCBA
educba.com › home › software development › software development tutorials › web development tutorial › gulp uglify
Gulp Uglify | What is Gulp Uglify? | Installation Method
April 3, 2023 - Basically, gulp is used to automate ... is one of the features provided by the gulp. Basically uglify is used to avoid the error event if it is not able to minify our file....
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Gulpjs
gulpjs.com › plugins
Plugins | gulp.js
Skip to main content · Getting Started · Community · GitHub · Stack Overflow · Twitter
Find elsewhere
🌐
GitHub
github.com › sapics › gulp-uglify-cli
GitHub - sapics/gulp-uglify-cli: Minify javascript by UglifyJs2 with command line options · GitHub
uglify({command: '-c', uglifyjs: '/home/node_modules/.bin/uglifyjs'}) If you have an error 'Permission Denied' in 'gulp-uglify-cli', please try to use tmp option which is accessible file path.
Author   sapics
🌐
GitHub
github.com › kaseya › gulp-uglify-harmony
GitHub - kaseya/gulp-uglify-harmony: Minify files with UglifyJS · GitHub
var uglify = require('gulp-uglify'); gulp.task('compress', function() { return gulp.src('lib/*.js') .pipe(uglify()) .pipe(gulp.dest('dist')); });
Author   kaseya
🌐
Ionic Framework
forum.ionicframework.com › t › code-breaks-after-using-gulp-uglify › 26751
Code breaks after using gulp-uglify - Ionic Forum
June 19, 2015 - Hi guys, I’m just new to Ionicframework and new to using gulp. I am currently setting up my workflow and I use gulp-uglify to minify my app files. After using it the code breaks so I checked on the console and got this error. Uncaught Error: [$injector:modulerr] Failed to instantiate module ebhealth due to: Error: [$injector:unpr] Unknown provider: t I am using this gulp function to basically gather all js file from a specified location, concatenate them to all.js then uglify/minify. gulp.tas...
🌐
GitHub
github.com › terinjokes › gulp-uglify
GitHub - terinjokes/gulp-uglify: Minify files with UglifyJS
Minify JavaScript with UglifyJS3. Install package with NPM and add it to your development dependencies: ... var gulp = require('gulp'); var uglify = require('gulp-uglify'); var pipeline = require('readable-stream').pipeline; gulp.task('compress', function () { return pipeline( gulp.src('lib/*.js'), uglify(), gulp.dest('dist') ); });
Starred by 1.2K users
Forked by 153 users
Languages   JavaScript 100.0% | JavaScript 100.0%
🌐
Stack Overflow
stackoverflow.com › questions › tagged › gulp-uglify
Newest 'gulp-uglify' Questions - Stack Overflow
In my gulpfile.js if I comment this line .pipe(uglify({ preserveComments: 'some'})) then my code is not looking ugly and for following line of code everything is working fine.
🌐
jsDelivr
jsdelivr.com › package › npm › gulp-uglify-es
gulp-uglify-es CDN by jsDelivr - A CDN for npm and GitHub
July 26, 2021 - A free, fast, and reliable CDN for gulp-uglify-es. gulp stream to uglify with 'terser' (es6 supported).
Published   Jul 25, 2017
🌐
UNPKG
app.unpkg.com › gulp-uglify-es@0.1.5 › files › README.md
gulp-uglify-es
## Install npm install --save-dev gulp-uglify-es ## Usage gulpfile.js ```js let gulp = require("gulp"); let rename = require("gulp-rename"); let uglify = require('gulp-uglify-es').default; gulp.task("uglify", function () { return gulp.src("lib/bundle.js") .pipe(uglify(/* options */)) .pipe(rename("bundle.min.js")) .pipe(gulp.dest("lib/")); }); ``` For documentation about the options-object, See [Uglify API Reference](https://www.npmjs.com/package/uglify-es#API_Reference).
🌐
TypeScript
typescriptlang.org › docs › handbook › gulp.html
TypeScript: Documentation - Gulp
This quick start guide will teach you how to build TypeScript with gulp and then add Browserify, terser, or Watchify to the gulp pipeline.
🌐
Read the Docs
jakeydocs.readthedocs.io › en › latest › client-side › using-gulp.html
Using Gulp — ASP.NET documentation
gulp.task("clean:js", function (cb) { rimraf(paths.concatJsDest, cb); }); gulp.task("clean:css", function (cb) { rimraf(paths.concatCssDest, cb); }); gulp.task("clean", ["clean:js", "clean:css"]); gulp.task("min:js", function () { return gulp.src([paths.js, "!" + paths.minJs], { base: "." }) .pipe(concat(paths.concatJsDest)) .pipe(uglify()) .pipe(gulp.dest(".")); }); gulp.task("min:css", function () { return gulp.src([paths.css, "!" + paths.minCss]) .pipe(concat(paths.concatCssDest)) .pipe(cssmin()) .pipe(gulp.dest(".")); }); gulp.task("min", ["min:js", "min:css"]);
🌐
freeCodeCamp
forum.freecodecamp.org › t › error-with-gulp-uglify › 118226
Error with gulp-uglify - The freeCodeCamp Forum
March 22, 2017 - Hello campers, I need help with my gulp config. I use gulp-uglify for minify my JS files (see whole gulpfile.js file below), but now it's throwing error - it is my fault or gulp's one? Thanks for any advice and help :s…
🌐
Codementor
codementor.io › community › troubleshooting es6 minification in gulp: upgrading to gulp-terser
Troubleshooting ES6 Minification in Gulp: Upgrading to gulp-terser | Codementor
March 1, 2024 - To address the issue of ES6 syntax not being recognized during the minification process in Gulp, it's crucial to understand the root cause and implement a targeted solution. The error message indicates that the current minification plugin, gulp-uglify, is encountering difficulties interpreting ES6 syntax, particularly the const keyword.
🌐
Reddit
reddit.com › r/webdev › [gulp] uglify and minify-css don't seem to be working - probably something very simple...
r/webdev on Reddit: [Gulp] uglify and minify-css don't seem to be working - probably something very simple...
May 18, 2015 -

Please see the edit at the bottom of the question

Here's my entire gulpfile.js.

I was under the impression that the following should work: .pipe(minifyCss()) and, .pipe(uglify())

Assuming that they were appearing after the gulp.src(...) selection of files to minify. Any advice is appreciated. The output of my .css and .js files are not minified, and look exactly the same as the source files. At the very least, I can somewhat see how my css minification wouldn't work, since it may not be correctly syntaxed for scss minification, although the javascript minification looks like it should be working... however, as mentioned, neither are.

var gulp = require('gulp');
var serve = require('gulp-serve');
var sass = require('gulp-sass');
var useref = require('gulp-useref');
var uglify = require('gulp-uglify');
var minifyCss = require('gulp-minify-css');

gulp.task('serve', serve('dist'));

gulp.task('styles', function() {
	return gulp.src('app/styles/**/*.scss')
		.pipe(sass({
			style: "compressed"
		}).on('error', sass.logError))
		.pipe(minifyCss())
		.pipe(gulp.dest('dist/css'));
});

gulp.task('scripts', function() {
	return gulp.src('app/scripts/**/*.js')
		.pipe(uglify())
		.pipe(gulp.dest('dist/js'));
});

gulp.task('html', function() {
	 var assets = useref.assets();

  return gulp.src('app/*.html')
    .pipe(assets)
    .pipe(assets.restore())
    .pipe(useref())
    .pipe(gulp.dest('dist'));
});

gulp.task('default', ['styles', 'scripts', 'html'], function() { 
	// Do nothing/
});

EDIT

If I run the scripts task by itself gulp scripts then it minifies fine - only when I run the default task does the minification actually fail. Perhaps there is an asynchronous issue going on?

EDIT 2

It looks to be these two lines that are causing the problem:

.pipe(assets)
.pipe(assets.restore())

Which are part of the html gulp task. Need to dig further into why caling useref's asset's restore() erases all minification.

According to the gulp-useref documentation:

It can handle file concatenation but not minification. Files are then passed down the stream. For minification of assets or other modifications, use gulp-if to conditionally handle specific types of assets.

This is kinda balls, imo. I'd like to keep minification out of my 'html' gulp task, but according to this in order to use useref then I'll need to keep the minification in in my 'html' task?

🌐
Npm
npm.io › package › gulp-uglify-es
Gulp-uglify-es NPM | npm.io
let gulp = require("gulp"); let rename = require("gulp-rename"); var sourcemaps = require('gulp-sourcemaps'); let uglify = require('gulp-uglify-es').default; gulp.task("uglify", function () { return gulp.src("lib/bundle.js") .pipe(rename("bundle.min.js")) .pipe(sourcemaps.init()) .pipe(uglify()) .pipe(sourcemaps.write()) // Inline source maps.