hughsk / uglifyify

A browserify transform which minifies your code using UglifyJS2
Other
376 stars 62 forks source link

Setting uglifyJS options #26

Open DylanPiercey opened 9 years ago

DylanPiercey commented 9 years ago

How can one send options to uglify js with this transform?

Such as:

{
    mangle: true,
    compress: {
        sequences: true,
        dead_code: true,
        conditionals: true,
        booleans: true,
        unused: true,
        if_return: true,
        join_vars: true,
        drop_console: true
}
hughsk commented 9 years ago

From the CLI, you can use subarg syntax :)

browserify index.js -t [ uglifyify --mangle --compress [ --sequences --dead_code --booleans ] ]

Or if working with browserify as a module:

var browserify = require('browserify')
var bundler = browserify('index.js')

bundler.transform({
  mangle: true,
  compress: {
    sequences: true,
    dead_code: true
    booleans: true
  }
}, 'uglifyify')
AsaAyers commented 9 years ago

Do you know if there's a set of configs that will only trim dead code? I'd like to combine this with envify to remove some requires, but something just doesn't seem right about uglifying the code in the bundle. And I'm already running uglify on the final output.

hughsk commented 9 years ago

@AsaAyers my guess would be:

{
  mangle: false,
  compress: {
    dead_code: true
  }
}

Give that a shot and see if it works :)

AsaAyers commented 9 years ago

It still collapses everything onto one line. Thanks for trying though :)

I tried looking through the list of transforms again and I think unreachable-branch-transform is really what I'd prefer to use.

naorye commented 9 years ago

@hughsk I am executing browserify with the following arguments: bundler.transform(uglifyify, { global: true, mangle: true, compress: { sequences: true, dead_code: true, booleans: true } });

and I get the same code as if I executed: bundler.transform(uglifyify, { global: true });

I think there is a problem.

weilu commented 9 years ago

@naorye a bunch of options are set to true by default, including compress and mangle. Try setting them to false you should see different output.

naorye commented 9 years ago

@weilu You right. The thing is that after using uglifyify, I get several uglyfied files concatenated into one file with a lot of line breaks. So I am using gulp-uglify after browserify done it's work. Is it the right way to do it?

weilu commented 9 years ago

You shouldn't need to run it through gulp-uglify again. You are not the first person to report the line breaks problem. Unfortunately I don't have the same issue with my package. Is it possible for you to note down the exact steps to reproduce the line break issue?

ribeiroct commented 9 years ago

+1 With the line-break problem

yoshuawuyts commented 9 years ago

@ribeiroct +1's are generally not as useful as a step-by-step repro of what causes the issue for you. Would you mind posting one so we can help resolve this issue? Thanks!

ribeiroct commented 9 years ago

I'm using it as follows:

    browserify -d -t babelify -g [ uglifyify --no-sourcemap] files/*.jsx -o bundle.js

Browserify version: 9.0.3 Uglifyify version 3.0.1

Breaklines seem to be introduced after some of the function opening brackets.

I'm using it on a reactjs project.

To solve it i'm doing another pass with uglifyjs in the end. Can babelify be having an effect on the process? I'm not sure.

weilu commented 9 years ago

@ribeiroct @naorye I don't have your source code so I'm not sure how to reproduce this. Can either of you give me the smallest reproducible source before bundle?

naorye commented 9 years ago

This is my bundle creation code:

    var bundleEntries = [ './a/client.js', './b/client.js', './c/client.js' ],
        bundleOutputs = [ './build/a/client.js', './build/b/client.js', './build/c/client.js' ],
        commonOutput = './build/bundle.js',
        b = null,
        // The following runs scriptBuild() over all the returned scripts
        write = filesWriter(scriptBuild, 1 + bundleOutputs.length, done)

        params = { // Preare params
            entries: bundleEntries, // All frames entries
            transform: [ [ envify, { global: true } ] ],
            extensions: [ '.jsx' ] // Add jsx for the module lookup machinery
        };

    if (process.env.DISTRIBUTION) {
        params.transform.push([ 'uglifyify', { global: true, sourcemap: process.env.WATCH } ]);
    }

    if (process.env.WATCH) {
        objectAssign(params, {
            debug: true, // Gives us sourcemaps
            cache: {}, // Requirement of watchify
            packageCache: {}, // Requirement of watchify
            fullPaths: true// Requirement of watchify
        });

        b = browserify(params);
        b = watchify(b);
        b.on('update', bundle);
    } else {
        b = browserify(params);
    }

    bundle();

    function bundle() {
        return b
            .plugin('factor-bundle', { outputs: bundleOutputs.map(write) })
            .on('error', function(err) { console.error(err) })
            .bundle()
            .pipe(write(commonOutput));
    }

    function scriptBuild(stream, filePath, dirname, basename) {
        if (process.env.DISTRIBUTION) {
            stream = stream
                .pipe(uglify())
                .pipe(rev())
                .pipe(gulp.dest(dirname))
                .pipe(rev.manifest());
        }
        return stream.pipe(gulp.dest(dirname));
    }