Skip to content

Commit

Permalink
feat(plugin-loader): support esm and ts(experiment) configure load (#197
Browse files Browse the repository at this point in the history
)


[Experiment] Node.js v22 LTS natively load TypeScript configuration files:
need inject env to turn on:
1. czg: `NODE_OPTIONS='--experimental-transform-types --disable-warning ExperimentalWarning' czg`
2. commitizen + cz-git: `NODE_OPTIONS='--experimental-transform-types --disable-warning ExperimentalWarning' cz`
  • Loading branch information
Zhengqbbb authored Nov 1, 2024
1 parent c6589de commit 1288093
Show file tree
Hide file tree
Showing 7 changed files with 238 additions and 37 deletions.
17 changes: 9 additions & 8 deletions .commitlintrc.cjs → commitlint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
const { execSync } = require('node:child_process')
const fg = require('fast-glob')
import { execSync } from 'node:child_process'
import { defineConfig } from 'cz-git'
import fg from 'fast-glob'

// git branch name = feature/cli_33 => auto get defaultIssues = #33
/** Get git branch issue number. e.g feature/cli_33 => #33 */
const issue = execSync('git rev-parse --abbrev-ref HEAD')
.toString()
.trim()
.split('_')[1]

// dynamic get monorepo packages name
const packages = fg.sync('*', { cwd: 'packages/@cz-git', onlyDirectories: true })
/** Get monorepo packages name */
const packages = fg
.sync('*', { cwd: 'packages/@cz-git', onlyDirectories: true })

/** @type {import('cz-git').UserConfig} */
module.exports = {
export default defineConfig({
extends: ['@commitlint/config-conventional'],
rules: {
'scope-enum': [2, 'always', ['cz-git', 'site', 'cli', ...packages]],
Expand All @@ -37,4 +38,4 @@ module.exports = {
customIssuePrefixAlign: !issue ? 'top' : 'bottom',
defaultIssues: !issue ? '' : `#${issue}`,
},
}
})
83 changes: 75 additions & 8 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ sitemap:
- `commitlint.config.cjs`
- `commitlint.config.mjs`

::::: details Click to expand `.commitlintrc.js` complete default configuration template
::::: details Click to expand `commitlint.config.js` complete default configuration template

:::: code-group
::: code-group-item CommonJS
Expand Down Expand Up @@ -77,7 +77,7 @@ If your project does not use commitlint,and want to use other profiles. You can

## Emoji template

::: details Click to expand `.commitlintrc.js` complete emoji template template
::: details Click to expand `commitlint.config.js` complete emoji template template

<<< @/snippets/commitlint.config.emoji.js{24-34,36 js}

Expand Down Expand Up @@ -121,13 +121,80 @@ https://cdn.jsdelivr.net/gh/Zhengqbbb/cz-git@{{ v }}/docs/public/schema/cz-git.j

## TypeScript template

:::danger
**Since <u>v1.3.0</u>. The typescript configuration file will no longer be loaded**. e.g(`cz.config.ts`)
- Added in `cz-git`, `czg` version `v1.11.0`
- Will use Node.js LTS version >= `v22.11.0` ==experimental feature==, **native TypeScript configuration file loading**<br>==Need injection of experimental options== `NODE_OPTIONS='--experimental-transform-types --disable-warning ExperimentalWarning'` to use
- For details: [see Node.js documentation](https://nodejs.org/api/cli.html#--experimental-transform-types) | [Node.js TypeScript support roadmap](https://github.com/nodejs/loaders/issues/217)
- ==TIP==: You can first use [ESM js](#javascript-template) configuration files as a transition for future TypeScript configuration files, and switch after Node.js runs TypeScript stably

::::: details Click to expand `NODE_OPTIONS` injection methods
:::: code-group
::: code-group-item package.json - with cross-env package
```diff
"scripts": {
- "cz": "czg"
+ "cz": "cross-env NODE_OPTIONS='--experimental-transform-types --disable-warning ExperimentalWarning' czg"
}
```
:::
::: code-group-item Global - with alias command
```sh
# .zshrc | .bashrc
alias czg="NODE_OPTIONS='--experimental-transform-types --disable-warning ExperimentalWarning' \czg"
```
:::
::: code-group-item Global - with export command
```sh
# .zshrc | .bashrc
export NODE_OPTIONS="--experimental-transform-types --disable-warning ExperimentalWarning"
```
:::
::::
:::::
---
- `.commitlintrc.ts`
- `.commitlintrc.mts`
- `.commitlintrc.cts`
- `commitlint.config.ts`
- `commitlint.config.mts`
- `commitlint.config.cts`
::::: details Click to expand `commitlint.config.ts` complete default configuration template
:::: code-group
::: code-group-item CommonJS
<<< @/snippets/commitlint.config.cjs{ts}
:::
::: code-group-item ESM
<<< @/snippets/commitlint.config.mjs{ts}
:::
::::
:::::
---
- `cz.config.ts`
- `cz.config.mts`
- `cz.config.cts`
::::: details Click to expand `cz.config.ts` complete default configuration template
:::: code-group
::: code-group-item CommonJS
<<< @/snippets/cz.config.cjs{ts}
- Using the TypeScript configuration file will **affects command line tool startup speed**.
- Increase the package size.
:::
::: code-group-item ESM
<<< @/snippets/cz.config.mjs{ts}
:::tip
Using the js configuration file to add the `@type` annotation can be a good way to provide code hints at configuration time.
:::
::::
:::::
86 changes: 78 additions & 8 deletions docs/zh/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ sitemap:
- `commitlint.config.cjs`
- `commitlint.config.mjs`

::::: details 点击展开 `.commitlintrc.js` 完整 默认 配置模板
::::: details 点击展开 `commitlint.config.js` 完整 默认 配置模板

:::: code-group
::: code-group-item CommonJS
Expand Down Expand Up @@ -77,7 +77,7 @@ sitemap:

## 中英文对照模板

::: details 点击展开 `.commitlintrc.js` 完整 中英文 配置模板
::: details 点击展开 `commitlint.config.js` 完整 中英文 配置模板

<<< @/snippets/commitlint.config.cn-en.js{js}

Expand All @@ -90,15 +90,15 @@ sitemap:
[推荐使用中英文对照](#中英文对照模板),可以很好给予团队的新人帮助。
:::

::: details 点击展开 `.commitlintrc.js` 完整 纯汉化 配置模板
::: details 点击展开 `commitlint.config.js` 完整 纯汉化 配置模板

<<< @/snippets/commitlint.config.cn.js{js}

:::

## Emoji 模板

::: details 点击展开 `.commitlintrc.js` 完整 emoji 配置模板
::: details 点击展开 `commitlint.config.js` 完整 emoji 配置模板

<<< @/snippets/commitlint.config.emoji.js{24-34,36 js}

Expand Down Expand Up @@ -142,10 +142,80 @@ https://cdn.jsdelivr.net/gh/Zhengqbbb/cz-git@{{ v }}/docs/public/schema/cz-git.j

## TypeScript 模板

:::danger
<u>v1.3.0</u> 开始,将不再支持 TypeScript 配置文件。例如(`cz.config.ts`)
-`cz-git`, `czg``v1.11.0` 版本开始
- 将使用 Node.js LTS 版本 >= `v22.11.0`==实验性功能==, **原生加载 TypeScript 配置文件**<br>==需注入开启实验性的参数== `NODE_OPTIONS='--experimental-transform-types --disable-warning ExperimentalWarning'` 使用
- 详情: [可查看 Node.js 文档](https://nodejs.org/api/cli.html#--experimental-transform-types) | [Node.js TypeScript 支持路线图](https://github.com/nodejs/loaders/issues/217)
- ==建议==: 可先使用 [ESM js](#javascript-%E6%A8%A1%E6%9D%BF) 配置文件作为未来 TypeScript 配置文件的过渡,待 Node.js 运行 TypeScript 稳定后,再进行切换

::::: details 点击展开 `NODE_OPTIONS` 注入方式
:::: code-group
::: code-group-item 项目 package.json 脚本注入 - 配合包 cross-env
```diff
"scripts": {
- "cz": "czg"
+ "cz": "cross-env NODE_OPTIONS='--experimental-transform-types --disable-warning ExperimentalWarning' czg"
}
```
:::
::: code-group-item 全局注入 - 使用 alias 命令
```sh
# .zshrc | .bashrc
alias czg="NODE_OPTIONS='--experimental-transform-types --disable-warning ExperimentalWarning' \czg"
```
:::
::: code-group-item 全局注入 - 使用 export 命令
```sh
# .zshrc | .bashrc
export NODE_OPTIONS="--experimental-transform-types --disable-warning ExperimentalWarning"
```
:::
::::
:::::

---

- `.commitlintrc.ts`
- `.commitlintrc.mts`
- `.commitlintrc.cts`
- `commitlint.config.ts`
- `commitlint.config.mts`
- `commitlint.config.cts`

::::: details 点击展开 `commitlint.config.ts` 完整 默认 配置模板

:::: code-group
::: code-group-item CommonJS

<<< @/snippets/commitlint.config.cjs{ts}

:::tip
使用 js 配置文件添加 `@type` 注释可以很好提供在配置时的代码提示.
:::
::: code-group-item ESM

<<< @/snippets/commitlint.config.mjs{ts}

:::
::::

:::::

---

- `cz.config.ts`
- `cz.config.mts`
- `cz.config.cts`

::::: details 点击展开 `cz.config.ts` 完整 默认 配置模板
:::: code-group
::: code-group-item CommonJS

<<< @/snippets/cz.config.cjs{ts}

:::
::: code-group-item ESM

<<< @/snippets/cz.config.mjs{ts}

:::
::::

:::::
1 change: 1 addition & 0 deletions packages/@cz-git/plugin-loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"devDependencies": {
"@commitlint/resolve-extends": "catalog:commitlint",
"@commitlint/types": "catalog:commitlint",
"@cz-git/inquirer": "workspace:*",
"@types/tmp": "^0.2.3",
"cosmiconfig": "catalog:",
"pkg-dir": "5.0.0",
Expand Down
43 changes: 43 additions & 0 deletions packages/@cz-git/plugin-loader/src/esm-ts-loader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import process from 'node:process'
import { style } from '@cz-git/inquirer'
import type { Loader } from 'cosmiconfig'

type LoaderError = Error & {
code?: string
}

// export NODE_OPTIONS="--experimental-transform-types --disable-warning ExperimentalWarning"
export function esmTsLoader(): Loader {
return async (cfgPath: string, _: string) => {
try {
const result = await import(cfgPath) as { default?: any }
return result.default || result
}
catch (e: any) {
// TODO: bun - The Current native bun will cause the TUI unnormal
// @ts-ignore // Usage: `deno task cz`
const isDeno = typeof Deno !== 'undefined' && Deno?.version?.deno
if (isDeno)
throw e

const error = e as LoaderError
const isNodeLTSInRange = (() => {
if (!process.version.startsWith('v'))
return false
const major = process.version.split('.')[0].slice(1)
const minor = process.version.split('.')[1]
return Number(major) >= 22 && Number(minor) >= 10
})()
if (error?.code === 'ERR_UNKNOWN_FILE_EXTENSION') {
if (isNodeLTSInRange)
console.log(style.gray(`Loading file: ${cfgPath}\nRequires injecting experimental NODE_OPTIONS env: --experimental-transform-types\nSee: ${style.underline('https://cz-git.qbb.sh/config/#typescript-template')}`))
else
console.log(style.gray(`Loading file: ${cfgPath}\n1. Requires Node.js version >= v22.10.0\n2. Inject experimental NODE_OPTIONS env: --experimental-transform-types\nSee: ${style.underline('https://cz-git.qbb.sh/config/#typescript-template')}`))
return {}
}
else {
throw error
}
}
}
}
Loading

0 comments on commit 1288093

Please sign in to comment.