[英]Remove scripts from index.html through gulp
I'am using gulp in our application, we have 2 flows in Gulpfile.js, one for production and second for development, but I dont want to keep 2 index.html files eg index.html and index.dev. 我在我们的应用程序中使用gulp,我们在Gulpfile.js中有2个流程,一个用于生产,第二个用于开发,但我不想保留2个index.html文件,例如index.html和index.dev。 html, I want to have one index.html file, but for production build I have scripts which are no needed eg .
html,我想要一个index.html文件,但对于生产版本,我有不需要的脚本,例如。
<!--dev depends -->
<script src="angular-mocks/angular-mocks.js"></script>
<script src="server.js"></script>
<!--dev depends -->
question is: How can I remove something from html through Gulp ? 问题是: 如何通过Gulp从html中删除某些内容 ?
You can use the gulp-html-replace plugin which is intended for this specific purpose : 您可以使用专门用于此特定目的的gulp-html-replace插件:
https://www.npmjs.org/package/gulp-html-replace https://www.npmjs.org/package/gulp-html-replace
You could approach is slightly differently: templatize your index.html
and use the gulp-template
plugin to process the template in your build: 您的方法可能略有不同:模板化您的
index.html
并使用gulp-template
插件处理您的构建中的模板:
var template = require('gulp-template');
//production task
gulp.task('prod', function () {
return gulp.src('src/index.html').pipe(template({
scripts : []
})).pipe(gulp.dest('dist'));
});
//dev task
gulp.task('prod', function () {
return gulp.src('src/index.html').pipe(template({
scripts : ['angular-mocks/angular-mocks.js', 'server.js']
})).pipe(gulp.dest('dist'));
});
and your index.html
could be turned into a template like so: 并且您的
index.html
可以变成这样的模板:
<% _.forEach(scripts, function(name) { %><script src="<%- name %>" type="text/javascript"></script><% }); %>
Of course you could write your own plugin / pass-through stream that removes scripts from your index.html but it would require actual parsing / re-writing of the index.html. 当然,您可以编写自己的插件/传递流,从index.html中删除脚本,但这需要对index.html进行实际的解析/重写。 personally I find the template-based solution easier to put in place and more "elegant".
我个人认为基于模板的解决方案更容易实施,更“优雅”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.