From 8378dd4d277f1dc644c16d9f385beb7255b1ef5b Mon Sep 17 00:00:00 2001 From: Zhengqbbb <1074059947@qq.com> Date: Fri, 28 Feb 2025 21:22:41 +0800 Subject: [PATCH 1/4] fix(plugin-loader): loading ESM configure using URL format link #216 --- docs/faq/index.md | 2 +- docs/zh/faq/index.md | 2 +- .../fixtures/6-esm-config/commitlint.config.mjs | 10 ++++++++++ .../__tests__/fixtures/6-esm-config/package.json | 10 ++++++++++ .../@cz-git/plugin-loader/__tests__/loader.test.ts | 9 +++++++++ packages/@cz-git/plugin-loader/src/esm-ts-loader.ts | 4 +++- 6 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 packages/@cz-git/plugin-loader/__tests__/fixtures/6-esm-config/commitlint.config.mjs create mode 100644 packages/@cz-git/plugin-loader/__tests__/fixtures/6-esm-config/package.json diff --git a/docs/faq/index.md b/docs/faq/index.md index 6a74458d4..7ed59fbcc 100644 --- a/docs/faq/index.md +++ b/docs/faq/index.md @@ -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, diff --git a/docs/zh/faq/index.md b/docs/zh/faq/index.md index b568cc38e..584540913 100644 --- a/docs/zh/faq/index.md +++ b/docs/zh/faq/index.md @@ -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` 语法 diff --git a/packages/@cz-git/plugin-loader/__tests__/fixtures/6-esm-config/commitlint.config.mjs b/packages/@cz-git/plugin-loader/__tests__/fixtures/6-esm-config/commitlint.config.mjs new file mode 100644 index 000000000..f430786c6 --- /dev/null +++ b/packages/@cz-git/plugin-loader/__tests__/fixtures/6-esm-config/commitlint.config.mjs @@ -0,0 +1,10 @@ +import { defineConfig } from 'cz-git' + +export default defineConfig({ + rules: { + 'scope-enum': [2, 'always', ['cz-git']], + }, + prompt: { + useEmoji: true, + }, +}) diff --git a/packages/@cz-git/plugin-loader/__tests__/fixtures/6-esm-config/package.json b/packages/@cz-git/plugin-loader/__tests__/fixtures/6-esm-config/package.json new file mode 100644 index 000000000..1d6660dc7 --- /dev/null +++ b/packages/@cz-git/plugin-loader/__tests__/fixtures/6-esm-config/package.json @@ -0,0 +1,10 @@ +{ + "name": "test", + "private": true, + "author": "Zhengqbbb (https://github.com/Zhengqbbb)", + "config": { + "commitizen": { + "path": "node_modules/cz-git" + } + } +} diff --git a/packages/@cz-git/plugin-loader/__tests__/loader.test.ts b/packages/@cz-git/plugin-loader/__tests__/loader.test.ts index 64abba9f5..6a888beca 100644 --- a/packages/@cz-git/plugin-loader/__tests__/loader.test.ts +++ b/packages/@cz-git/plugin-loader/__tests__/loader.test.ts @@ -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) }) diff --git a/packages/@cz-git/plugin-loader/src/esm-ts-loader.ts b/packages/@cz-git/plugin-loader/src/esm-ts-loader.ts index 90f1a5448..0b7c76ca3 100644 --- a/packages/@cz-git/plugin-loader/src/esm-ts-loader.ts +++ b/packages/@cz-git/plugin-loader/src/esm-ts-loader.ts @@ -1,4 +1,5 @@ import process from 'node:process' +import { pathToFileURL } from 'node:url' import { style } from '@cz-git/inquirer' import type { Loader } from 'cosmiconfig' @@ -10,7 +11,8 @@ type LoaderError = Error & { export function esmTsLoader(): Loader { return async (cfgPath: string, _: string) => { try { - const result = await import(cfgPath) as { default?: any } + const fileUrl = pathToFileURL(cfgPath).href + const result = await import(fileUrl) return result.default || result } catch (e: any) { From 9af21d0cabd9dbe83a9023cdae7946147f13678f Mon Sep 17 00:00:00 2001 From: Zhengqbbb <1074059947@qq.com> Date: Fri, 28 Feb 2025 21:48:46 +0800 Subject: [PATCH 2/4] ci(plugin-loader): fix path error --- packages/@cz-git/plugin-loader/src/esm-ts-loader.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/@cz-git/plugin-loader/src/esm-ts-loader.ts b/packages/@cz-git/plugin-loader/src/esm-ts-loader.ts index 0b7c76ca3..c32ed0014 100644 --- a/packages/@cz-git/plugin-loader/src/esm-ts-loader.ts +++ b/packages/@cz-git/plugin-loader/src/esm-ts-loader.ts @@ -1,5 +1,6 @@ import process from 'node:process' import { pathToFileURL } from 'node:url' +import { resolve } from 'node:path' import { style } from '@cz-git/inquirer' import type { Loader } from 'cosmiconfig' @@ -11,7 +12,9 @@ type LoaderError = Error & { export function esmTsLoader(): Loader { return async (cfgPath: string, _: string) => { try { - const fileUrl = pathToFileURL(cfgPath).href + console.log(111, cfgPath) + const fileUrl = pathToFileURL(resolve(cfgPath)).href + console.log(222, fileUrl) const result = await import(fileUrl) return result.default || result } From 7817f0a2506f375d6695cfa491b4468f2c4da1fa Mon Sep 17 00:00:00 2001 From: Zhengqbbb <1074059947@qq.com> Date: Fri, 28 Feb 2025 22:07:15 +0800 Subject: [PATCH 3/4] ci(plugin-loader): fix path error --- packages/@cz-git/plugin-loader/src/esm-ts-loader.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/@cz-git/plugin-loader/src/esm-ts-loader.ts b/packages/@cz-git/plugin-loader/src/esm-ts-loader.ts index c32ed0014..85d0a8caf 100644 --- a/packages/@cz-git/plugin-loader/src/esm-ts-loader.ts +++ b/packages/@cz-git/plugin-loader/src/esm-ts-loader.ts @@ -1,6 +1,7 @@ import process from 'node:process' import { pathToFileURL } from 'node:url' import { resolve } from 'node:path' +import fs from 'node:fs' import { style } from '@cz-git/inquirer' import type { Loader } from 'cosmiconfig' @@ -13,8 +14,11 @@ export function esmTsLoader(): Loader { return async (cfgPath: string, _: string) => { try { console.log(111, cfgPath) - const fileUrl = pathToFileURL(resolve(cfgPath)).href - console.log(222, fileUrl) + const filePath = resolve(cfgPath) + console.log('exists ?', fs.existsSync(filePath)) + console.log(222, filePath) + const fileUrl = pathToFileURL(filePath).href + console.log(333, fileUrl) const result = await import(fileUrl) return result.default || result } From 5594ee7a553db93862d26aaf4ee4459a7be830ed Mon Sep 17 00:00:00 2001 From: Zhengqbbb <1074059947@qq.com> Date: Fri, 28 Feb 2025 22:30:51 +0800 Subject: [PATCH 4/4] ci(plugin-loader): fix path error --- .../plugin-loader/src/esm-ts-loader.ts | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/packages/@cz-git/plugin-loader/src/esm-ts-loader.ts b/packages/@cz-git/plugin-loader/src/esm-ts-loader.ts index 85d0a8caf..b0113ecf4 100644 --- a/packages/@cz-git/plugin-loader/src/esm-ts-loader.ts +++ b/packages/@cz-git/plugin-loader/src/esm-ts-loader.ts @@ -1,7 +1,5 @@ import process from 'node:process' -import { pathToFileURL } from 'node:url' import { resolve } from 'node:path' -import fs from 'node:fs' import { style } from '@cz-git/inquirer' import type { Loader } from 'cosmiconfig' @@ -13,13 +11,7 @@ type LoaderError = Error & { export function esmTsLoader(): Loader { return async (cfgPath: string, _: string) => { try { - console.log(111, cfgPath) - const filePath = resolve(cfgPath) - console.log('exists ?', fs.existsSync(filePath)) - console.log(222, filePath) - const fileUrl = pathToFileURL(filePath).href - console.log(333, fileUrl) - const result = await import(fileUrl) + const result = await import(fileToURL(cfgPath)) return result.default || result } catch (e: any) { @@ -50,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) +}