locked
Fix encoding using command line or msbuild

    Question

  • We compile many small related js files into one using MSBuild. And have some issue with encoding:

    File encoding
    FAILED
    UTF-8 file encoding
    • Error Found: The UTF-8 file encoding test detected the following errors:
      • File C:\Program Files\WindowsApps\someapp\somepath\compiled.js is not properly UTF-8 encoded. Re-save the file as UTF-8 (including Byte Order Mark).
    • Impact if not fixed: HTML, CSS, and JavaScript files must be encoded in UTF-8 form with a corresponding byte-order mark (BOM) in order to benefit from bytecode caching and to avoid other runtime error conditions.
    • How to fix: Open the affected file, and select "Save As..." option from the File menu in Visual Studio. Select the drop-down control next to the Save button and select "Save with Encoding”... option. From the Advanced save options dialog, choose the "Unicode (UTF-8 with signature)" option and click the OK button. 

    Can we fix it automatically using msbuild or something command line tools? Not by clicking buttons in visual studio.

    Monday, April 15, 2013 10:42 PM

Answers

  • Find workaround whats work in my case. Just put BOM to top of file before compile it.
      <ItemGroup>
        <CompileOutput Include="js\player\compiled.js" />
        <CompileInput Include="js\player\**\*.js" Exclude="@(CompiledPlugins)" />
      </ItemGroup>
    
      <Target Name="CompilePlugins"
              Inputs="@(CompileInput)"
              Outputs="@(CompileOutput)">
    
        <PropertyGroup>
          <BOMCode>65279</BOMCode>
          <BOM>$([System.Convert]::ToChar( $([System.Int32]::Parse( $(BOMCode) )) ))</BOM>
        </PropertyGroup>
    
        <!-- clean old file and write BOM into it  -->
        <WriteLinesToFile File="@(CompileOutput)"
                          Overwrite="true"
                          Lines="$(BOM)" />
    
        <!-- Calls ones for each file so cannot use override here -->
        <WriteLinesToFile File="@(CompileOutput)"
                          Overwrite="false"
                          Lines="$([System.IO.File]::ReadAllText(%(CompileInput.Identity)))" />
    

    • Marked as answer by Anton Pogonets Wednesday, April 17, 2013 10:35 AM
    Tuesday, April 16, 2013 2:54 PM

All replies