Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ See [index.d.ts](https://github.com/oxc-project/oxc-resolver/blob/main/napi/inde
| aliasFields | [] | A list of alias fields in description files |
| extensionAlias | {} | An object which maps extension to extension aliases |
| conditionNames | [] | A list of exports field condition names |
| descriptionFiles | ["package.json"] | A list of description files to read from |
| enforceExtension | false | Enforce that a extension from extensions must be used |
| exportsFields | ["exports"] | A list of exports fields in description files |
| extensions | [".js", ".json", ".node"] | A list of extensions which should be tried for files |
Expand All @@ -190,6 +189,7 @@ See [index.d.ts](https://github.com/oxc-project/oxc-resolver/blob/main/napi/inde

| Field | Default | Description |
| ---------------- | --------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
| descriptionFiles | ["package.json"] | A list of description files to read from |
| modules | ["node_modules"] | A list of directories to resolve modules from, can be absolute path or folder name |
| cachePredicate | function() { return true }; | A function which decides whether a request should be cached or not. An object is passed to the function with `path` and `request` properties. |
| cacheWithContext | true | If unsafe cache is enabled, includes `request.context` in the cache key |
Expand Down
1 change: 0 additions & 1 deletion napi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ impl ResolverFactory {
.map(|o| o.into_iter().map(|x| StrOrStrList(x).into()).collect::<Vec<_>>())
.unwrap_or(default.alias_fields),
condition_names: op.condition_names.unwrap_or(default.condition_names),
description_files: op.description_files.unwrap_or(default.description_files),
enforce_extension: op
.enforce_extension
.map(|enforce_extension| enforce_extension.into())
Expand Down
5 changes: 0 additions & 5 deletions napi/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ pub struct NapiResolveOptions {
/// Default `[]`
pub condition_names: Option<Vec<String>>,

/// The JSON files to use for descriptions. (There was once a `bower.json`.)
///
/// Default `["package.json"]`
pub description_files: Option<Vec<String>>,

/// If true, it will not allow extension-less files.
/// So by default `require('./foo')` works if `./foo` has a `.js` extension,
/// but with this enabled only `require('./foo.js')` will work.
Expand Down
39 changes: 17 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,30 +590,26 @@ impl<C: Cache> ResolverGeneric<C> {
}

fn load_as_directory(&self, cached_path: &C::Cp, ctx: &mut Ctx) -> ResolveResult<C::Cp> {
// TODO: Only package.json is supported, so warn about having other values
// Checking for empty files is needed for omitting checks on package.json
// 1. If X/package.json is a file,
if !self.options.description_files.is_empty() {
// a. Parse X/package.json, and look for "main" field.
if let Some((_, package_json)) =
self.cache.get_package_json(cached_path, &self.options, ctx)?
{
// b. If "main" is a falsy value, GOTO 2.
for main_field in package_json.main_fields(&self.options.main_fields) {
// c. let M = X + (json main field)
let cached_path = cached_path.normalize_with(main_field, self.cache.as_ref());
// d. LOAD_AS_FILE(M)
if let Some(path) = self.load_as_file(&cached_path, ctx)? {
return Ok(Some(path));
}
// e. LOAD_INDEX(M)
if let Some(path) = self.load_index(&cached_path, ctx)? {
return Ok(Some(path));
}
// a. Parse X/package.json, and look for "main" field.
if let Some((_, package_json)) =
self.cache.get_package_json(cached_path, &self.options, ctx)?
{
// b. If "main" is a falsy value, GOTO 2.
for main_field in package_json.main_fields(&self.options.main_fields) {
// c. let M = X + (json main field)
let cached_path = cached_path.normalize_with(main_field, self.cache.as_ref());
// d. LOAD_AS_FILE(M)
if let Some(path) = self.load_as_file(&cached_path, ctx)? {
return Ok(Some(path));
}
// e. LOAD_INDEX(M)
if let Some(path) = self.load_index(&cached_path, ctx)? {
return Ok(Some(path));
}
// f. LOAD_INDEX(X) DEPRECATED
// g. THROW "not found"
}
// f. LOAD_INDEX(X) DEPRECATED
// g. THROW "not found"
}
// 2. LOAD_INDEX(X)
self.load_index(cached_path, ctx)
Expand Down Expand Up @@ -1286,7 +1282,6 @@ impl<C: Cache> ResolverGeneric<C> {
Some(b'.') => Ok(tsconfig.directory().normalize_with(specifier)),
_ => self
.clone_with_options(ResolveOptions {
description_files: vec![],
extensions: vec![".json".into()],
main_files: vec!["tsconfig.json".into()],
..ResolveOptions::default()
Expand Down
7 changes: 0 additions & 7 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ pub struct ResolveOptions {
/// Default `[]`
pub condition_names: Vec<String>,

/// The JSON files to use for descriptions. (There was once a `bower.json`.)
///
/// Default `["package.json"]`
pub description_files: Vec<String>,

/// Set to [EnforceExtension::Enabled] for [ESM Mandatory file extensions](https://nodejs.org/api/esm.html#mandatory-file-extensions).
///
/// If `enforce_extension` is set to [EnforceExtension::Enabled], resolution will not allow extension-less files.
Expand Down Expand Up @@ -443,7 +438,6 @@ impl Default for ResolveOptions {
alias: vec![],
alias_fields: vec![],
condition_names: vec![],
description_files: vec!["package.json".into()],
enforce_extension: EnforceExtension::Auto,
extension_alias: vec![],
exports_fields: vec![vec!["exports".into()]],
Expand Down Expand Up @@ -590,7 +584,6 @@ mod test {
alias_fields: vec![],
builtin_modules: false,
condition_names: vec![],
description_files: vec![],
enforce_extension: EnforceExtension::Disabled,
exports_fields: vec![],
extension_alias: vec![],
Expand Down
20 changes: 1 addition & 19 deletions src/tests/incorrect_description_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use rustc_hash::FxHashSet;

use crate::{JSONError, Resolution, ResolveContext, ResolveError, ResolveOptions, Resolver};
use crate::{JSONError, ResolveContext, ResolveError, Resolver};

// should not resolve main in incorrect description file #1
#[test]
Expand Down Expand Up @@ -45,21 +45,3 @@ fn incorrect_description_file_3() {
let resolution = Resolver::default().resolve(f.join("pack2"), ".");
assert!(resolution.is_err());
}

// `enhanced_resolve` does not have this test case
#[test]
fn no_description_file() {
let f = super::fixture_root().join("enhanced_resolve");

// has description file
let resolver = Resolver::default();
assert_eq!(
resolver.resolve(&f, ".").map(Resolution::into_path_buf),
Ok(f.join("lib/index.js"))
);

// without description file
let resolver =
Resolver::new(ResolveOptions { description_files: vec![], ..ResolveOptions::default() });
assert_eq!(resolver.resolve(&f, "."), Err(ResolveError::NotFound(".".into())));
}
Loading