Skip to content

Commit

Permalink
fix(register): resolve .cjs/.cts file in esm package (#813)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Jul 13, 2024
1 parent 07a3b35 commit 154ee94
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/integrate-module/src/common.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports.common = "common";
1 change: 1 addition & 0 deletions packages/integrate-module/src/common.d.cts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare const common: string
5 changes: 5 additions & 0 deletions packages/integrate-module/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()}`)
Expand Down Expand Up @@ -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')
})
16 changes: 8 additions & 8 deletions packages/register/esm.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readFile } from 'fs/promises'
import { readFile } from 'node:fs/promises'
import {
createRequire,
type LoadFnOutput,
Expand All @@ -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'
Expand Down Expand Up @@ -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',
})
}

Expand Down

0 comments on commit 154ee94

Please sign in to comment.