0

Gulp noob here and I'm seeing odd behaviour with gulp-uglify.

Here's my task:

var gulp = require ("gulp");
var util = require ("gulp-util");
var clean = require ("gulp-clean");
var cleanCSS = require ("gulp-clean-css");
var debug = require ("gulp-debug");
var filesize = require ("gulp-filesize");
var rename = require ("gulp-rename");
var sass = require ("gulp-ruby-sass");
var browserify = require (BROWSERIFY);
var source = require ("vinyl-source-stream");
var buffer = require ("vinyl-buffer");
var sourceMaps = require("gulp-sourcemaps");
var babelify = require ("babelify");
var uglify = require ("gulp-uglify");
var concatFileNames = require ("gulp-concat-filenames");
var header = require ("gulp-header");

function browserifyTask () {

    return browserify ("./app/main.js")
        .transform(babelify)
        .bundle()
        .pipe (source("main.js"))
        .pipe (buffer())
        .pipe (sourceMaps.init({loadMaps: true}))

        .pipe (sourceMaps.write("./")) // Ensure the source map gets written
        .pipe (gulp.dest("./public/js"))

        // Now do production build stuff

        .pipe (rename("main.min.js"))
        .pipe (sourceMaps.init({loadMaps: true}))
        .pipe (uglify().on ("error", util.log))
        .pipe (sourceMaps.write("./")) // Ensure the source map gets written
        .pipe (gulp.dest("./public/js"))
}

The idea is to produce normal (non-minified) JS and a minified version.

However, Uglify throws an error:

[16:20:26] Using gulpfile C:\play\untitled\gulpfile.js
[16:20:26] Starting 'browserify'...
[16:20:28] { [Error: C:\play\untitled\public\js\main.min.js: Unexpected      token: punc (:)]
  message: 'C:\\play\\untitled\\public\\js\\main.min.js: Unexpected token: punc (:)',
  fileName: 'C:\\play\\untitled\\public\\js\\main.min.js',
  lineNumber: 1,
  stack: 'Error\n    at new JS_Parse_Error (eval at <anonymous>  (C:\\play\\untitled\\node_modules\\uglify-js\\tools\\node.js:22:1),  <anonymous>:1526:18)\n    at js_error (eval at <anonymous>  (C:\\play\\untitled\\node_modules\\uglify-js\\tools\\node.js:22:1),  <anonymous>:1534:11)\n    at croak (eval at <anonymo
us> (C:\\play\\untitled\\node_modules\\uglify-js\\tools\\node.js:22:1),  <anonymous>:2026:9)\n    at token_error (eval at <anonymous>  (C:\\play\\untitled\\node_modules\\uglify-js\\tools\\node.js:22:1),  <anonymous>:2034:9)\n    at unexpected (eval at <anonymous>  (C:\\play\\untitled\\node_modules\\uglify-js\\tool
s\\node.js:22:1), <anonymous>:2040:9)\n    at semicolon (eval at <anonymous> (C:\\play\\untitled\\node_modules\\uglify-js\\tools\\node.js:22:1),     <anonymous>:2060:56)\n    at simple_statement (eval at <anonymous>  (C:\\play\\untitled\\node_modules\\uglify-js\\tools\\node.js:22:1),  <anonymous>:2240:73)\n    at ev
al (eval at <anonymous> (C:\\play\\untitled\\node_modules\\uglify- js\\tools\\node.js:22:1), <anonymous>:2093:47)\n    at eval (eval at <anonymous>  (C:\\play\\untitled\\node_modules\\uglify-js\\tools\\node.js:22:1),  <anonymous>:2073:24)\n    at block_ (eval at <anonymous>  (C:\\play\\untitled\\node_modules\\ugli
fy-js\\tools\\node.js:22:1), <anonymous>:2353:20)',
  showStack: false,
  showProperties: true,
  plugin: 'gulp-uglify' }

It would seem it doesn't like something about the JS it's being piped but Uglify does not throw the error if it's called before the first gulp.dest() call (and I do get minified code).

This post indicated that source maps might be an issue and uses gulp-ignore to exclude them. Tried it and it didn't work; same error.

Have I missed something obvious here?

Thanks, Jeff

1 Answer 1

0

So it was the source maps causing Uglify to balk. There seems to be a more general issue combining Browserify with other plug-ins that is related to Browserify wanting things in buffers and other plug-ins wanting things in streams.

To that end, I refind my task function to this:

function browserifyTask () {

    function doBrowserify (isProduction) {
        browserify ("./app/main.js")
            .transform(babelify)
            .bundle()
            .pipe (source("main.js"))
            .pipe (isProduction ? rename("main.min.js") : util.noop())
            .pipe (buffer())
            .pipe (sourceMaps.init({loadMaps: true}))
            .pipe (isProduction ? uglify().on ("error", util.log) : util.noop())
            .pipe (sourceMaps.write("./")) 
            .pipe (gulp.dest("./public/js"))
    }

    doBrowserify(false);
    doBrowserify(true);
}

Not as elegant as I'd like it, but it gets the job done and I need to crack on with this project (and not get bogged down in the build process).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.