gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 19:16:52 26 4
gpt4 key购买 nike

我有两个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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com