Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you minify multiple files into one?

I have my JavaScript code split into few files, all using the module pattern (updating one global variable, say MyApp, with new features and members.

Will it be possible to minify the files into one and not spoiling the scopes

Example I want to minify:

File1.js

var Module = (function(ns) {

 ns.fun1 = function() { alert('fun1'); };
 return ns;

})(Module || {});

File2.js

var Module = (function(ns) {

 ns.fun2 = function() { alert('fun2'); };
 return ns;

})(Module || {});
like image 354
Joshua MN Avatar asked Feb 21 '13 08:02

Joshua MN


People also ask

What is the difference between minify and uglify?

Minification is just removing unnecesary whitespace and redundant / optional tokens like curlys and semicolons, and can be reversed by using a linter. Uglification is the act of transforming the code into an "unreadable" form, that is, renaming variables/functions to hide the original intent...

How do I minify a file?

Go to minifycode.com and click the CSS minifier tab. Then paste the CSS code into the input box and click the Minify CSS button. After the new minified code is generated, copy the code. Then go back to the css file of your website and replace the code with the new minified version.

What is the point of minify?

Minification is the process of minimizing code and markup in your web pages and script files. It's one of the main methods used to reduce load times and bandwidth usage on websites. Minification dramatically improves site speed and accessibility, directly translating into a better user experience.


2 Answers

The global scope is in fact global, in that it doesn't matter if you are changing it from one file or multiple files. However, the order of your files can matter, depending on the module pattern flavour you're using. For a good writeup, covering cross-file private state also, read this: http://www.adequatelygood.com/2010/3/JavaScript-Module-Pattern-In-Depth

like image 130
depoulo Avatar answered Sep 29 '22 18:09

depoulo


Yes you can, and is a good way to work, for everyone, especially if more developers are involved and healthy for the application.

I use the Minify class (http://code.google.com/p/minify/) with a special config file (https://beat.snipt.net/minify-config-file-minify-each-folder-from-project-1-minified-file/) that scans my folders and makes for each subfolder a file (JS or CSS). This way I can make modular JS or CSS files, that can be splitted on classes or regular files.

The key is the ORDER of the files, depending on the read-folder functions and OS type may be different (ex php readdir vs scandir)

like image 43
BG Adrian Avatar answered Sep 29 '22 17:09

BG Adrian