-
Notifications
You must be signed in to change notification settings - Fork 255
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(extract): splits transpiler availability from execution (#804)
- Loading branch information
Showing
6 changed files
with
261 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import path from "node:path/posix"; | ||
import { createRequire } from "node:module"; | ||
import { coerce, satisfies } from "semver"; | ||
|
||
const require = createRequire(import.meta.url); | ||
|
||
const PACKAGE_RE = "[^/]+"; | ||
const SCOPED_PACKAGE_RE = "@[^/]+(/[^/]+)"; | ||
const ROOT_MODULE_RE = new RegExp(`^(${SCOPED_PACKAGE_RE}|${PACKAGE_RE})`, "g"); | ||
|
||
function extractRootModuleName(pModuleName) { | ||
return (pModuleName.match(ROOT_MODULE_RE) || []).shift(); | ||
} | ||
|
||
function getVersion(pModuleName) { | ||
// of course we'd love to use something like an import with an import assertion | ||
// (yo, you're import-ing 'json'!), but that's _experimental_, printing scary | ||
// messages to stderr so: ¯\_(ツ)_/¯ | ||
// eslint-disable-next-line import/no-dynamic-require, security/detect-non-literal-require | ||
const lManifest = require(path.join( | ||
extractRootModuleName(pModuleName), | ||
"package.json" | ||
)); | ||
return lManifest.version; | ||
} | ||
|
||
export default function tryImportAvailable(pModuleName, pSemanticVersion) { | ||
try { | ||
if (pSemanticVersion) { | ||
const lVersion = getVersion(pModuleName); | ||
const lCoerced = coerce(lVersion); | ||
if ( | ||
lVersion && | ||
lCoerced && | ||
!satisfies(lCoerced.version, pSemanticVersion) | ||
) { | ||
return false; | ||
} | ||
} | ||
// of course we'd love to use something like import.meta.resolve, but | ||
// that's _experimental_, so ¯\_(ツ)_/¯ | ||
return Boolean(require.resolve(pModuleName)); | ||
} catch (pError) { | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.