-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plugin-loader): support esm and ts configure loader
link #192
- Loading branch information
Showing
5 changed files
with
85 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.