Can you minify multiple files into one?
Asked Answered
C

2

8

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 || {});
Caulis answered 21/2, 2013 at 8:29 Comment(4)
A quick sh: cat one.js two.js three.js | uglifyjs -m -c -o out.min.jsPhotosynthesis
"Will it be possible to minify the files into one and not spoiling the scopes" well , you wrote the code , minification doesnt spoil the scopes, your code does ...Enchanter
@Photosynthesis i dont have this uglifyjs application, could you post an answer showing what's the resulting code? Perhaps it's the same as Google Closure's?Caulis
@JoshuaMN: All you need is NodeJS and you can install UglifyJS with npm install -g uglifyjs, then it should be available system-wide. If you're on Windows install a better console, or just install git.Photosynthesis
M
1

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)

Metrify answered 27/2, 2013 at 20:31 Comment(0)
B
2

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

Biller answered 21/2, 2013 at 8:42 Comment(0)
M
1

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)

Metrify answered 27/2, 2013 at 20:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.