Uglify global variable

I have an application in nodejs. In it, I define some global variables that are shared across multiple files. For example:

//common.js
async = requires("async");
isAuthenticated = function() {
  //...
  return false;
};

//run.js
require("common.js");
async.series([function () {
  isAuthenicated();
}], function () {
  console.log("done");
});

      

I want the variables to be async

and isAuthenticated

minified, but minified for all files. It will look like this:

//common.min.js
a = requires("async");
b = function() {
  //...
  return false;
};

//run.min.js
require("common.js");
a.series([function () {
  b();
}], function () {
  console.log("done");
});

      

How do I do this in uglifyjs?

I'm going through the files right now and using the command uglifyjs $file -m "sort,toplevel" -c > $file.min

for each.

+3


source to share


4 answers


  • Don't use global variables.
  • Use if necessary var async = reuqire('async')

    .
  • Use module.exports

    in your specific modules that you need.
  • Use something like browserify to create one js.
  • Uglify (or use a browser transform named uglifyify )

For example, the simplest form (without using uglifyify)

$ browserify run.js | uglifyjs -c > run.min.js

      

Please note that if you are using your own code, for example common.js

, you have to request it by using a relative path var common = require("./common")

.

I suggest you use the export syntax:

// common.js code

exports.isAuthenticated = function() {
  //...
  return false;
};

      

And of course, use it in the same way as with async.js:

//run.js
var common = require("./common");
var async = require("async")
async.series([function () {
  common.isAuthenicated();
}], function () {
  console.log("done");
});

      

assuming both common.js

and run.js

are in the same directory.

related question: How do I get the abbreviated output using the browser?

Side note



The way you used async.series

in your question has no real advantage. You could just:

//run.js
var common = require("./common");

common.isAuthenicated();
console.log("done");

      

in Async series, you usually call async functions:

async.series([
    function(callback){
        // do some stuff ...
        callback(null, 'one');
    },
    function(callback){
        // do some more stuff ...
        callback(null, 'two');
    }
],
// optional callback
function(err, results){
    // results is now equal to ['one', 'two']
}); 

      

So, I would expect to see something like:

// common.js code

exports.isAuthenticated = function(callback) {
  //...
  callback(null, false);
};

      

and then

//run.js
var common = require("./common");
var async = require("async")
async.series([common.isAuthenicated], function (err, results) {
  console.log("done with", results[0]);
});

      

I usually prefer a different "syntax"

// an example using an object instead of an array
async.series({
    one: function(callback){
        setTimeout(function(){
            callback(null, 1);
        }, 200);
    },
    two: function(callback){
        setTimeout(function(){
            callback(null, 2);
        }, 100);
    }
},
function(err, results) {
    // results is now equal to: {one: 1, two: 2}
});

      

But this is your call. Asynchronous examples were taken from https://github.com/caolan/async#seriestasks-callback

+6


source


You would like to link the files before proceeding and guessing them. Concatenation is the process of combining multiple code files into one monolithic being that knows everything about all parts of your code. This is often done in combination with ugliness for several reasons, mostly to improve performance (your application is much faster if you only send one file to the client)

That being said, this is usually a practice that is done when your code is executing for the client, not necessarily for the server / server side logic. Ideally, no one other than you or people with access to whatever service you use to deploy said server code should see this side of your code. If your main concern is to prevent reverse engineering or make your code unreadable, then I suggest obfuscating your code.



"This is an omega site. The best encrypted level it has. It looks like a convoluted code to hide its true purpose. Security through obscurity." - Q Skyfall 2012

+1


source


If your globals are limited to common.js you can try

uglifyjs --define-from-module common.js $file...

      

and delete require()s

.

0


source


In NodeJs there is a concept of defining global variables, for example, posted in this thread:

global.myGlobalVar = "something visible to all modules";

      

I also use uglify in my node apps, and it turns out that when using it global.xyz

, I xyz

can't guess.

disclaimer . I fully understand that exposing global information is an anti-pattern. But sometimes there is a good reason for this.

Hope it helps!

0


source







All Articles