Skip to content

Commit 1288093

Browse files
authored
feat(plugin-loader): support esm and ts(experiment) configure load (#197)
[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`
1 parent c6589de commit 1288093

7 files changed

Lines changed: 238 additions & 37 deletions

File tree

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
const { execSync } = require('node:child_process')
2-
const fg = require('fast-glob')
1+
import { execSync } from 'node:child_process'
2+
import { defineConfig } from 'cz-git'
3+
import fg from 'fast-glob'
34

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

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

13-
/** @type {import('cz-git').UserConfig} */
14-
module.exports = {
15+
export default defineConfig({
1516
extends: ['@commitlint/config-conventional'],
1617
rules: {
1718
'scope-enum': [2, 'always', ['cz-git', 'site', 'cli', ...packages]],
@@ -37,4 +38,4 @@ module.exports = {
3738
customIssuePrefixAlign: !issue ? 'top' : 'bottom',
3839
defaultIssues: !issue ? '' : `#${issue}`,
3940
},
40-
}
41+
})

‎docs/config/index.md‎

Lines changed: 75 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ sitemap:
1717
- `commitlint.config.cjs`
1818
- `commitlint.config.mjs`
1919

20-
::::: details Click to expand `.commitlintrc.js` complete default configuration template
20+
::::: details Click to expand `commitlint.config.js` complete default configuration template
2121

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

7878
## Emoji template
7979

80-
::: details Click to expand `.commitlintrc.js` complete emoji template template
80+
::: details Click to expand `commitlint.config.js` complete emoji template template
8181

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

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

122122
## TypeScript template
123123

124-
:::danger
125-
**Since <u>v1.3.0</u>. The typescript configuration file will no longer be loaded**. e.g(`cz.config.ts`)
124+
- Added in `cz-git`, `czg` version `v1.11.0`
125+
- 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
126+
- 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)
127+
- ==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
128+
129+
::::: details Click to expand `NODE_OPTIONS` injection methods
130+
:::: code-group
131+
::: code-group-item package.json - with cross-env package
132+
```diff
133+
"scripts": {
134+
- "cz": "czg"
135+
+ "cz": "cross-env NODE_OPTIONS='--experimental-transform-types --disable-warning ExperimentalWarning' czg"
136+
}
137+
```
138+
:::
139+
::: code-group-item Global - with alias command
140+
```sh
141+
# .zshrc | .bashrc
142+
alias czg="NODE_OPTIONS='--experimental-transform-types --disable-warning ExperimentalWarning' \czg"
143+
```
144+
:::
145+
::: code-group-item Global - with export command
146+
```sh
147+
# .zshrc | .bashrc
148+
export NODE_OPTIONS="--experimental-transform-types --disable-warning ExperimentalWarning"
149+
```
150+
:::
151+
::::
152+
:::::
153+
154+
---
155+
156+
- `.commitlintrc.ts`
157+
- `.commitlintrc.mts`
158+
- `.commitlintrc.cts`
159+
- `commitlint.config.ts`
160+
- `commitlint.config.mts`
161+
- `commitlint.config.cts`
162+
163+
::::: details Click to expand `commitlint.config.ts` complete default configuration template
164+
165+
:::: code-group
166+
::: code-group-item CommonJS
167+
168+
<<< @/snippets/commitlint.config.cjs{ts}
169+
170+
:::
171+
::: code-group-item ESM
172+
173+
<<< @/snippets/commitlint.config.mjs{ts}
174+
175+
:::
176+
::::
177+
178+
:::::
179+
180+
---
181+
182+
- `cz.config.ts`
183+
- `cz.config.mts`
184+
- `cz.config.cts`
185+
186+
::::: details Click to expand `cz.config.ts` complete default configuration template
187+
:::: code-group
188+
::: code-group-item CommonJS
189+
190+
<<< @/snippets/cz.config.cjs{ts}
126191
127-
- Using the TypeScript configuration file will **affects command line tool startup speed**.
128-
- Increase the package size.
129192
:::
193+
::: code-group-item ESM
194+
195+
<<< @/snippets/cz.config.mjs{ts}
130196
131-
:::tip
132-
Using the js configuration file to add the `@type` annotation can be a good way to provide code hints at configuration time.
133197
:::
198+
::::
199+
200+
:::::

‎docs/zh/config/index.md‎

Lines changed: 78 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ sitemap:
1616
- `commitlint.config.cjs`
1717
- `commitlint.config.mjs`
1818

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

2121
:::: code-group
2222
::: code-group-item CommonJS
@@ -77,7 +77,7 @@ sitemap:
7777

7878
## 中英文对照模板
7979

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

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

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

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

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

9797
:::
9898

9999
## Emoji 模板
100100

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

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

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

143143
## TypeScript 模板
144144

145-
:::danger
146-
从 <u>v1.3.0</u> 开始,将不再支持 TypeScript 配置文件。例如(`cz.config.ts`)
145+
- 从 `cz-git`, `czg` 的 `v1.11.0` 版本开始
146+
- 将使用 Node.js LTS 版本 >= `v22.11.0` 的==实验性功能==, **原生加载 TypeScript 配置文件**<br>==需注入开启实验性的参数== `NODE_OPTIONS='--experimental-transform-types --disable-warning ExperimentalWarning'` 使用
147+
- 详情: [可查看 Node.js 文档](https://nodejs.org/api/cli.html#--experimental-transform-types) | [Node.js TypeScript 支持路线图](https://github.com/nodejs/loaders/issues/217)
148+
- ==建议==: 可先使用 [ESM js](#javascript-%E6%A8%A1%E6%9D%BF) 配置文件作为未来 TypeScript 配置文件的过渡,待 Node.js 运行 TypeScript 稳定后,再进行切换
149+
150+
::::: details 点击展开 `NODE_OPTIONS` 注入方式
151+
:::: code-group
152+
::: code-group-item 项目 package.json 脚本注入 - 配合包 cross-env
153+
```diff
154+
"scripts": {
155+
- "cz": "czg"
156+
+ "cz": "cross-env NODE_OPTIONS='--experimental-transform-types --disable-warning ExperimentalWarning' czg"
157+
}
158+
```
159+
:::
160+
::: code-group-item 全局注入 - 使用 alias 命令
161+
```sh
162+
# .zshrc | .bashrc
163+
alias czg="NODE_OPTIONS='--experimental-transform-types --disable-warning ExperimentalWarning' \czg"
164+
```
147165
:::
166+
::: code-group-item 全局注入 - 使用 export 命令
167+
```sh
168+
# .zshrc | .bashrc
169+
export NODE_OPTIONS="--experimental-transform-types --disable-warning ExperimentalWarning"
170+
```
171+
:::
172+
::::
173+
:::::
174+
175+
---
176+
177+
- `.commitlintrc.ts`
178+
- `.commitlintrc.mts`
179+
- `.commitlintrc.cts`
180+
- `commitlint.config.ts`
181+
- `commitlint.config.mts`
182+
- `commitlint.config.cts`
183+
184+
::::: details 点击展开 `commitlint.config.ts` 完整 默认 配置模板
185+
186+
:::: code-group
187+
::: code-group-item CommonJS
188+
189+
<<< @/snippets/commitlint.config.cjs{ts}
148190

149-
:::tip
150-
使用 js 配置文件添加 `@type` 注释可以很好提供在配置时的代码提示.
151191
:::
192+
::: code-group-item ESM
193+
194+
<<< @/snippets/commitlint.config.mjs{ts}
195+
196+
:::
197+
::::
198+
199+
:::::
200+
201+
---
202+
203+
- `cz.config.ts`
204+
- `cz.config.mts`
205+
- `cz.config.cts`
206+
207+
::::: details 点击展开 `cz.config.ts` 完整 默认 配置模板
208+
:::: code-group
209+
::: code-group-item CommonJS
210+
211+
<<< @/snippets/cz.config.cjs{ts}
212+
213+
:::
214+
::: code-group-item ESM
215+
216+
<<< @/snippets/cz.config.mjs{ts}
217+
218+
:::
219+
::::
220+
221+
:::::

‎packages/@cz-git/plugin-loader/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"devDependencies": {
4444
"@commitlint/resolve-extends": "catalog:commitlint",
4545
"@commitlint/types": "catalog:commitlint",
46+
"@cz-git/inquirer": "workspace:*",
4647
"@types/tmp": "^0.2.3",
4748
"cosmiconfig": "catalog:",
4849
"pkg-dir": "5.0.0",
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import process from 'node:process'
2+
import { style } from '@cz-git/inquirer'
3+
import type { Loader } from 'cosmiconfig'
4+
5+
type LoaderError = Error & {
6+
code?: string
7+
}
8+
9+
// export NODE_OPTIONS="--experimental-transform-types --disable-warning ExperimentalWarning"
10+
export function esmTsLoader(): Loader {
11+
return async (cfgPath: string, _: string) => {
12+
try {
13+
const result = await import(cfgPath) as { default?: any }
14+
return result.default || result
15+
}
16+
catch (e: any) {
17+
// TODO: bun - The Current native bun will cause the TUI unnormal
18+
// @ts-ignore // Usage: `deno task cz`
19+
const isDeno = typeof Deno !== 'undefined' && Deno?.version?.deno
20+
if (isDeno)
21+
throw e
22+
23+
const error = e as LoaderError
24+
const isNodeLTSInRange = (() => {
25+
if (!process.version.startsWith('v'))
26+
return false
27+
const major = process.version.split('.')[0].slice(1)
28+
const minor = process.version.split('.')[1]
29+
return Number(major) >= 22 && Number(minor) >= 10
30+
})()
31+
if (error?.code === 'ERR_UNKNOWN_FILE_EXTENSION') {
32+
if (isNodeLTSInRange)
33+
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')}`))
34+
else
35+
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')}`))
36+
return {}
37+
}
38+
else {
39+
throw error
40+
}
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)