简体   繁体   English

如何使用gulp向html文件注入已安装的Bower包的链接

[英]How to use gulp to inject to an html file the links for the installed Bower packages

I made a gulpfile to setup a front-end development working environment. 我制作了一个gulpfile来设置前端开发工作环境。

Here is how it works: 下面是它的工作原理:

The script grabs Bower packages defined in bower.json and outputs in to the /public folder the main files of every installed package, js and css in their respective production folders (/public/js) (/public/css). 该脚本获取bower.json中定义的Bower包,并将每个已安装包的主文件(js和css)输出到/ public文件夹中的各个生产文件夹(/ public / js)(/ public / css)。

The scripts also starts a watch task on all files, css, js and html files and whenever I save those files on the development folder (/src) their /public counterparts will be updated in real-time. 脚本还会对所有文件,css,js和html文件启动监视任务,每当我将这些文件保存在开发文件夹(/ src)上时,它们/公共对应文件将实时更新。

This way i simply install packages locally with Bower and by running this gulpfile i will have the production files ready in my /public folder, but i need to link them! 这样我只需在Bower本地安装软件包,并通过运行这个gulp文件,我将生成文件准备好在我的/公共文件夹中,但我需要链接它们!

As said, the missing part that I want the script to write links to those files in the head of my index.html, based on what packages I decided to install from Bower. 如上所述,我希望脚本根据我决定从Bower安装的软件包编写链接到index.html头部的文件的缺失部分。

So for example, if I downloaded Bootstrap, I want the script to automatically inject in to the head of my html page the link to Bootstrap css and js files right before the body closing tag (ideally). 因此,例如,如果我下载了Bootstrap,我希望脚本能够在我的html页面的头部自动注入Bootstrap css和js文件的链接,直接在正文结束标记之前(理想情况下)。

Here's my script on github , have a look at the README and at the gulpfile : 这是我在github上的脚本,看一下README和gulpfile

I don't know what approach and/or if there's a gulp plugin to use to achieve that, if someone can point me in the right direction i would appreciate very much. 我不知道采用什么方法和/或是否有一个gulp插件来实现这一目标,如果有人能指出我正确的方向我会非常感激。 Thanks. 谢谢。

为此,我目前使用gulp-inject ,就像一个魅力!

I was looking for this too. 我也在寻找这个。 And it seems that gulp-processhtml plugin can fit for this task and/ or gulp-replace and/ or gulp-preprocess . 似乎gulp-processhtml插件可以适合这个任务和/或gulp-replace和/或gulp-preprocess

How about just using 如何使用

gulp wiredep

Example: 例:

Add Holder.js package to existing Yeoman/Bower/Gulp project. 将Holder.js包添加到现有的Yeoman / Bower / Gulp项目中。

bower install holderjs --save

This added it to the dependency array. 这将它添加到依赖数组中。 After you need to get it added to your html by using 在你需要通过使用它添加到你的HTML后

gulp wiredep

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

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