2015-04-23 45 views
6

我在使用gulp版本3.8.11處理的Visual Studio 2013 wep應用程序項目中有幾個源文件。這些文件是Unicode (UTF-8 with signature) - Codepage 65001編碼的文本文件。處理它們後,它們顯示爲Windows 1252編碼的文本文件。在Windows上使用gulp編碼問題

例如,給出以下UTF-8 encodedsrc/hello.html文件:

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title>Hello</title> 
</head> 
<body> 
    <h1>Hello, my name is Jesús López</h1> 
</body> 
</html> 

這是它的外觀上的瀏覽器:

enter image description here

使用以下gulpfile.js

var gulp = require('gulp'); 

gulp.task('build', function() { 
    gulp.src('src/hello.html') 
     .pipe(gulp.dest('dist')); 
}); 

在命令行中執行gulp build後,這是它的外觀上的瀏覽器:

enter image description here

我怎樣才能解決這個編碼的問題?請幫忙。

+0

這非常有趣。我查看了'gulp'源代碼,它似乎不可能用gulp 3將編碼選項傳遞給'gulp.src'。你可能會有一些插件的運氣。快速谷歌搜索將返回許多結果。我認爲你應該向開發者提出這個問題。 –

+0

什麼是你的吞嚥版本? –

+0

@Lim H.我嘗試過'gulp-convert-encoding',但沒有運氣。我在gitub https://github.com/gulpjs/gulp/issues/1037上提出了一個問題。 –

回答

14

我對.cshtml文件有同樣的問題,我發現這是因爲缺少UTF-8 BOM。這可以很容易地添加一個叫做gulp-header的gulp插件。

var gulp = require('gulp'); 
var header = require('gulp-header'); 

gulp.task('build', function() { 
    gulp.src('src/hello.html') 
     .pipe(header('\ufeff')); 
     .pipe(gulp.dest('dist')); 
}); 
+0

感謝您的回答。有點可笑,我們不得不手動做到 - 我期望'dest()'有一個編碼參數。希望瞭解當前行爲背後的基本原理。 – zeh

1

我有類似的問題,我解決它通過添加

var convertEncoding = require('gulp-convert-encoding'); 

    gulp.task("copy:templates", function() { 
     return gulp 
      .src("./app/**/*.html") 
      .pipe(convertEncoding({ from: "windows1250", to: "utf8" })) 
      //.pipe(header('\ufeff')) 
      .pipe(gulp.dest("./wwwroot/app")); 
    }); 

    gulp.task('build:ts', function() { 

     return gulp.src("./app/**/*.ts") 
      .pipe(sourcemaps.init()) 
      .pipe(typescript(tsconfigBuildTs)) 
      .pipe(sourcemaps.write()) 
      .pipe(convertEncoding({ from: "windows1250", to: "utf8" })) 
      .pipe(gulp.dest("./wwwroot/app/")); 
    }); 

最重要的線是

.pipe(convertEncoding({ from: "windows1250", to: "utf8" })) 

您應該從文件中檢查文件編碼爲>高級保存。我被設置爲windows1250,你的可能會有所不同。