oxc_resolver 11.19.1

ESM / CJS module resolution
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
//! package.json definitions (SIMD implementation for little-endian systems)
//!
//! Code related to export field are copied from [Parcel's resolver](https://github.com/parcel-bundler/parcel/blob/v2/packages/utils/node-resolver-rs/src/package_json.rs)

use std::{
    fmt,
    path::{Path, PathBuf},
};

use self_cell::MutBorrow;
use simd_json::{BorrowedValue, prelude::*};

use super::{ImportsExportsKind, PackageType, SideEffects};
use crate::{FileSystem, JSONError, ResolveError, path::PathUtil, replace_bom_with_whitespace};

// Use simd_json's Object type which handles the hasher correctly based on features
type BorrowedObject<'a> = simd_json::value::borrowed::Object<'a>;

self_cell::self_cell! {
    struct PackageJsonCell {
        owner: MutBorrow<Vec<u8>>,

        #[covariant]
        dependent: BorrowedValue,
    }
}

/// Serde implementation for the deserialized `package.json`.
///
/// This implementation is used by the [crate::Cache] and enabled through the
/// `fs_cache` feature.
pub struct PackageJson {
    /// Path to `package.json`. Contains the `package.json` filename.
    pub path: PathBuf,

    /// Realpath to `package.json`. Contains the `package.json` filename.
    pub realpath: PathBuf,

    cell: PackageJsonCell,
}

impl fmt::Debug for PackageJson {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("PackageJson")
            .field("path", &self.path)
            .field("realpath", &self.realpath)
            .field("name", &self.name())
            .field("type", &self.r#type())
            .finish_non_exhaustive()
    }
}

impl PackageJson {
    /// Returns the path where the `package.json` was found.
    ///
    /// Contains the `package.json` filename.
    ///
    /// This does not need to be the path where the file is stored on disk.
    /// See [Self::realpath()].
    #[must_use]
    pub fn path(&self) -> &Path {
        &self.path
    }

    /// Returns the path where the `package.json` file was stored on disk.
    ///
    /// Contains the `package.json` filename.
    ///
    /// This is the canonicalized version of [Self::path()], where all symbolic
    /// links are resolved.
    #[must_use]
    pub fn realpath(&self) -> &Path {
        &self.realpath
    }

    /// Directory to `package.json`.
    ///
    /// # Panics
    ///
    /// * When the `package.json` path is misconfigured.
    #[must_use]
    pub fn directory(&self) -> &Path {
        debug_assert!(self.realpath.file_name().is_some_and(|x| x == "package.json"));
        self.realpath.parent().unwrap()
    }

    /// Name of the package.
    ///
    /// The "name" field can be used together with the "exports" field to
    /// self-reference a package using its name.
    ///
    /// <https://nodejs.org/api/packages.html#name>
    #[must_use]
    pub fn name(&self) -> Option<&str> {
        self.cell
            .borrow_dependent()
            .as_object()
            .and_then(|obj| obj.get("name"))
            .and_then(|v| v.as_str())
    }

    /// Version of the package.
    ///
    /// <https://nodejs.org/api/packages.html#name>
    #[must_use]
    pub fn version(&self) -> Option<&str> {
        self.cell
            .borrow_dependent()
            .as_object()
            .and_then(|obj| obj.get("version"))
            .and_then(|v| v.as_str())
    }

    /// Returns the package type, if one is configured in the `package.json`.
    ///
    /// <https://nodejs.org/api/packages.html#type>
    #[must_use]
    pub fn r#type(&self) -> Option<PackageType> {
        self.cell
            .borrow_dependent()
            .as_object()
            .and_then(|obj| obj.get("type"))
            .and_then(|v| v.as_str())
            .and_then(PackageType::from_str)
    }

    /// The "sideEffects" field.
    ///
    /// <https://webpack.js.org/guides/tree-shaking>
    #[must_use]
    pub fn side_effects(&self) -> Option<SideEffects<'_>> {
        self.cell.borrow_dependent().as_object().and_then(|obj| obj.get("sideEffects")).and_then(
            |value| match value {
                BorrowedValue::Static(simd_json::StaticNode::Bool(b)) => {
                    Some(SideEffects::Bool(*b))
                }
                BorrowedValue::String(s) => Some(SideEffects::String(s.as_ref())),
                BorrowedValue::Array(arr) => {
                    let strings: Vec<&str> = arr.iter().filter_map(|v| v.as_str()).collect();
                    Some(SideEffects::Array(strings))
                }
                _ => None,
            },
        )
    }

    /// The "exports" field allows defining the entry points of a package.
    ///
    /// <https://nodejs.org/api/packages.html#exports>
    #[must_use]
    pub fn exports(&self) -> Option<ImportsExportsEntry<'_>> {
        self.cell
            .borrow_dependent()
            .as_object()
            .and_then(|obj| obj.get("exports"))
            .map(ImportsExportsEntry)
    }

    /// The "types" field in package.json.
    ///
    /// Used by TypeScript to find type declarations for a package.
    #[must_use]
    pub fn types(&self) -> Option<&str> {
        self.cell
            .borrow_dependent()
            .as_object()
            .and_then(|obj| obj.get("types"))
            .and_then(|v| v.as_str())
    }

    /// The "typings" field in package.json (legacy equivalent of "types").
    ///
    /// Used by TypeScript to find type declarations for a package.
    #[must_use]
    pub fn typings(&self) -> Option<&str> {
        self.cell
            .borrow_dependent()
            .as_object()
            .and_then(|obj| obj.get("typings"))
            .and_then(|v| v.as_str())
    }

    /// The "typesVersions" field in package.json.
    ///
    /// Returns the raw JSON value for the "typesVersions" field, which maps
    /// TypeScript version ranges to path redirect maps.
    ///
    /// <https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html#version-selection-with-typesversions>
    pub(crate) fn types_versions(&self) -> Option<ImportsExportsMap<'_>> {
        self.cell
            .borrow_dependent()
            .as_object()
            .and_then(|obj| obj.get("typesVersions"))
            .and_then(|v| v.as_object())
            .map(ImportsExportsMap)
    }

    /// The "main" field defines the entry point of a package when imported by
    /// name via a node_modules lookup. Its value should be a path.
    ///
    /// When a package has an "exports" field, this will take precedence over
    /// the "main" field when importing the package by name.
    ///
    /// Values are dynamically retrieved from [crate::ResolveOptions::main_fields].
    ///
    /// <https://nodejs.org/api/packages.html#main>
    pub(crate) fn main_fields<'a>(
        &'a self,
        main_fields: &'a [String],
    ) -> impl Iterator<Item = &'a str> + 'a {
        let json_value = self.cell.borrow_dependent();
        let json_object = json_value.as_object();

        main_fields
            .iter()
            .filter_map(move |main_field| json_object.and_then(|obj| obj.get(main_field.as_str())))
            .filter_map(|v| v.as_str())
    }

    /// The "exports" field allows defining the entry points of a package when
    /// imported by name loaded either via a node_modules lookup or a
    /// self-reference to its own name.
    ///
    /// <https://nodejs.org/api/packages.html#exports>
    pub(crate) fn exports_fields<'a>(
        &'a self,
        exports_fields: &'a [Vec<String>],
    ) -> impl Iterator<Item = ImportsExportsEntry<'a>> + 'a {
        let json_value = self.cell.borrow_dependent();

        exports_fields
            .iter()
            .filter_map(move |object_path| {
                json_value
                    .as_object()
                    .and_then(|json_object| Self::get_value_by_path(json_object, object_path))
            })
            .map(ImportsExportsEntry)
    }

    /// In addition to the "exports" field, there is a package "imports" field
    /// to create private mappings that only apply to import specifiers from
    /// within the package itself.
    ///
    /// <https://nodejs.org/api/packages.html#subpath-imports>
    pub(crate) fn imports_fields<'a>(
        &'a self,
        imports_fields: &'a [Vec<String>],
    ) -> impl Iterator<Item = ImportsExportsMap<'a>> + 'a {
        let json_value = self.cell.borrow_dependent();

        imports_fields
            .iter()
            .filter_map(move |object_path| {
                json_value
                    .as_object()
                    .and_then(|json_object| Self::get_value_by_path(json_object, object_path))
                    .and_then(|v| v.as_object())
            })
            .map(ImportsExportsMap)
    }

    /// Resolves the request string for this `package.json` by looking at the
    /// "browser" field.
    ///
    /// <https://github.com/defunctzombie/package-browser-field-spec>
    pub(crate) fn resolve_browser_field<'a>(
        &'a self,
        path: &Path,
        request: Option<&str>,
        alias_fields: &'a [Vec<String>],
    ) -> Result<Option<&'a str>, ResolveError> {
        for object in self.browser_fields(alias_fields) {
            if let Some(request) = request {
                // Find matching key in object
                if let Some(value) = object.get(request) {
                    return Self::alias_value(path, value);
                }
            } else {
                let dir = self.path.parent().unwrap();
                for (key, value) in object {
                    let joined = dir.normalize_with(key.as_ref());
                    if joined == path {
                        return Self::alias_value(path, value);
                    }
                }
            }
        }
        Ok(None)
    }

    /// Parse a package.json file from JSON bytes
    ///
    /// # Panics
    /// # Errors
    pub fn parse<Fs: FileSystem>(
        fs: &Fs,
        path: PathBuf,
        realpath: PathBuf,
        json: Vec<u8>,
    ) -> Result<Self, JSONError> {
        let mut json = json;
        replace_bom_with_whitespace(&mut json);

        // Check if empty after BOM stripping
        super::check_if_empty(&json, &path)?;

        // Create the self-cell with the JSON bytes and parsed BorrowedValue
        let cell = PackageJsonCell::try_new(MutBorrow::new(json), |bytes| {
            // Use MutBorrow to safely get mutable access for simd_json parsing
            simd_json::to_borrowed_value(bytes.borrow_mut())
        })
        .map_err(|simd_error| {
            // Fallback: re-read the file and parse with serde_json to get detailed error information
            // We re-read because simd_json may have mutated the buffer during its failed parse attempt
            // simd_json doesn't provide line/column info, so we use serde_json for better error messages
            let fallback_result = fs
                .read(&realpath)
                .map_err(|io_error| JSONError {
                    path: path.clone(),
                    message: format!("Failed to re-read file for error reporting: {io_error}"),
                    line: 0,
                    column: 0,
                })
                .and_then(|bytes| {
                    serde_json::from_slice::<serde_json::Value>(&bytes).map_err(|serde_error| {
                        JSONError {
                            path: path.clone(),
                            message: serde_error.to_string(),
                            line: serde_error.line(),
                            column: serde_error.column(),
                        }
                    })
                });

            match fallback_result {
                Ok(_) => {
                    // serde_json succeeded but simd_json failed - this shouldn't happen
                    // for valid JSON, but could indicate simd_json is more strict
                    JSONError {
                        path: path.clone(),
                        message: format!("simd_json parse error: {simd_error}"),
                        line: 0,
                        column: 0,
                    }
                }
                Err(error) => error,
            }
        })?;

        Ok(Self { path, realpath, cell })
    }

    fn get_value_by_path<'a>(
        fields: &'a BorrowedObject<'a>,
        path: &[String],
    ) -> Option<&'a BorrowedValue<'a>> {
        if path.is_empty() {
            return None;
        }
        let mut value = fields.get(path[0].as_str())?;

        for key in path.iter().skip(1) {
            if let Some(obj) = value.as_object() {
                value = obj.get(key.as_str())?;
            } else {
                return None;
            }
        }
        Some(value)
    }

    /// The "browser" field is provided by a module author as a hint to javascript bundlers or component tools when packaging modules for client side use.
    /// Multiple values are configured by [ResolveOptions::alias_fields].
    ///
    /// <https://github.com/defunctzombie/package-browser-field-spec>
    pub(crate) fn browser_fields<'a>(
        &'a self,
        alias_fields: &'a [Vec<String>],
    ) -> impl Iterator<Item = &'a BorrowedObject<'a>> + 'a {
        let json_value = self.cell.borrow_dependent();

        alias_fields.iter().filter_map(move |object_path| {
            json_value
                .as_object()
                .and_then(|json_object| Self::get_value_by_path(json_object, object_path))
                // Only object is valid, all other types are invalid
                // https://github.com/webpack/enhanced-resolve/blob/3a28f47788de794d9da4d1702a3a583d8422cd48/lib/AliasFieldPlugin.js#L44-L52
                .and_then(|value| value.as_object())
        })
    }

    pub(crate) fn alias_value<'a>(
        key: &Path,
        value: &'a BorrowedValue<'a>,
    ) -> Result<Option<&'a str>, ResolveError> {
        match value {
            BorrowedValue::String(s) => Ok(Some(s.as_ref())),
            BorrowedValue::Static(simd_json::StaticNode::Bool(false)) => {
                Err(ResolveError::Ignored(key.to_path_buf()))
            }
            _ => Ok(None),
        }
    }
}

#[derive(Clone)]
pub struct ImportsExportsEntry<'a>(pub(crate) &'a BorrowedValue<'a>);

impl<'a> ImportsExportsEntry<'a> {
    #[must_use]
    pub fn kind(&self) -> ImportsExportsKind {
        match self.0 {
            BorrowedValue::String(_) => ImportsExportsKind::String,
            BorrowedValue::Array(_) => ImportsExportsKind::Array,
            BorrowedValue::Object(_) => ImportsExportsKind::Map,
            BorrowedValue::Static(_) => ImportsExportsKind::Invalid,
        }
    }

    #[must_use]
    pub fn as_string(&self) -> Option<&'a str> {
        match self.0 {
            BorrowedValue::String(s) => Some(s.as_ref()),
            _ => None,
        }
    }

    #[must_use]
    pub fn as_array(&self) -> Option<ImportsExportsArray<'a>> {
        match self.0 {
            BorrowedValue::Array(arr) => Some(ImportsExportsArray(arr)),
            _ => None,
        }
    }

    #[must_use]
    pub fn as_map(&self) -> Option<ImportsExportsMap<'a>> {
        match self.0 {
            BorrowedValue::Object(obj) => Some(ImportsExportsMap(obj)),
            _ => None,
        }
    }
}

#[derive(Clone)]
pub struct ImportsExportsArray<'a>(&'a [BorrowedValue<'a>]);

impl<'a> ImportsExportsArray<'a> {
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    #[must_use]
    pub fn len(&self) -> usize {
        self.0.len()
    }

    pub fn iter(&self) -> impl Iterator<Item = ImportsExportsEntry<'a>> {
        ImportsExportsArrayIter { slice: self.0, index: 0 }
    }
}

struct ImportsExportsArrayIter<'a> {
    slice: &'a [BorrowedValue<'a>],
    index: usize,
}

impl<'a> Iterator for ImportsExportsArrayIter<'a> {
    type Item = ImportsExportsEntry<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        self.slice.get(self.index).map(|value| {
            self.index += 1;
            ImportsExportsEntry(value)
        })
    }
}

#[derive(Clone)]
pub struct ImportsExportsMap<'a>(pub(crate) &'a BorrowedObject<'a>);

impl<'a> ImportsExportsMap<'a> {
    pub fn get(&self, key: &str) -> Option<ImportsExportsEntry<'a>> {
        self.0.get(key).map(ImportsExportsEntry)
    }

    pub fn keys(&self) -> impl Iterator<Item = &'a str> {
        self.0.keys().map(std::convert::AsRef::as_ref)
    }

    pub fn iter(&self) -> impl Iterator<Item = (&'a str, ImportsExportsEntry<'a>)> {
        self.0.iter().map(|(k, v)| (k.as_ref(), ImportsExportsEntry(v)))
    }
}