Windows 上 gulp 的编码问题
Encoding problems with gulp on Windows
我在 Visual Studio 2013 wep 应用程序项目中有几个源文件,我使用 gulp
版本 3.8.11 处理这些文件。这些文件是 Unicode (UTF-8 with signature) - Codepage 65001
编码的文本文件。处理它们后,它们看起来像 Windows 1252 编码的文本文件。
例如,给定以下 UTF-8 encoded
src/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>
这是它在浏览器上的样子:
使用以下 gulpfile.js
:
var gulp = require('gulp');
gulp.task('build', function () {
gulp.src('src/hello.html')
.pipe(gulp.dest('dist'));
});
在命令行中执行 gulp build
后,在浏览器中是这样的:
如何解决这个编码问题?请帮忙
我对 .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'));
});
我遇到了类似的问题,我通过添加
解决了它
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 你的可能不同。
我在 Visual Studio 2013 wep 应用程序项目中有几个源文件,我使用 gulp
版本 3.8.11 处理这些文件。这些文件是 Unicode (UTF-8 with signature) - Codepage 65001
编码的文本文件。处理它们后,它们看起来像 Windows 1252 编码的文本文件。
例如,给定以下 UTF-8 encoded
src/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>
这是它在浏览器上的样子:
使用以下 gulpfile.js
:
var gulp = require('gulp');
gulp.task('build', function () {
gulp.src('src/hello.html')
.pipe(gulp.dest('dist'));
});
在命令行中执行 gulp build
后,在浏览器中是这样的:
如何解决这个编码问题?请帮忙
我对 .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'));
});
我遇到了类似的问题,我通过添加
解决了它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 你的可能不同。