esm-resolve
Advanced tools
Comparing version 1.0.9 to 1.0.10
// index.ts | ||
import * as path2 from "path"; | ||
import * as fs2 from "fs"; | ||
import * as path2 from "node:path"; | ||
import * as fs2 from "node:fs"; | ||
// lib/helper.ts | ||
import * as fs from "fs"; | ||
import * as fs from "node:fs"; | ||
var statOrNull = (p) => { | ||
@@ -18,6 +18,6 @@ try { | ||
// index.ts | ||
import { createRequire } from "module"; | ||
import { createRequire } from "node:module"; | ||
// lib/node.ts | ||
import * as path from "path"; | ||
import * as path from "node:path"; | ||
var alwaysConstraints = ["module", "import"]; | ||
@@ -78,2 +78,4 @@ function matchModuleNodePath(exports, rest) { | ||
var defaults = { | ||
isDir: false, | ||
resolveToAbsolute: false, | ||
constraints: ["browser"], | ||
@@ -84,2 +86,3 @@ allowMissing: false, | ||
matchNakedMjs: false, | ||
allowImportingExtraExtensions: false, | ||
includeMainFallback: true, | ||
@@ -100,3 +103,4 @@ checkNestedPackages: true | ||
this.constraints = [this.options.constraints].flat(); | ||
const importerDir = path2.join(path2.resolve(importer), "..", path2.sep); | ||
importer = path2.resolve(importer); | ||
const importerDir = this.options.isDir ? path2.join(importer, "/") : path2.join(importer, "..", path2.sep); | ||
this.importerDir = new URL(`file://${importerDir}`); | ||
@@ -152,3 +156,17 @@ this.require = createRequire(importerDir); | ||
} | ||
const extToCheck = this.options.matchNakedMjs ? [".js", ".mjs"] : [".js"]; | ||
const extToCheck = [".js"]; | ||
if (this.options.matchNakedMjs) { | ||
extToCheck.push(".mjs"); | ||
} | ||
if (this.options.allowImportingExtraExtensions) { | ||
if (Array.isArray(this.options.allowImportingExtraExtensions)) { | ||
extToCheck.push( | ||
...this.options.allowImportingExtraExtensions.map((x) => { | ||
return x.startsWith(".") ? x : "." + x; | ||
}) | ||
); | ||
} else { | ||
extToCheck.push(".ts", ".tsx", ".jsx"); | ||
} | ||
} | ||
if (stat === null) { | ||
@@ -169,2 +187,10 @@ for (const ext of extToCheck) { | ||
} | ||
const { name, dir } = path2.parse(pathname); | ||
const naked = path2.join(dir, name); | ||
for (const ext of extToCheck) { | ||
const check = naked + ext; | ||
if (statIsFile(check)) { | ||
return check; | ||
} | ||
} | ||
} else if (stat.isDirectory()) { | ||
@@ -266,2 +292,5 @@ for (const ext of extToCheck) { | ||
} | ||
if (this.options.resolveToAbsolute) { | ||
return pathname; | ||
} | ||
let out = path2.relative(this.importerDir.pathname, pathname); | ||
@@ -268,0 +297,0 @@ if (!relativeRegexp.test(out)) { |
{ | ||
"name": "esm-resolve", | ||
"version": "1.0.9", | ||
"version": "1.0.10", | ||
"description": "Resolves ESM imports in Node", | ||
@@ -10,3 +10,4 @@ "main": "bundle.js", | ||
"import": "./bundle.js", | ||
"require": "./bundle.cjs" | ||
"require": "./bundle.cjs", | ||
"types": "./index.d.ts" | ||
} | ||
@@ -20,7 +21,7 @@ } | ||
"devDependencies": { | ||
"@types/node": "^20.11.28", | ||
"esbuild": "^0.14.1", | ||
"@types/node": "^20.12.7", | ||
"esbuild": "^0.14.54", | ||
"tsx": "^4.7.1" | ||
}, | ||
"types": "index.d.ts", | ||
"types": "./index.d.ts", | ||
"scripts": { | ||
@@ -27,0 +28,0 @@ "test": "npx tsx test/resolve.ts" |
[![Tests](https://github.com/samthor/esm-resolve/workflows/Tests/badge.svg)](https://github.com/samthor/esm-resolve/actions) | ||
An ESM import resolver for Node written in pure JS. | ||
This is written to be part of an [ESM dev server](https://github.com/samthor/dhost) or build process, as Node's import process is impossible to introspect. | ||
It also allows some cases which would otherwise be failures. | ||
Sync ESM import resolver for Node written in pure JS. | ||
This is written to be part of an [ESM dev server](https://github.com/samthor/dhost) or build process. | ||
It is permissive by default, allowing some cases which would normally be failures. | ||
⚠️ This resolver was writtem before [`import.meta.resolve()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import.meta/resolve) was widely available—it may work for you without adding _yet another_ dependency. | ||
However, "esm-resolve" is a bit more permissive. | ||
## Usage | ||
@@ -16,6 +19,6 @@ | ||
const r = buildResolver('./path/to/js/file.js'); | ||
const r = buildResolver('./lib/file.js'); | ||
r('./relative'); // './relative.js' | ||
r('foo-test-package-name'); // './node_modules/foo-test-package-name/index.js' | ||
r('foo-test-package-name'); // '../node_modules/foo-test-package-name/index.js' | ||
``` | ||
@@ -25,5 +28,8 @@ | ||
The resolved path is returned relative _to the importer_ of that file, not your process' current directory. | ||
You can set the `resolveToAbsolute` option if you'd always like an absolute path. | ||
## Notes | ||
This implements modern Node resolution, i.e., [subpath exports](https://nodejs.org/api/packages.html#packages_subpath_exports) and [conditional exports](https://nodejs.org/api/packages.html#packages_conditional_exports). | ||
This implements modern Node resolution, i.e., [subpath exports](https://nodejs.org/api/packages.html#packages_subpath_exports), [subpath imports](https://nodejs.org/api/packages.html#subpath-imports) and [conditional exports](https://nodejs.org/api/packages.html#packages_conditional_exports). | ||
By default, it will rewrite to the "browser", "import" or "default" keys (not "node", as it's expected that you'll use this for browser builds). | ||
@@ -38,3 +44,3 @@ | ||
// Resolves for Node, and allows .mjs files. | ||
const r = buildResolver('./path/to/js/file.js', { | ||
const r = buildResolver('./lib/file.js', { | ||
constraints: 'node', | ||
@@ -48,3 +54,3 @@ matchNakedMjs: true, | ||
// Or if we're importing package with a node constraint: | ||
r('node-only'); // './node-modules/node-only/build-for-node.js' | ||
r('node-only'); // '../node-modules/node-only/build-for-node.js' | ||
``` |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
53
4
0
33838
6
647