简体   繁体   English

如何连接javascript文件并替换HTML中的引用?

[英]How can I concatenate javascript files and replace the reference in my HTML?

As part of my build/CI process on TeamCity, I would like to specify a set of JavaScript files referenced in my HTML and combine them into one. 作为TeamCity上构建/ CI过程的一部分,我想指定一组在我的HTML中引用的JavaScript文件,并将它们合并为一个。

In other words, this is what I would like to accomplish. 换句话说,这就是我想要实现的目标。

Before: 之前:

<script src="1.js" />
<script src="2.js" />

After

<script src="combined.js" />

I am looking for a commandline tool to concatenate 1.js and 2.js into combined.js. 我正在寻找一个命令行工具来将1.js和2.js连接成combined.js。 Then, a commandline tool (or maybe the same tool) to replace the references in the HTML file to this new file. 然后,命令行工具(或可能是相同的工具)将HTML文件中的引用替换为此新文件。 Please tell me how I could accomplish this. 请告诉我如何才能做到这一点。

What I have tried so far: 到目前为止我尝试过的:

I have been looking at grunt-usemin which looks good, but requires the build server to perform an npm install on each build to get the dependencies, then run it. 我一直在寻找看起来不错的grunt-usemin ,但是需要构建服务器在每个构建上执行npm install以获取依赖项,然后运行它。 That takes too long and isn't a nice solution, because we build+deploy very frequently. 这需要太长时间并且不是一个好的解决方案,因为我们非常频繁地构建+部署。

I know I could also add my node_modules folder to git but this is also undesirable. 我知道我也可以将我的node_modules文件夹添加到git中,但这也是不可取的。 It would be nice if grunt could run these modules installed globally but it is not the grunt way (unless I am mistaken, grunt wants everything installed locally). 如果grunt可以运行全局安装这些模块会很好,但它不是那种笨拙的方式(除非我弄错了,grunt想要在本地安装所有东西)。

Someone also suggested running grunt on the developer machines. 有人还建议在开发者机器上运行grunt。 Again, undesirable as we have transient VMs and this would break the development flow. 同样,由于我们有瞬态虚拟机而不受欢迎,这会破坏开发流程。

It would be nice if I could run grunt-usemin locally without grunt! 如果我可以在没有咕噜声的情况下在本地运行grunt-usemin会很好!

For combining the files you can just use cat 要合并文件,您只需使用cat

cat 1.js 2.js > combined.js

To replace the chunk of html you could use sed 要替换大块的html,你可以使用sed

sed -i "s/<script src=\"1.js\">\n<script src=\"2.js\">/<script src=\"combined.js\">/g" *.html

This is a rather general solution, but it seems a bit clunky to me. 这是一个相当普遍的解决方案,但对我来说似乎有点笨拙。 If you're rendering this html in node you might consider replacing it in javascript if the combined file exists. 如果你在节点中渲染这个html,你可以考虑在javascript中替换它,如果组合文件存在的话。

if (fs.existsSync('combined.js')) {
    res.end(html_contents.replace('<script src="1.js"/>\n<script src="2.js"/>','<script src="combined.js">'));
} else {
    res.end(html_contents);
}

for better performance you can of course use the asynchronous version of fs.exists 为了获得更好的性能,您当然可以使用fs.exists的异步版本

I ended up creating my own: https://npmjs.org/package/hashcat 我最终创建了自己的: https//npmjs.org/package/hashcat

npm install -g hashcat

@gustavohenke's recommendation was close but ultimately h5bp was proving to be too problematic for use on a build server. @ gustavohenke的建议很接近,但最终h5bp在构建服务器上使用时证明太麻烦了。 Ultimately creating this module did not take long and serves my needs for now. 最终创建这个模块不需要很长时间,现在满足我的需求。 I will mark this as the answer until a better answer comes along in the future. 我将此标记为答案,直到将来出现更好的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM