简体   繁体   English

Typescript const 枚举替代

[英]Typescript const enum alternative

More and more modern Typescript transpilers have moved toward a strategy of per-module transpilation, which significantly increases build speed, but eliminates the possibility for cross-module const enum usage, since transpilation of them requires type information.越来越多的现代 Typescript 转译器已转向按模块转译的策略,这显着提高了构建速度,但消除了跨模块const enum使用的可能性,因为它们的转译需要类型信息。

I have a significant amount of const enums that when used without the inlining that const provides:我有大量const enums ,当它们在没有const提供的内联的情况下使用时:

  1. End up being hundreds of KBs even after minification due to long property names由于较长的属性名称,即使在缩小后最终也是数百 KB
  2. Leak internal, backend property names that I don't want public泄漏我不想公开的内部后端属性名称

Right now I have these const enum definitions auto generated from backend native code.现在我有这些从后端本机代码自动生成的const enum定义。 As a contrived example, you can imagine I work at Apple and have a great big const enum of every hardware device.举个人为的例子,你可以想象我在 Apple 工作,并且对每个硬件设备都有一个很大的常量枚举。

const enum HardwareType {
    Apple1 = 0,
    // ...
    iPhoneX = 412,
    // ...
    iPhoneUltraXD = 499, // Some theoretical unannounced iPhone
}

If I just change const enum HardwareType to enum HardwareType , in addition to bumping up my bundle size, I've now leaked the new "iPhone Ultra XD" to the public.如果我只是将const enum HardwareType更改为enum HardwareType ,除了增加我的包大小之外,我现在已经向公众泄露了新的“iPhone Ultra XD”。

I see that something like Terser supports the --mangle-props option , but even that seems to be warned against in the official docs and also would mean creating a regex that covers every single HardwareType ?我看到像 Terser 这样的东西支持--mangle-props选项,但即便如此似乎在官方文档中也被警告反对并且还意味着创建一个涵盖每个HardwareType的正则表达式? Not to mention that's just my contrived example and I have dozens of these enums in reality with hundreds of values.更不用说这只是我人为的例子,我在现实中有几十个这样的枚举,有数百个值。

I'd really like to use the latest tech for application bundling, but is there really not a better option out there for compile time inlining of constant values?我真的很想使用最新的技术进行应用程序捆绑,但是对于常量值的编译时内联真的没有更好的选择吗?

const enum is not very secure in hiding original names. const enum在隐藏原始名称方面不是很安全。 As you can see in that playground typescript compiler adds original names in comments:正如您在playground typescript 中看到的那样,编译器在注释中添加了原始名称:

// Input:
const enum Fruites {
    Apple = 1,
    Banana = 2
}

const x = Fruites.Apple
const y = Fruites.Banana

// Output:
"use strict";
const x = 1 /* Apple */;
const y = 2 /* Banana */;

If your a really wondering to use the latest tech for application bundling and want to hide some secret names from output files, try to use esbuild-loader or esbuild itself.如果您真的想使用最新技术进行应用程序捆绑,并想从 output 文件中隐藏一些秘密名称,请尝试使用esbuild-loaderesbuild本身。 It support define option that allows you to replace some secret naming with meaningless values in compilation time like that它支持定义选项,允许您在编译时用无意义的值替换一些秘密命名

define: {
  "secrets.hardwareType.iPhoneUltraXD": "499"
}

and safely use the defined value in source code并安全地使用源代码中定义的值

// Source code: 
if (deviceId === secrets.hardwareType.iPhoneUltraXD) {
// Bundled code:
if(deviceId===499){

define option could be initiated in webpack or esbuild config file with any computed values (even with required json files) so you have no limit in count of compile-time definitions. define选项可以在 webpack 或带有任何计算值的 esbuild 配置文件中启动(即使需要 json 文件),因此您对编译时定义的数量没有限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM