Skip to content

Commit

Permalink
Fix circular dependency in lib/get-format.js
Browse files Browse the repository at this point in the history
Closes GH-13.

Reviewed-by: Titus Wormer <tituswormer@gmail.com>
  • Loading branch information
brawaru authored Mar 11, 2023
1 parent cf9de89 commit db5db1d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/get-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import path from 'node:path'
import {URL, fileURLToPath} from 'node:url'
import {getPackageType} from './resolve.js'
import {getPackageType} from './resolve-get-package-type.js'
import {codes} from './errors.js'

const {ERR_UNKNOWN_FILE_EXTENSION} = codes
Expand Down
23 changes: 23 additions & 0 deletions lib/resolve-get-package-type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Manually “tree shaken” from:
// <https://github.com/nodejs/node/blob/6668c4d/lib/internal/modules/esm/resolve.js>
// Last checked on: Jan 6, 2023.
//
// This file solves a circular dependency.
// In Node.js, `getPackageType` is in `resolve.js`.
// `resolve.js` imports `get-format.js`, which needs `getPackageType`.
// We split that up so that bundlers don’t fail.

/**
* @typedef {import('./package-config.js').PackageType} PackageType
*/

import {getPackageScopeConfig} from './package-config.js'

/**
* @param {URL} url
* @returns {PackageType}
*/
export function getPackageType(url) {
const packageConfig = getPackageScopeConfig(url)
return packageConfig.type
}
12 changes: 3 additions & 9 deletions lib/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
/**
* @typedef {import('./errors.js').ErrnoException} ErrnoException
* @typedef {import('./package-config.js').PackageConfig} PackageConfig
* @typedef {import('./package-config.js').PackageType} PackageType
*/

import assert from 'node:assert'
Expand Down Expand Up @@ -883,14 +882,9 @@ function packageImportsResolve(name, base, conditions) {
throw importNotDefined(name, packageJsonUrl, base)
}

/**
* @param {URL} url
* @returns {PackageType}
*/
export function getPackageType(url) {
const packageConfig = getPackageScopeConfig(url)
return packageConfig.type
}
// Note: In Node.js, `getPackageType` is here.
// To prevent a circular dependency, we move it to
// `resolve-get-package-type.js`.

/**
* @param {string} specifier
Expand Down

0 comments on commit db5db1d

Please sign in to comment.