Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(plugin-loader): loading ESM configure using URL format #219

Merged
merged 4 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/faq/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ sitemap:

# FAQ

## Error: require() of ES Module ... not supported
## Error: Cannot require() ES Module ... not supported
> [MODULE_TYPELESS_PACKAGE_JSON] Warning: Module type of xxxx is not specified and it doesn't parse as CommonJS.

1. If you are an ESM project (i.e., `"type": "module"` in package.json) or using `export default` syntax,
Expand Down
2 changes: 1 addition & 1 deletion docs/zh/faq/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ sitemap:
---
# 常见问题

## Error: require() of ES Module ... not supported
## Error: Cannot require() ES Module ... not supported
> [MODULE_TYPELESS_PACKAGE_JSON] Warning: Module type of xxxx is not specified and it doesn't parse as CommonJS.

1. 如果你是 ESM 项目 (即 package.json 中有 `"type": "module"`) 或使用 `export default` 语法
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineConfig } from 'cz-git'

export default defineConfig({
rules: {
'scope-enum': [2, 'always', ['cz-git']],
},
prompt: {
useEmoji: true,
},
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "test",
"private": true,
"author": "Zhengqbbb <[email protected]> (https://github.com/Zhengqbbb)",
"config": {
"commitizen": {
"path": "node_modules/cz-git"
}
}
}
9 changes: 9 additions & 0 deletions packages/@cz-git/plugin-loader/__tests__/loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,13 @@ describe('config loader', () => {
},
})
}, 1000)

it('default env load esm config', async () => {
mockDir = await useBootstrap('./fixtures/6-esm-config')
const config = await loaderSpyFn({ cwd: mockDir.name })
expect(config).toEqual({
rules: { 'scope-enum': [2, 'always', ['cz-git']] },
prompt: { path: 'node_modules/cz-git', useEmoji: true },
})
}, 1000)
})
21 changes: 20 additions & 1 deletion packages/@cz-git/plugin-loader/src/esm-ts-loader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import process from 'node:process'
import { resolve } from 'node:path'
import { style } from '@cz-git/inquirer'
import type { Loader } from 'cosmiconfig'

Expand All @@ -10,7 +11,7 @@ type LoaderError = Error & {
export function esmTsLoader(): Loader {
return async (cfgPath: string, _: string) => {
try {
const result = await import(cfgPath) as { default?: any }
const result = await import(fileToURL(cfgPath))
return result.default || result
}
catch (e: any) {
Expand Down Expand Up @@ -41,3 +42,21 @@ export function esmTsLoader(): Loader {
}
}
}

export function fileToURL(filePath: string) {
if (typeof filePath !== 'string')
throw new TypeError(`Expected a string, got ${typeof filePath}`)

let pathName = resolve(filePath)

pathName = pathName.replace(/\\/g, '/')

// Windows drive letter must be prefixed with a slash.
if (pathName[0] !== '/') {
pathName = `/${pathName}`
}

// Escape required characters for path components.
// See: https://tools.ietf.org/html/rfc3986#section-3.3
return encodeURI(`file://${pathName}`).replace(/[?#]/g, encodeURIComponent)
}