javascript JS - 无法合并 lib 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20307462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JS - Can't combine lib files
提问by dthree
I have multiple lib files in an an index.html file, that are loaded in proper sequence for an app I am running.
我在 index.html 文件中有多个 lib 文件,这些文件按我正在运行的应用程序的正确顺序加载。
<!-- example of some of them... -->
<script src="/./sys/lib/jquery.min.js"></script>
<script src="/./sys/lib/jquery.ui.min.js"></script>
<script src="/./sys/lib/jquery.easing.min.js"></script>
<script src="/./sys/lib/underscore.min.js"></script>
<script src="/./sys/lib/handlebars.min.js"></script>
<script src="/./sys/lib/backbone.min.js"></script>
<script src="/./sys/lib/moment.min.js"></script>
<script src="/./sys/lib/libs.extensions.js"></script>
These run fine, they are already all minified.
这些运行良好,它们已经全部缩小。
Now, I want to combine these all into one file for load speed:
现在,我想将所有这些合并到一个文件中以提高加载速度:
<script src="/./sys/lib/libs.all.js"></script>
So I open up the new libs.all.js
file, and one by one paste the minified .js files into it, with zeromodification, in the exact same sequenceas listed above. This works until I get to moment.js. When I then paste that in and run it, I get a JS error.
因此,我打开新libs.all.js
文件,将缩小的 .js 文件逐个粘贴到其中,修改为零,顺序与上面列出的完全相同。这一直有效,直到我到达 moment.js。当我然后粘贴它并运行它时,我收到一个 JS 错误。
TypeError: (intermediate value)(...) is not a function
I don't get what I am missing - if I paste them in the right sequence as they synch loaded in the HTML file, what is the difference?
我不明白我错过了什么 - 如果我在它们同步加载到 HTML 文件时以正确的顺序粘贴它们,有什么区别?
回答by bagonyi
Most probably one of your js files is missing a ;
at the end. Open up the one you believe is causing the error and add a ;
at the end, or add a ;
to the very first line of the next js file.
很可能你的一个 js 文件;
最后缺少一个。打开您认为导致错误的那个并;
在末尾添加一个,或者;
在下一个 js 文件的第一行添加一个。
回答by dale.lotts
Modifying the javascript code is not fixing the root cause of the problem so this could happen again as soon as you introduce a new JavaScript file.
修改 javascript 代码并不能解决问题的根本原因,因此一旦引入新的 JavaScript 文件,这种情况可能会再次发生。
You have at least few choices for a permanent fix - they involve changing the build so that this cannot happen in the future, even if a semi-colon is missing:
对于永久修复,您至少有几个选择 - 它们涉及更改构建,以便将来不会发生这种情况,即使缺少分号:
- Inject a
;
between the files you are concatenating. This is usually a simple one-line change depending on how you are concatenating the files. - Minify the files first and then concatenate. This should leave a \n between the files you are concatenating, allowing ASI to take care of this for you.
;
在要连接的文件之间注入 a 。这通常是一个简单的一行更改,具体取决于您连接文件的方式。- 首先缩小文件,然后连接。这应该在您连接的文件之间留下一个 \n,让 ASI 为您处理这个问题。
回答by Amr Ibrahim
just add semi-clone and newline
只需添加半克隆和换行符
';\n'
at end of each file
在每个文件的末尾