473,778 Members | 1,852 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Program that takes readable JS code and packs it together for downloading?

Hello. I am wondering if there exists a piece of software that takes
multiple .js files that are nicely indented and commented and create
one big tightly packed .js file. I'm hoping the one file would be less
of a burden for the user's browser to download. I guess the final code
output by this hypothetical program could maybe even just be on one
long line unless that would create problems. Maybe there is some kind
of optimal line length for the computer to read. I've seen some sites
with some very unreadable .js files with lines hundreds of characters
long. I figure they must be using some program to make the code that
way before it gets put on the server. Noone would want to work on code
that looked like that. I guess I could easily write a small program to
accomplish this, but I'd rather not reinvent the wheel. Any help with
this would be greatly appreciated.

- Ken

Dec 23 '05 #1
4 2118
Ke********@gmai l.com said the following on 12/22/2005 10:53 PM:
Hello. I am wondering if there exists a piece of software that takes
multiple .js files that are nicely indented and commented and create
one big tightly packed .js file. I'm hoping the one file would be less
of a burden for the user's browser to download. I guess the final code
output by this hypothetical program could maybe even just be on one
long line unless that would create problems. Maybe there is some kind
of optimal line length for the computer to read. I've seen some sites
with some very unreadable .js files with lines hundreds of characters
long. I figure they must be using some program to make the code that
way before it gets put on the server. Noone would want to work on code
that looked like that. I guess I could easily write a small program to
accomplish this, but I'd rather not reinvent the wheel. Any help with
this would be greatly appreciated.


http://www.crockford.com/javascript/jsmin.html

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 23 '05 #2
Ke********@gmai l.com wrote:
Hello. I am wondering if there exists a piece of software that takes
multiple .js files that are nicely indented and commented and create
one big tightly packed .js file.


I have written a PHP function which takes JS code and 'compacts' it
on-the-fly to be downloaded to the browser.
I'm far from a PHP expert, but this works for me. You could change to to
also read in additional files and then compact the whole result.
It's also not bullet-proof, since some coding styles will surely break the
output, but it works for me. I always test against the 'compacted' code
output, so if there are any errors introduced by the compacting I can fix
them in the original source.

function js_compact($cod e) {
// Remove single-line comments
$code = preg_replace('|//.*\n|','',$code );
// Replace tabs with a space
$code = preg_replace('/\t/'," ",$code);
// Remove space at the beginning and end of lines
$code = preg_replace('/\n\s*/',"\n",$code) ;
$code = preg_replace('/\s*$/','',$code);
// Remove empty lines
$code = preg_replace('/^\s*\n/',"",$code);
$code = preg_replace('/\n\s*\n/',"\n",$code) ;
// Replace whitespace before { or } or ( or )
$code = preg_replace('/([\S\{\}\(\)])\s*([\{\}\(\)])/',"$1$2",$code) ;
// Remove /* comments */
$code = preg_replace('| \n*/\*.*\*/\n*|s',"",$code );
// Remove whitepace around { }
$code = preg_replace('| \s*([\{\}])\s*|s',"$1",$c ode);
// Remove whitespace between commands
$code = preg_replace('| ;[\s\n]*|s',";",$code) ;
// Put each function on its own line
$code =
preg_replace('| ([\}\;])(function\s*)( \w+)(\s*)\(|s', "$1\n$2$3$4(",$ code);
// Put each prototype function on its own line
$code =
preg_replace('| ([\}\;])(\w+\.prototyp e\.\S+\s*=\s*fu nction)|s',"$1\ n$2",$code);
// Put each class function on its own line
$code =
preg_replace('| ([\}\;])(\w+\.\S+\s*=\ s*function)|s', "$1\n$2",$code) ;
// Put return this on a new line
$code = preg_replace('| (return\s*this; )|s',"\n$1",$co de);
// Add some nice whitespace at the end
$code = $code . "\n\n";
return $code;
}
--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Dec 23 '05 #3

Ke********@gmai l.com wrote:
Hello. I am wondering if there exists a piece of software that takes
multiple .js files that are nicely indented and commented and create
one big tightly packed .js file.

[snip]

Not sure about multiple files, but this is an interesting one:-

<URL:http://dean.edwards.na me/packer/>

Julian Turner

Dec 23 '05 #4
Hello,
Hello. I am wondering if there exists a piece of software that takes
multiple .js files that are nicely indented and commented and create
one big tightly packed .js file. I'm hoping the one file would be less
of a burden for the user's browser to download. I guess the final code
output by this hypothetical program could maybe even just be on one
long line unless that would create problems. Maybe there is some kind
of optimal line length for the computer to read. I've seen some sites
with some very unreadable .js files with lines hundreds of characters
long. I figure they must be using some program to make the code that
way before it gets put on the server. Noone would want to work on code
that looked like that. I guess I could easily write a small program to
accomplish this, but I'd rather not reinvent the wheel. Any help with
this would be greatly appreciated.


you can check my build tool Milk (Windows only for now)
http://www.burrrn.com/projects/Milk.html

features:
a.. generate release build from source directory
a.. filter for authorized/ignored extensions
a.. use 1 config file
a.. use 1 build file
a.. can use unlimited additional config files
a.. get the SVN repository last revision
a.. get verbose information about files listing, config files etc.
a.. compile all files into a single file
a.. remove comments based on the comment type
a.. remove unecessary spaces, empty lines, etc.
a.. can tag the files with build info, file name, date stamp
a.. can generate the documentation
a.. can zip all files, developper release files, library release file and
documentation files
a.. can normalize special and private comments
a.. options to modify the comments included in zip files
etc..

if people (I mean more than 1) are really interested in more features
as "all on one single line" I could see into it.

btw, the tool is written in JavaScript and configured by JavaScript config
files ;)

HTH
zwetan
ps: here an output log of the tool

_____ .__.__ __
/ \ |__| | | | __
/ \ / \| | | | |/ /
/ Y \ | |_| < black
\____|__ /__|____/__|_ \ Math
----------\/-------------\/-------------
Milk: Make include list kit v1.0.5
Copyright (c) 2003-2005 zwetan Kjukov
----------------------------------------
[Config file: core2.eden,core 2_JS.eden]
----------------------------------------
[Build file: src\build_JS.ed en]
----------------------------------------
[Release name: core2 v1.0.0 JS]
----------------------------------------
[releaseFile: core2_v1.0.0_JS]
[Copy files and directories]
from: src\*.*
to: release\dev\cor e2_v1.0.0_JS\*. *
(ignored extensions: *.txt, *.eden)
----------------------------------------
[Source Entries]
src\build.eden
src\build_AS.ed en
src\build_DOC.e den
src\build_JS.ed en
src\build_JS56. eden
src\build_JSDB. eden
src\buRRRn
src\buRRRn\core 2.as1
src\buRRRn\core 2.js
src\buRRRn\core 2.js56
src\buRRRn\core 2.jsdb
src\buRRRn\core 2.txt
src\buRRRn\core 2
src\buRRRn\core 2\Array.es
src\buRRRn\core 2\Boolean.es
src\buRRRn\core 2\Date.es
src\buRRRn\core 2\Error.es
src\buRRRn\core 2\Function.es
src\buRRRn\core 2\ICloneable.es
src\buRRRn\core 2\IComparable.e s
src\buRRRn\core 2\IConvertible. es
src\buRRRn\core 2\ICopyable.es
src\buRRRn\core 2\IEquality.es
src\buRRRn\core 2\IFormattable. es
src\buRRRn\core 2\ISerializable .es
src\buRRRn\core 2\NullObject.es
src\buRRRn\core 2\Number.es
src\buRRRn\core 2\Object.es
src\buRRRn\core 2\String.es
src\buRRRn\core 2\_global.es
----------------------------------------
[Release Entries]
release\dev\cor e2_v1.0.0_JS\bu RRRn
release\dev\cor e2_v1.0.0_JS\bu RRRn\core2.js
release\dev\cor e2_v1.0.0_JS\bu RRRn\core2
release\dev\cor e2_v1.0.0_JS\bu RRRn\core2\Arra y.js
release\dev\cor e2_v1.0.0_JS\bu RRRn\core2\Bool ean.js
release\dev\cor e2_v1.0.0_JS\bu RRRn\core2\Date .js
release\dev\cor e2_v1.0.0_JS\bu RRRn\core2\Erro r.js
release\dev\cor e2_v1.0.0_JS\bu RRRn\core2\Func tion.js
release\dev\cor e2_v1.0.0_JS\bu RRRn\core2\IClo neable.js
release\dev\cor e2_v1.0.0_JS\bu RRRn\core2\ICom parable.js
release\dev\cor e2_v1.0.0_JS\bu RRRn\core2\ICon vertible.js
release\dev\cor e2_v1.0.0_JS\bu RRRn\core2\ICop yable.js
release\dev\cor e2_v1.0.0_JS\bu RRRn\core2\IEqu ality.js
release\dev\cor e2_v1.0.0_JS\bu RRRn\core2\IFor mattable.js
release\dev\cor e2_v1.0.0_JS\bu RRRn\core2\ISer ializable.js
release\dev\cor e2_v1.0.0_JS\bu RRRn\core2\Null Object.js
release\dev\cor e2_v1.0.0_JS\bu RRRn\core2\Numb er.js
release\dev\cor e2_v1.0.0_JS\bu RRRn\core2\Obje ct.js
release\dev\cor e2_v1.0.0_JS\bu RRRn\core2\Stri ng.js
release\dev\cor e2_v1.0.0_JS\bu RRRn\core2\_glo bal.js
----------------------------------------
[Removing comments]
release\dev\cor e2_v1.0.0_JS\bu RRRn\core2.js
release\dev\cor e2_v1.0.0_JS\bu RRRn\core2\Arra y.js
release\dev\cor e2_v1.0.0_JS\bu RRRn\core2\Bool ean.js
release\dev\cor e2_v1.0.0_JS\bu RRRn\core2\Date .js
release\dev\cor e2_v1.0.0_JS\bu RRRn\core2\Erro r.js
release\dev\cor e2_v1.0.0_JS\bu RRRn\core2\Func tion.js
release\dev\cor e2_v1.0.0_JS\bu RRRn\core2\IClo neable.js
release\dev\cor e2_v1.0.0_JS\bu RRRn\core2\ICom parable.js
release\dev\cor e2_v1.0.0_JS\bu RRRn\core2\ICon vertible.js
release\dev\cor e2_v1.0.0_JS\bu RRRn\core2\ICop yable.js
release\dev\cor e2_v1.0.0_JS\bu RRRn\core2\IEqu ality.js
release\dev\cor e2_v1.0.0_JS\bu RRRn\core2\IFor mattable.js
release\dev\cor e2_v1.0.0_JS\bu RRRn\core2\ISer ializable.js
release\dev\cor e2_v1.0.0_JS\bu RRRn\core2\Null Object.js
release\dev\cor e2_v1.0.0_JS\bu RRRn\core2\Numb er.js
release\dev\cor e2_v1.0.0_JS\bu RRRn\core2\Obje ct.js
release\dev\cor e2_v1.0.0_JS\bu RRRn\core2\Stri ng.js
release\dev\cor e2_v1.0.0_JS\bu RRRn\core2\_glo bal.js
----------------------------------------
[Generate library]
release\lib\cor e2_v1.0.0_JS.js
----------------------------------------
[Zipping releases]
core2_v1.0.0_JS _DEV.zip
core2_v1.0.0_JS _LIB.zip
----------------------------------------
processed in: 0h:0mn:18s:687m s
----------------------------------------
Dec 24 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
3493
by: Nick Evans | last post by:
Hello there, I have been on and off learning to code (with python being the second language I have worked on after a bit of BASIC). What I really want to know is, if you are going to actually write a program or a project of some sort, how do you actually start. Picture this, you know what you want the program to do (its features), you have a possably rough idea of how the program will work. You have opened an empty text file and saved...
22
3608
by: edgrsprj | last post by:
PROPOSED EARTHQUAKE FORECASTING COMPUTER PROGRAM DEVELOPMENT EFFORT Posted July 11, 2005 My main earthquake forecasting Web page is: http://www.freewebz.com/eq-forecasting/Data.html Newsgroup Readers: If you circulate copies of this report to groups of computer programmers at different universities etc. around the world then they might find the subject matter to be interesting.
0
2509
by: metaperl | last post by:
A Comparison of Python Class Objects and Init Files for Program Configuration ============================================================================= Terrence Brannon bauhaus@metaperl.com http://www.livingcosmos.org/Members/sundevil/python/articles/a-comparison-of-python-class-objects-and-init-files-for-program-configuration/view
9
3404
by: gdarian216 | last post by:
I have written a c++ program that takes input from a file and outputs the average. The program uses structs and I need to convert the struct to a class. I just dont know how to get started and if I will have to re-write my whole program. #include <cstdlib> #include <iostream> #include <string> #include <vector> #include <fstream> using namespace std;
2
2201
by: gdarian216 | last post by:
the program reads input from a file and then outputs the averages and grade. for some reason it is reading in the same line twice and it doesn't print out the grade. everything else is correct, if someone can help me thanks this is my code.... #include <cstdlib> #include <iostream> #include <string> #include <vector>
5
1905
by: mohammaditraders | last post by:
Write a program which overloads a binary Minus (-) operator, The program will contain a class Matrix, This class will contain a private data member Array which store int values. The class will further contain a Default constructor, get() function which takes values for array from the user and also contain a Display function witch display the array on the screen, In main function create three objects Mat1, Mat2, Mat3 of this class,...
24
3240
by: Peter Michaux | last post by:
I have a Perl script that I want to run as a set-user-ID program. Many OSes don't allow scripts run as set-user-ID. To make this script portable, it seems I need to write a C wrapper program that calls exec or system to give the Perl script the necessary effective permissions. How can I make the C wrapper program secure? or "more" secure? The Perl script, which is "-rwsr-xr-x root root" will look at the real user id and then check a...
0
9465
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10296
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10068
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9923
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8954
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7474
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5370
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5497
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4031
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.