Grunt和UglifyJS在Windows应用程序开发 - UTF8问题 [英] Grunt and UglifyJS in Windows app development - UTF8 problems

查看:131
本文介绍了Grunt和UglifyJS在Windows应用程序开发 - UTF8问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Windows 8 / 8.1 JavaScript应用程序。它基本上是我们web应用程序的一个端口。对于部署,我使用grunt,grunt-contrib-uglify和grunt-contrib-cssmin(接近一些,但这些与我的问题没有关系。)



UglifyJS用于将我的所有JS文件和库放在一个缩小的包中,对于CSS来说,它和CSSmin一样。



由于Windows应用程序认证强制使用UTF -8编码与签名(BOM),我所有的源文件编码的方式,我也设置默认编码为UTF-8与preserveBOM为true,虽然,我缩小的文件编码在us_ascii,甚至在二进制流。

Windows 8.1 32bit和Mac OS Mavericks的行为是一样的。



我该怎么办?

使用UTF-8 BOM编码重新保存此打包版本会导致通过认证检查,但应用程序未运行(白屏,看起来像缩小的JS是不再工作,但在重新保存之前它完美无瑕)。



这就是我正在做的在concat / minify之后的JS文件:

  var buf = grunt.file.read(fileName,{encoding:null}); 
var missingBOM =(buf [0]!== 0xEF&& buf [1]!== 0xBE&& buf [2]!== 0xBB);
if(missingBOM){
grunt.file.write(fileName,'\\\'+ buf,{encoding:'utf-8'});
}

更新:
我发现在我的合并文件中有多个BOM,所以我优化了我的脚本,在写新文件之前删除它们:

  var buf = grunt.file.read(dist + fileName,{encoding:null}); 
var BOM =新缓冲区([0xEF,0xBB,0xBF]);

//从缓冲区中删除多个BOM
var bufString = buf.toString('utf-8');
bufString = bufString.replace(BOM.toString('utf-8'),null);
buf = new Buffer(bufString,'utf-8');

//将新的UTF-8 BOM添加到文件缓冲区的开头
var bomFile = Buffer.concat([BOM,buf]);
grunt.file.write(dist + fileName,bomFile,{encoding:'utf-8'});

不过,没有运气-.-

解决方案

这个问题似乎是uglify合并文件和BOM的结合。
我当前的设置会缩小每个JS文件,但不会再合并它们(因为Windows应用程序将本地存储所有脚本,所以请求不成问题)。我在最后使用更新版本2的代码片段将BOM添加到所有这些BOM中。



我的应用程序现在可以成功验证并且完美无缺地运行。呃......


I'm developing a Windows 8/8.1 JavaScript app. It basicallly is a port of our webapp. For deployment, I'm using grunt, grunt-contrib-uglify and grunt-contrib-cssmin (next to some more, but those are not connected to my problem.

UglifyJS is used to package all my JS files and libs in a minified bundle for each. For the CSS it's the same using CSSmin.

As the Windows app certification forces a UTF-8 encoding with signature (BOM), all my source files are encoded that way. I also did set the grunt default encoding to UTF-8 with preserveBOM to true. Though, my minified files ar encoded in us_ascii, some even in binary stream.

That behavior is the same for Windows 8.1 32bit and Mac OS Mavericks.

What can I do?

Re-saving this packed version with UTF-8 BOM encoding leads to a passed certification check, but the app is not running (white screen, looks like the minified JS is not working anymore, but it does flawlessly before re-saving).

This is what I'm doing with the JS files after concat/minify:

var buf = grunt.file.read(fileName, { encoding: null });
var missingBOM = (buf[0] !== 0xEF && buf[1] !== 0xBE && buf[2] !== 0xBB);
if (missingBOM) {
  grunt.file.write(fileName, '\ufeff' + buf, { encoding: 'utf-8' });
}

Update: I discovered that there are multiple BOMs in my merged files so I refined my script to strip those out before writing the new one:

var buf = grunt.file.read(dist + fileName, { encoding: null });
var BOM = new Buffer([0xEF,0xBB,0xBF]);

// remove multi BOMs from Buffer
var bufString = buf.toString('utf-8');
bufString = bufString.replace(BOM.toString('utf-8'), null);
buf = new Buffer(bufString, 'utf-8');

// add new UTF-8 BOM to the beginning of the file buffer
var bomFile = Buffer.concat([BOM,buf]);
grunt.file.write(dist + fileName, bomFile, { encoding: 'utf-8' });

Still, no luck -.-

解决方案

The Problem really seems to be the combination of uglify merged files and BOM. My current setup minifies each JS file but doesn't merge them anymore (there's no need to, as the Windows app will store all scripts locally so requests are not a problem). I'm adding the BOM to all of them at the end using the snippet from update 2 of my main post.

My app now successfully certifies and runs flawlessly. Phew...

这篇关于Grunt和UglifyJS在Windows应用程序开发 - UTF8问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆