javascript - 使用闭包编译器编译两个js文件

标签 javascript google-closure-compiler

我有两个js文件:

1.js

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

2.js:

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

注意,2.js将访问1.js中定义的方法(setLength)。

所以我希望编译器使用相同的替换来编译这两个文件。

我想要这样的结果:

(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

最佳答案

要使用相同的替换来编译这两个文件,闭包编译器的两个选项可以提供帮助

 
 --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
    
  • 使用生成的variable_map第二次编译2.js。

      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

关于javascript - 使用闭包编译器编译两个js文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13025128/

相关文章:

javascript - 旋转盖流效果

javascript - 如何为此类定义 Google Closure Compiler externs

javascript - 闭包编译器忽略对象突变

google-closure-compiler - 如何包含谷歌关闭的依赖项

javascript - 使用 Closure Library 构建库

javascript - 我如何理解 Backbone 语法

javascript - 日期格式验证出现错误

javascript - 如何使复选框单独与输入字段一起使用

javascript - 谷歌图表巨大的组织图离开屏幕