What is a JavaScript pre-compiled library?
Asked Answered
W

2

7

I'm learning how to use Magento e-commerce system, and I'm reading its documentation, but I don't understand some term, a "pre-compiled JavaScript library". What do they mean by that? How can JavaScript code be compiled?

The web downloader for upgrading and installing Magento without the use of SSH (covered in Chapter 2). • js—The core folder where all JavaScript code included with the installation of Magento is kept. We will find all pre-compiled libraries of JavaScript here.

Source: http://www.magentocommerce.com/wiki/2_-_magento_concepts_and_architecture/magento_s_base_structure

Watercraft answered 4/8, 2011 at 18:46 Comment(2)
Compiled in this case might simply mean - put together before hand.Snip
The link is (effectively) broken (it redirects to a generic top-level documentation page).Zenaidazenana
Z
12

I do not know what Magento e-commerce uses, but I know what JavaScript Compilers are.

Pre-Compiled JavaScript is not practical as the various JavaScript interpreters will have their own method of compiling. Therefore, when most people talk about Compiling JavaScript, they are usually referring Minified JavaScript.

However, the latest minifiers go way beyond. A good example is Google Closure Compiler Advanced Mode. It is related to Google Closure Library and Tools, but is well designed even when used by itself.

There is an Online Demo of Closure Compiler.

It is called a Compiler because it is more than a minifier and the name JavaScript Compiler is not used for anything else. For example: This code

function hello(name) {
  alert('Hello, ' + name);
}
hello('New user');

compiles to alert("Hello, New user"); in advanced mode. Single-use functions are removed, variable names are shortened, and even re-used.

It is very thorough. Simple mode assumes that there might be other JavaScript involved. in which case it would preserve the hello function. Advanced mode assumes that there is only a single JavaScript file, or that it is properly exported.

The one thing that keeps this from really being compiled is that it is not bytecode like compiled C or Java would be. It still has to be compiled at run-time like Perl.

Zosima answered 4/8, 2011 at 18:48 Comment(3)
Agree, most probably "pre-compiled" actually means minimized or obfuscated. Besides Google Closure Compiler, there are dozen of other tools like: uglifyJs, Yui Compressor, Packer & etcAleron
Correct, although the formal definition of compilation (transforming source code from one form into another form) does apply.Portwin
Compiled JavaScript does not exist. This statement is incorrect as we see that V8 actually compiles javascript down to machine code. en.wikipedia.org/wiki/V8_(JavaScript_engine)Pinhole
P
7

Magento has an configuration option in the admin at

System -> Configuration -> Developer -> JavaScript Settings

named Merged Javascript Files.

When this setting is on, Magento will take all the javascript files it knows about, "compile" them to a minified version to product smaller files sizes, and then combine all the javascript files into a single javascript file. This way, instead of opening multiple network connections to download multiple files, Magento will open a single network connection to open one file.

So, when the documentation says that folder contains the pre-compiled versions, it means that's where the individual javascript files are stored, and where the files are loaded from when Merged Javascript Files

The term compilation comes from Computer Science. A compiler takes source code, and transforms it from one language into another language.

Traditionally, it's meant taking code from a higher level language (C, .NET, Java, etc.), and transforming it into the machine code (assembly code) that's understood by the computer chip. However, the term is generic and more modern usage includes taking the source code written in one language (Javascript) and transforms it into a different form (minified javascript).

Portwin answered 4/8, 2011 at 19:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.