返回

compiler v1.0.0 + gt-next@6.7.0

Ernest McCarter avatarErnest McCarter
@general-translation/compilerv1.0.0compilertranslationi18n

概览

我们非常高兴地宣布发布 @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' | 'error' | 'warn' | 'info' | 'debug'

注意: 对于 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',
  },
})

结论

我们已经为编译器规划了更多工作,因此在接下来的几个月里可以期待频繁更新。