From 269e305a093380ea92f15724c0b67a903098e027 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Sat, 13 Jul 2024 14:30:04 +0800 Subject: [PATCH] fix(register): resolve .cjs/.cts file in esm package --- packages/integrate-module/src/common.cjs | 1 + packages/integrate-module/src/common.d.cts | 1 + packages/integrate-module/src/index.ts | 5 +++++ packages/register/esm.mts | 16 ++++++++-------- 4 files changed, 15 insertions(+), 8 deletions(-) create mode 100644 packages/integrate-module/src/common.cjs create mode 100644 packages/integrate-module/src/common.d.cts diff --git a/packages/integrate-module/src/common.cjs b/packages/integrate-module/src/common.cjs new file mode 100644 index 000000000..7a20f9cb9 --- /dev/null +++ b/packages/integrate-module/src/common.cjs @@ -0,0 +1 @@ +module.exports.common = "common"; \ No newline at end of file diff --git a/packages/integrate-module/src/common.d.cts b/packages/integrate-module/src/common.d.cts new file mode 100644 index 000000000..6e9b876ce --- /dev/null +++ b/packages/integrate-module/src/common.d.cts @@ -0,0 +1 @@ +export declare const common: string diff --git a/packages/integrate-module/src/index.ts b/packages/integrate-module/src/index.ts index 369a84f89..ce2c4007e 100644 --- a/packages/integrate-module/src/index.ts +++ b/packages/integrate-module/src/index.ts @@ -14,6 +14,7 @@ import { foo } from './foo.mjs' import { bar } from './subdirectory/bar.mjs' import { baz } from './subdirectory/index.mjs' import { Component } from './component.js' +import { common } from './common.cjs' import './js-module.mjs' const { foo: fooWithQuery } = await import(`./foo.mjs?q=${Date.now()}`) @@ -62,3 +63,7 @@ await test('resolve simple-git', () => { await test('resolve local cjs module', () => { assert.equal(cjs.default(), 'default.default') }) + +await test('resolve commonjs module', () => { + assert.equal(common, 'common') +}) diff --git a/packages/register/esm.mts b/packages/register/esm.mts index 21d2b8bee..1802122a1 100644 --- a/packages/register/esm.mts +++ b/packages/register/esm.mts @@ -1,4 +1,4 @@ -import { readFile } from 'fs/promises' +import { readFile } from 'node:fs/promises' import { createRequire, type LoadFnOutput, @@ -7,8 +7,8 @@ import { type ResolveHook, builtinModules, } from 'node:module' -import { extname, join } from 'path' -import { fileURLToPath, parse as parseUrl, pathToFileURL } from 'url' +import { extname, join } from 'node:path' +import { fileURLToPath, parse as parseUrl, pathToFileURL } from 'node:url' import debugFactory from 'debug' import { EnforceExtension, ResolverFactory } from 'oxc-resolver' @@ -225,11 +225,11 @@ export const resolve: ResolveHook = async (specifier, context, nextResolve) => { ...context, url: url.href, format: - moduleType === 'module' - ? 'module' - : path.endsWith('cjs') || path.endsWith('cts') || moduleType === 'commonjs' || !moduleType - ? 'commonjs' - : 'module', + path.endsWith('cjs') || path.endsWith('cts') || moduleType === 'commonjs' || !moduleType + ? 'commonjs' + : moduleType === 'module' + ? 'module' + : 'commonjs', }) }