Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Organize and merge JS files, Google Closure?

I'm trying to merge all my plugins so I can change from this:

<html>
    <head>
    </head>
    <body>    
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script src="js/plugin1.js"></script>
        <script src="js/plugin2.js"></script>
        <script src="js/plugin3.js"></script>
        <script src="js/plugin4.js"></script>
    </body>
</html>

to this:

<html>
    <head>
    </head>
    <body>    
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script src="js/plugins.min.js"></script>
    </body>
</html>

The main idea is to have a tool to "auto import" into my plugins.js file all the plugins I need for my websites and also minify them when compiling. I've been testing with Google Closure (downloadable Java version of the compiler) and I figured how to minify files but I can't import external files. So I'd like to ask you if you could maybe tell me if Google Closure is the tool I'm looking for or if I should use another tool or method.

With "auto import" I mean something like:

PLUGINS/HELLO.JS

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

PLUGINS.JS

@import hello.js
hello('New user');

I hope I made myself clear, I apologize if I messed up with my English.

Thanks!!

like image 826
user2122223 Avatar asked Mar 31 '13 07:03

user2122223


1 Answers

I asked myself same question, and found no acceptable answer. Only way to join many files is putting multiple "--js" params in command line:

java -jar compiler.jar --js 1.js --js 2.js --js 3.js --js_output_file all.js

I tried put lines like this (which used in Closure Compiler service UI):

// ==ClosureCompiler==
// @code_url 1.js
// @code_url 2.js
// @code_url 3.js
// ==/ClosureCompiler==

... in "main.js" and compile it - no effect. :(

like image 181
0gog0 Avatar answered Sep 21 '22 18:09

0gog0