dudewad
dudewad

Reputation: 13923

grunt-contrib-uglify output is empty

I've been looking all day for the answer here and I'm convinced it's just my config is messed up.

Short version: I have grunt configured. grunt-contrib-concat WORKS. grunt-contrib-uglify SAYS IT WORKS but outputs an empty file.

Long version: Project structure is:

/projectRoot
    ->/dev
        ->/js
            /foo.js
            /bar.js
    ->/dist
        ->/js

I want to take foo.js and bar.js and output them to a file in the /dist/js directory called "app.js".

Here is my config:

module.exports = function(grunt) {
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),

        concat: {
            options:{
                separator: ';'
            },
            dist:{
                src:["js/foo.js", "js/barjs"],
                dest:"../prod/js/app.js"
            }
        },

        uglify:{
            dist:{
                files:{
                    "../prod/js/app.min.js":["js/foo.js", "js/bar.js"]
                }
            }
        }
    });



    /**
     * Set up tasks
     * @type {string[]}
     */
    var taskList = [
        "grunt-contrib-concat",
        "grunt-contrib-uglify"
    ];

    for(var task in taskList){
        grunt.loadNpmTasks(taskList[task]);
    }

    grunt.registerTask('default', ['concat']);
    grunt.registerTask('dist', ['concat','uglify:dist']);
};

Yes, Grunt is installed in my DEV directory.

When I run

grunt dist

I get the following console output:

Running "concat:dist" (concat) task
File ../prod/js/app.js created.

Running "uglify:dist" (uglify) task
File js/app.min.js created: 319 B → 0 B

Done, without errors.
Process finished with exit code 0

In the end, it does end up creating the file /projectRoot/prod/js/app.min.js

...But it's empty.

Any ideas on what I'm doing wrong?? This syntax EXACTLY follows what is in the readme and any other docs I can find. The concat task is working just fine.

Any help would be appreciated. Thanks!

--EDIT-- I updated Grunt so that it lives in the projectRoot but there is no difference in output.

==============

UPDATE

I found the reason why it was outputting an empty file. Apparently self-calling anonymous functions are "minified" out, or at least in this case they are. This might be because there is no active code here. I'm going to look into this further. I assume that functions that actually do stuff, are referencing global vars, etc will remain.

==============

FINAL UPDATE

Issue resolved. Even with options to minify all code it will still remove any unreferenced variables, etc. The stuff in my test files were unused. If I add a reference to said functions then they show up.

Hope this helps somebody.

Upvotes: 3

Views: 1772

Answers (1)

dudewad
dudewad

Reputation: 13923

Issue resolved. Even with options to minify all code it will still remove any unreferenced variables, etc. The stuff in my test files were unused. If I add a reference to said functions then they show up. Since none of the functions are referenced, they are removed and I end up with an empty file.

Hope this helps somebody.

Upvotes: 4

Related Questions