javascripttypescriptunicodeesbuildvariable-names

How to preserve Unicode characters in name when bundling with esbuild?


In test.ts I have:

const π: number = 3.14

Running esbuild .\test.ts I get:

const \u03C0 = 3.14;

Is there a way to keep the variable name as π? Likewise for function names, class names, types, interfaces, etc. So far I only see comments are preserved.


Solution

  • Use esbuild --charset=utf8 .\test.ts

    The text below is copied from the documentation:


    By default esbuild's output is ASCII-only. Any non-ASCII characters are escaped using backslash escape sequences. One reason is because non-ASCII characters are misinterpreted by the browser by default, which causes confusion. You have to explicitly add <meta charset="utf-8"> to your HTML or serve it with the correct Content-Type header for the browser to not mangle your code. Another reason is that non-ASCII characters can significantly slow down the browser's parser. However, using escape sequences makes the generated output slightly bigger, and also makes it harder to read.

    If you would like for esbuild to print the original characters without using escape sequences and you have ensured that the browser will interpret your code as UTF-8, you can disable character escaping by setting the charset:

    echo 'let π = Math.PI' | esbuild
    let \u03C0 = Math.PI;
    echo 'let π = Math.PI' | esbuild --charset=utf8
    let π = Math.PI;
    

    Some caveats: