# Vite

> 英文标题：Vite
> 官网原文：https://typeorm.io/docs/migrations/vite
> 文档版本：TypeORM v1.1
> 体例：英文原文（EN）与中文译文（ZH）逐块并列，共 14 块

---

<!-- EN -->
# Vite

<!-- ZH -->
# Vite

<!-- EN -->
Using TypeORM in a [Vite](https://vite.dev) project is pretty straight forward. However, when you use [migrations](https://typeorm.io/docs/migrations/why), you will run into "...migration name is wrong. Migration class name should have a JavaScript timestamp appended." errors when running the production build. On production builds, files are [optimized by default](https://vite.dev/config/build-options#build-minify) which includes mangling your code in order to minimize file sizes.

<!-- ZH -->
在 [Vite](https://vite.dev) 项目中使用 TypeORM 相当直接。不过当你使用[迁移](https://typeorm.io/docs/migrations/why)时，运行生产构建会遇到 "...migration name is wrong. Migration class name should have a JavaScript timestamp appended." 错误。在生产构建中，文件[默认会被优化](https://vite.dev/config/build-options#build-minify)，其中包括混淆你的代码以缩小文件体积。

<!-- EN -->
You have 3 options to mitigate this. The 3 options are shown below as diff to this basic `vite.config.ts`

<!-- ZH -->
你有 3 个选项可以缓解这个问题。下面这 3 个选项以对基础 `vite.config.ts` 的 diff 形式给出

<!-- EN -->
```typescript
import legacy from "@vitejs/plugin-legacy"
import vue from "@vitejs/plugin-vue"
import path from "path"
import { defineConfig } from "vite"

// https://vitejs.dev/config/
export default defineConfig({
    build: {
        sourcemap: true,
    },
    plugins: [vue(), legacy()],
    resolve: {
        alias: {
            "@": path.resolve(__dirname, "./src"),
        },
    },
})
```

<!-- ZH -->
```typescript
import legacy from "@vitejs/plugin-legacy"
import vue from "@vitejs/plugin-vue"
import path from "path"
import { defineConfig } from "vite"

// https://vitejs.dev/config/
export default defineConfig({
    build: {
        sourcemap: true,
    },
    plugins: [vue(), legacy()],
    resolve: {
        alias: {
            "@": path.resolve(__dirname, "./src"),
        },
    },
})
```

<!-- EN -->
## Option 1: Disable minify

<!-- ZH -->
## 选项 1：禁用压缩

<!-- EN -->
This is the most crude option and will result in significantly larger files. Add `build.minify = false` to your config.

<!-- ZH -->
这是最粗暴的选项，会导致文件明显变大。在你的配置中加上 `build.minify = false`。

<!-- EN -->
```diff
--- basic vite.config.ts
+++ disable minify vite.config.ts
@@ -7,6 +7,7 @@
 export default defineConfig({
   build: {
     sourcemap: true,
+    minify: false,
   },
   plugins: [vue(), legacy()],
   resolve: {
```

<!-- ZH -->
```diff
--- basic vite.config.ts
+++ disable minify vite.config.ts
@@ -7,6 +7,7 @@
 export default defineConfig({
   build: {
     sourcemap: true,
+    minify: false,
   },
   plugins: [vue(), legacy()],
   resolve: {
```

<!-- EN -->
## Option 2: Disable esbuild minify identifiers

<!-- ZH -->
## 选项 2：禁用 esbuild 的标识符压缩

<!-- EN -->
Vite uses esbuild as the default minifier. You can disable mangling of identifiers by adding `esbuild.minifyIdentifiers = false` to your config. This will result in smaller file sizes, but depending on your code base you will get diminishing returns as all identifiers will be kept at full length.

<!-- ZH -->
Vite 默认使用 esbuild 作为压缩器。你可以通过在配置中加上 `esbuild.minifyIdentifiers = false` 来禁用标识符混淆。这会带来更小的文件体积，但根据你的代码库情况，收益会递减，因为所有标识符都会保留完整长度。

<!-- EN -->
```diff
--- basic vite.config.ts
+++ disable esbuild minify identifiers vite.config.ts
@@ -8,6 +8,7 @@
   build: {
     sourcemap: true,
   },
+  esbuild: { minifyIdentifiers: false },
   plugins: [vue(), legacy()],
   resolve: {
```

<!-- ZH -->
```diff
--- basic vite.config.ts
+++ disable esbuild minify identifiers vite.config.ts
@@ -8,6 +8,7 @@
   build: {
     sourcemap: true,
   },
+  esbuild: { minifyIdentifiers: false },
   plugins: [vue(), legacy()],
   resolve: {
```

<!-- EN -->
## Option 3: Use terser as minifier while keeping only the migration class names

<!-- ZH -->
## 选项 3：使用 terser 作为压缩器，同时只保留迁移类名

<!-- EN -->
Vite supports using terser as minifier. Terser is slower then esbuild, but offers more fine grained control over what to minify. Add `minify: 'terser'` with `terserOptions.mangle.keep_classnames: /^Migrations\d+$/` and `terserOptions.compress.keep_classnames: /^Migrations\d+$/` to your config. These options will make sure classnames that start with "Migrations" and end with numbers are not renamed during minification.

<!-- ZH -->
Vite 支持使用 terser 作为压缩器。Terser 比 esbuild 慢，但对"压缩什么"提供了更细粒度的控制。在你的配置中加上 `minify: 'terser'`，并设置 `terserOptions.mangle.keep_classnames: /^Migrations\d+$/` 与 `terserOptions.compress.keep_classnames: /^Migrations\d+$/`。这些选项会确保以 "Migrations" 开头、以数字结尾的类名在压缩过程中不被重命名。

<!-- EN -->
Make sure terser is available as dev dependency in your project: `npm add -D terser`.

<!-- ZH -->
请确保 terser 已作为开发依赖安装在你的项目中：`npm add -D terser`。

<!-- EN -->
```diff
--- basic vite.config.ts
+++ terser keep migration class names vite.config.ts
@@ -7,6 +7,11 @@
 export default defineConfig({
   build: {
     sourcemap: true,
+    minify: 'terser',
+    terserOptions: {
+      mangle: { keep_classnames: /^Migrations\d+$/ },
+      compress: { keep_classnames: /^Migrations\d+$/ },
+    },
   },
   plugins: [vue(), legacy()],
   resolve: {
```

<!-- ZH -->
```diff
--- basic vite.config.ts
+++ terser keep migration class names vite.config.ts
@@ -7,6 +7,11 @@
 export default defineConfig({
   build: {
     sourcemap: true,
+    minify: 'terser',
+    terserOptions: {
+      mangle: { keep_classnames: /^Migrations\d+$/ },
+      compress: { keep_classnames: /^Migrations\d+$/ },
+    },
   },
   plugins: [vue(), legacy()],
   resolve: {
```
