![](/img/trans.png)
[英]How to rename the original files of scripts in index.html using gulp?
[英]Gulp: convert java scripts extension in index.html
我正在嘗試將Java腳本擴展名'.js'轉換為'.js.gz'。 為此,我選擇了gulp-html-replace模塊。 但是我無法獲得理想的結果。 對於下面的示例,我正在使用兩個腳本,但是我有更多。
index.html(之前):
<!-- build:js -->
<script src="routing_components/app-routing/app-routing.component.js"></script>
<script src="routing_components/home-routing/home-routing.component.js"></script>
<!-- endbuild -->
index.html(在gulp index.html之后應該是這樣的):
<script src="routing_components/app-routing/app-routing.component.js.gz"></script>
<script src="routing_components/home-routing/home-routing.component.js.gz"></script>
但是相反,我在運行gulp任務后得到了這個
<script src="index.js.gz"></script>
Gulp任務:
gulp.task('index_gzip', function () {
gulp.src('app/index.html')
.pipe(htmlreplace({
js: {
src: null,
tpl: '<script src="%f.js.gz"></script>'
}
}))
.pipe(gulp.dest(function (file) {
return file.base;
}))
});
如果有人對這種任務有更好的了解,請推薦。 並請幫助糾正我的任務
試試這個插件gulp-replace 。 但是我不知道為什么正則表達式分組不能正常工作。
var replace = require('gulp-replace');
gulp.task('index_gzip', function () {
gulp.src('app/index.html')
.pipe(replace(/(<script.*?src=\")(.*?).js\"/g, '$1$2.js.gz"'))
.pipe(gulp.dest(function (file) {
return file.base;
}))
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.