gruntjs / grunt

Grunt: The JavaScript Task Runner
http://gruntjs.com/
Other
12.26k stars 1.5k forks source link

grunt with obfuscator #1544

Closed achyuth closed 8 years ago

achyuth commented 8 years ago

Hi All, I am trying to obfuscated all js files in js folder by using https://www.npmjs.com/package/grunt-obfuscator I have installed grunt, node and updated npm in my windows machine. below is my sample code. I have run "grunt obfuscator" and its generates "obfuscator" js file that is great, But throws "obfuscated.js:1 Uncaught ReferenceError: require is not defined" browser console.

module.exports = function(grunt){
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        uglify: {
            /*options: {
                mangle: false
            },*/
            my_target: {
                files: {
                    'dest/output.min.js': ['src/js/app.js', 'src/js/site.js']
                }
            }
        },
        cssmin: {
            my_target:{
                files : [{
                    expand: true,
                    cwd: 'css/',
                    src: ['*.css', '.min.css'],
                    //src: '*.css',
                    dest: 'css/',
                    ext: '.min.css'
                }]
            }
        },
        obfuscator:{
            files: [
                'src/js/app.js',
                'src/js/*.js'
            ],
            entry: 'src/js/app.js',
            out: 'obfuscated.js',
            strings: false,
            root: __dirname
        }
    });
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-contrib-cssmin');
    grunt.loadNpmTasks('grunt-obfuscator');
}

app.js code:

var funt = function(){
    var test  = 213123;
    var test_a = "string";
    return test+test_a;
};
document.getElementByID('res').innerHTML = funt();

site.js code:

var text = "text Variable"; 
var test = "text Variable"; 
var string = "text Variable";
var array = [2,3,2432,23,23,2]; 
var boolean_var = false;

Please help me on this.

shama commented 8 years ago

I'm not familiar with grunt-obfuscator but the error Uncaught ReferenceError: require is not defined" browser console. indicates you're trying to run node.js (server side) code in the browser. Try running node obfuscated.js instead of running that file in the browser.