473,385 Members | 1,341 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 2081
Ke********@gmail.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.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 23 '05 #2
Ke********@gmail.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($code) {
// 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",$code);
// 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+\.prototype\.\S+\s*=\s*function)|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",$code);
// 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********@gmail.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.name/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,core2_JS.eden]
----------------------------------------
[Build file: src\build_JS.eden]
----------------------------------------
[Release name: core2 v1.0.0 JS]
----------------------------------------
[releaseFile: core2_v1.0.0_JS]
[Copy files and directories]
from: src\*.*
to: release\dev\core2_v1.0.0_JS\*.*
(ignored extensions: *.txt, *.eden)
----------------------------------------
[Source Entries]
src\build.eden
src\build_AS.eden
src\build_DOC.eden
src\build_JS.eden
src\build_JS56.eden
src\build_JSDB.eden
src\buRRRn
src\buRRRn\core2.as1
src\buRRRn\core2.js
src\buRRRn\core2.js56
src\buRRRn\core2.jsdb
src\buRRRn\core2.txt
src\buRRRn\core2
src\buRRRn\core2\Array.es
src\buRRRn\core2\Boolean.es
src\buRRRn\core2\Date.es
src\buRRRn\core2\Error.es
src\buRRRn\core2\Function.es
src\buRRRn\core2\ICloneable.es
src\buRRRn\core2\IComparable.es
src\buRRRn\core2\IConvertible.es
src\buRRRn\core2\ICopyable.es
src\buRRRn\core2\IEquality.es
src\buRRRn\core2\IFormattable.es
src\buRRRn\core2\ISerializable.es
src\buRRRn\core2\NullObject.es
src\buRRRn\core2\Number.es
src\buRRRn\core2\Object.es
src\buRRRn\core2\String.es
src\buRRRn\core2\_global.es
----------------------------------------
[Release Entries]
release\dev\core2_v1.0.0_JS\buRRRn
release\dev\core2_v1.0.0_JS\buRRRn\core2.js
release\dev\core2_v1.0.0_JS\buRRRn\core2
release\dev\core2_v1.0.0_JS\buRRRn\core2\Array.js
release\dev\core2_v1.0.0_JS\buRRRn\core2\Boolean.j s
release\dev\core2_v1.0.0_JS\buRRRn\core2\Date.js
release\dev\core2_v1.0.0_JS\buRRRn\core2\Error.js
release\dev\core2_v1.0.0_JS\buRRRn\core2\Function. js
release\dev\core2_v1.0.0_JS\buRRRn\core2\ICloneabl e.js
release\dev\core2_v1.0.0_JS\buRRRn\core2\IComparab le.js
release\dev\core2_v1.0.0_JS\buRRRn\core2\IConverti ble.js
release\dev\core2_v1.0.0_JS\buRRRn\core2\ICopyable .js
release\dev\core2_v1.0.0_JS\buRRRn\core2\IEquality .js
release\dev\core2_v1.0.0_JS\buRRRn\core2\IFormatta ble.js
release\dev\core2_v1.0.0_JS\buRRRn\core2\ISerializ able.js
release\dev\core2_v1.0.0_JS\buRRRn\core2\NullObjec t.js
release\dev\core2_v1.0.0_JS\buRRRn\core2\Number.js
release\dev\core2_v1.0.0_JS\buRRRn\core2\Object.js
release\dev\core2_v1.0.0_JS\buRRRn\core2\String.js
release\dev\core2_v1.0.0_JS\buRRRn\core2\_global.j s
----------------------------------------
[Removing comments]
release\dev\core2_v1.0.0_JS\buRRRn\core2.js
release\dev\core2_v1.0.0_JS\buRRRn\core2\Array.js
release\dev\core2_v1.0.0_JS\buRRRn\core2\Boolean.j s
release\dev\core2_v1.0.0_JS\buRRRn\core2\Date.js
release\dev\core2_v1.0.0_JS\buRRRn\core2\Error.js
release\dev\core2_v1.0.0_JS\buRRRn\core2\Function. js
release\dev\core2_v1.0.0_JS\buRRRn\core2\ICloneabl e.js
release\dev\core2_v1.0.0_JS\buRRRn\core2\IComparab le.js
release\dev\core2_v1.0.0_JS\buRRRn\core2\IConverti ble.js
release\dev\core2_v1.0.0_JS\buRRRn\core2\ICopyable .js
release\dev\core2_v1.0.0_JS\buRRRn\core2\IEquality .js
release\dev\core2_v1.0.0_JS\buRRRn\core2\IFormatta ble.js
release\dev\core2_v1.0.0_JS\buRRRn\core2\ISerializ able.js
release\dev\core2_v1.0.0_JS\buRRRn\core2\NullObjec t.js
release\dev\core2_v1.0.0_JS\buRRRn\core2\Number.js
release\dev\core2_v1.0.0_JS\buRRRn\core2\Object.js
release\dev\core2_v1.0.0_JS\buRRRn\core2\String.js
release\dev\core2_v1.0.0_JS\buRRRn\core2\_global.j s
----------------------------------------
[Generate library]
release\lib\core2_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:687ms
----------------------------------------
Dec 24 '05 #5

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

Similar topics

33
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...
22
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 ...
0
by: metaperl | last post by:
A Comparison of Python Class Objects and Init Files for Program Configuration ============================================================================= Terrence Brannon bauhaus@metaperl.com...
9
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...
2
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...
5
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...
24
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.