返回

编译器 v1.0.0 + gt-next@6.7.0

Ernest McCarter avatarErnest McCarter
@general-translation/compilerv1.0.0编译器翻译i18n

概述

我们非常高兴地宣布推出 @general-translation/compiler v1.0.0。

正如我们在 gt-next@6.2.0 的发布中所说,我们相信 i18n 库的未来在于构建时处理。今年早些时候,我们为 gt-next 发布了一个 SWC 插件,带来了编译时哈希和更出色的开发时字符串翻译等优化。虽然这让我们的团队对编译器插件有了一次极佳 (尽管颇具挑战) 的初步实践,但它的适用范围仅限于 Next.js 项目。

现在,我们通过发布这个通用的 React 编译器插件,进一步践行对构建时处理的承诺。这意味着,gt-next 中的所有构建时优化现在都已向所有 gt-react 用户开放!

安装

配置

选项类型默认值说明
compileTimeHashbooleantrue启用编译时哈希
disableBuildChecksbooleanfalse检测到无效的库用法时,不抛出错误
logLevelstring'warn'控制日志级别。可选值:'silent'

注意: 对于 gt-next,出于向后兼容性的要求,如果将 compileTimeHash 设置为 false,整个插件都会被禁用。

gt-react

首先需要安装该软件包:

npm install @generaltranslation/compiler

Vite

// vite.config.js
import { defineConfig } from 'vite'
import { vite as gtCompiler } from '@generaltranslation/compiler'

export default defineConfig({
  plugins: [gtCompiler()],
})

Webpack

// webpack.config.js
const { webpack: gtCompiler } = require('@generaltranslation/compiler')

module.exports = {
  plugins: [gtCompiler()],
}

Rollup

// rollup.config.js
import { rollup as gtCompiler } from '@generaltranslation/compiler'

export default {
  plugins: [gtCompiler()],
}

gt-next

你无需安装任何额外软件包。

对于 gt-next,我们正在弃用 experimentalSwcPluginOptions,改用新的 experimentalCompilerOptions 字段。关键区别在于,你现在必须指定要使用的编译器类型:'swc''babel'。默认使用 'swc' 编译器;如果将 type 字段设为 'babel',则可启用新的基于 Babel 的编译器。

// next.config.js
import { withGTConfig } from 'gt-next'

export default withGTConfig(nextConfig, {
  experimentalCompilerOptions: {
    type: 'babel',
  },
})

总结

我们还为编译器规划了许多后续工作,因此接下来几个月会持续频繁更新。