hguser
hguser

Reputation: 36020

compile two js files using closure compiler

I have two js files:

1.js

(function(){
  function setLength(a,len){
  a.length=len;
}
  ...........
})();

2.js:

function View(){
  setLength(this,3);
}

Note,the 2.js will access the method (setLength) defined in 1.js.

So I want the compiler compile these two files using the same replacement.

I want this kind of result:

(function(){
  function x(a,b){
  a.length=b;
}
  ...........
})();

function View(){
  x(this,3);
}

Is this possible?

BTW,I use the compiler.js to compile the files:

java -jar compiler.jar --js file.js --js_output_file file.min.js

This is the single file,I want to compile more than one file and each have its own output file,something like this:

java -jar compiler.jar --js file.js,file2.js --js_output_file file.min.js,file2.min.js

Upvotes: 0

Views: 936

Answers (1)

Tim Green
Tim Green

Reputation: 3649

To compile these two files using the same replacement, two options of closure compiler can help

 
 --variable_map_input_file VAL          : File containing the serialized version
                                           of the variable renaming map produced
                                           by a previous compilation
 --variable_map_output_file VAL         : File where the serialized version of t
                                          he variable renaming map produced shou
                                          ld be saved

So you can

  • First Compile 1.js and generate variable_map.

      java -jar compiler.jar --js 1.js --js_output_file 1.min.js -variable_map_output_file variable_map.txt
    
  • Second Compile 2.js with the generated variable_map.

      java -jar compiler.jar --js 2.js --js_output_file 2.min.js --variable_map_input_file variable_map.txt      
    

If 2.js will reference functions defined in 1.js, then the complier will need an extern.js in order to compile 2.js

And with the output wrapper (function(){%s})(), all functions defined in 1.js can not be accessed from 2.js. You might need drop the wrapper, or use export

Upvotes: 2

Related Questions