2012-10-23 85 views
0

我有兩個js文件編譯2個js文件使用封閉編譯

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

注意,2.js將訪問方法( setLength)定義於1.js

所以我想編譯器使用相同的替換編譯這兩個文件。

我想這種結果:

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

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

這可能嗎?

順便說一句,我用的是compiler.js編譯文件:

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

這是單個文件,我想編譯多個文件,並各有其自己的輸出文件,是這樣的:

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

回答

2

要使用相同的替換編譯這兩個文件,關閉編譯器的兩個選項可以幫助

 
--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 

所以你可以

  • 首先編譯1.js並生成variable_map。

     
    java -jar compiler.jar --js 1.js --js_output_file 1.min.js -variable_map_output_file variable_map.txt 
    
  • 第二編譯2.js與生成的variable_map。

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

如果2.js將引用在1.js定義的函數,則編譯器將需要一個extern.js爲了編譯2.js

並與輸出包裝器(function(){%s})(),所有功能在1.js中定義的不能從2.js中訪問。您可能需要刪除包裝,或使用export

+0

看來,使用'模塊'可以滿足我的要求。 – hguser