Skip to content

Commit 50d7675

Browse files
authored
fix(plugin-loader): loading ESM configure using URL format (#219)
link #216 #217
1 parent e118e76 commit 50d7675

File tree

6 files changed

+51
-3
lines changed

6 files changed

+51
-3
lines changed

docs/faq/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ sitemap:
88

99
# FAQ
1010

11-
## Error: require() of ES Module ... not supported
11+
## Error: Cannot require() ES Module ... not supported
1212
> [MODULE_TYPELESS_PACKAGE_JSON] Warning: Module type of xxxx is not specified and it doesn't parse as CommonJS.
1313
1414
1. If you are an ESM project (i.e., `"type": "module"` in package.json) or using `export default` syntax,

docs/zh/faq/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ sitemap:
77
---
88
# 常见问题
99

10-
## Error: require() of ES Module ... not supported
10+
## Error: Cannot require() ES Module ... not supported
1111
> [MODULE_TYPELESS_PACKAGE_JSON] Warning: Module type of xxxx is not specified and it doesn't parse as CommonJS.
1212
1313
1. 如果你是 ESM 项目 (即 package.json 中有 `"type": "module"`) 或使用 `export default` 语法
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { defineConfig } from 'cz-git'
2+
3+
export default defineConfig({
4+
rules: {
5+
'scope-enum': [2, 'always', ['cz-git']],
6+
},
7+
prompt: {
8+
useEmoji: true,
9+
},
10+
})
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "test",
3+
"private": true,
4+
"author": "Zhengqbbb <[email protected]> (https://github.com/Zhengqbbb)",
5+
"config": {
6+
"commitizen": {
7+
"path": "node_modules/cz-git"
8+
}
9+
}
10+
}

packages/@cz-git/plugin-loader/__tests__/loader.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,13 @@ describe('config loader', () => {
110110
},
111111
})
112112
}, 1000)
113+
114+
it('default env load esm config', async () => {
115+
mockDir = await useBootstrap('./fixtures/6-esm-config')
116+
const config = await loaderSpyFn({ cwd: mockDir.name })
117+
expect(config).toEqual({
118+
rules: { 'scope-enum': [2, 'always', ['cz-git']] },
119+
prompt: { path: 'node_modules/cz-git', useEmoji: true },
120+
})
121+
}, 1000)
113122
})

packages/@cz-git/plugin-loader/src/esm-ts-loader.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import process from 'node:process'
2+
import { resolve } from 'node:path'
23
import { style } from '@cz-git/inquirer'
34
import type { Loader } from 'cosmiconfig'
45

@@ -10,7 +11,7 @@ type LoaderError = Error & {
1011
export function esmTsLoader(): Loader {
1112
return async (cfgPath: string, _: string) => {
1213
try {
13-
const result = await import(cfgPath) as { default?: any }
14+
const result = await import(fileToURL(cfgPath))
1415
return result.default || result
1516
}
1617
catch (e: any) {
@@ -41,3 +42,21 @@ export function esmTsLoader(): Loader {
4142
}
4243
}
4344
}
45+
46+
export function fileToURL(filePath: string) {
47+
if (typeof filePath !== 'string')
48+
throw new TypeError(`Expected a string, got ${typeof filePath}`)
49+
50+
let pathName = resolve(filePath)
51+
52+
pathName = pathName.replace(/\\/g, '/')
53+
54+
// Windows drive letter must be prefixed with a slash.
55+
if (pathName[0] !== '/') {
56+
pathName = `/${pathName}`
57+
}
58+
59+
// Escape required characters for path components.
60+
// See: https://tools.ietf.org/html/rfc3986#section-3.3
61+
return encodeURI(`file://${pathName}`).replace(/[?#]/g, encodeURIComponent)
62+
}

0 commit comments

Comments
 (0)