Skip to content

fix: resolve npm package with .js in the name #227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
43 changes: 43 additions & 0 deletions .github/workflows/release-napi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,49 @@ jobs:
path: |
napi/*.node
napi/*.wasm

build-freebsd:
runs-on: ubuntu-latest
name: Build FreeBSD
steps:
- uses: actions/checkout@v4
- name: Build
id: build
uses: cross-platform-actions/action@v0.25.0
env:
DEBUG: napi:*
RUSTUP_IO_THREADS: 1
with:
operating_system: freebsd
version: '14.0'
memory: 8G
cpu_count: 3
environment_variables: 'DEBUG RUSTUP_IO_THREADS'
shell: bash
run: |
sudo pkg install -y -f curl node libnghttp2 npm
curl https://sh.rustup.rs -sSf --output rustup.sh
sh rustup.sh -y --profile minimal --default-toolchain stable
source "$HOME/.cargo/env"
echo "~~~~ rustc --version ~~~~"
rustc --version
echo "~~~~ node -v ~~~~"
node -v
pwd
ls -lah
whoami
env
corepack enable
pnpm install
pnpm build --target x86_64-unknown-freebsd
rm -rf node_modules
rm -rf target
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: bindings-freebsd
path: napi/*.node
if-no-files-found: error

publish:
name: Publish NAPI
Expand Down
1 change: 1 addition & 0 deletions fixtures/pnpm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"private": true,
"devDependencies": {
"axios": "1.6.2",
"ipaddr.js": "2.2.0",
"styled-components": "6.1.1",
"postcss": "8.4.33"
}
Expand Down
1 change: 1 addition & 0 deletions npm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"aarch64-pc-windows-msvc",
"x86_64-unknown-linux-gnu",
"x86_64-unknown-linux-musl",
"x86_64-unknown-freebsd",
"aarch64-unknown-linux-gnu",
"aarch64-unknown-linux-musl",
"armv7-unknown-linux-gnueabihf",
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ pub struct ResolveContextImpl {
/// The current resolving alias for bailing recursion alias.
pub resolving_alias: Option<String>,

/// Resolve files in `node_modules` folder.
/// If the extension alias is enabled and all of the aliased extensions are not found if:
/// 1. in `node_modules`, we can fallback to the original extension;
/// 2. not in `node_modules`, it should not allow to fallback to the original extension or add new extensions.
pub resolve_in_node_modules: bool,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 Let me take a look at the problem first 😅


/// For avoiding infinite recursion, which will cause stack overflow.
depth: u8,
}
Expand Down Expand Up @@ -78,6 +84,10 @@ impl ResolveContext {
self.resolving_alias = Some(alias);
}

pub fn with_resolve_in_node_modules(&mut self, yes: bool) {
self.resolve_in_node_modules = yes;
}

pub fn test_for_infinite_recursion(&mut self) -> Result<(), ResolveError> {
self.depth += 1;
// 64 should be more than enough for detecting infinite recursion.
Expand Down
14 changes: 12 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,8 +498,16 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {

fn load_as_file(&self, cached_path: &CachedPath, ctx: &mut Ctx) -> ResolveResult {
// enhanced-resolve feature: extension_alias
if let Some(path) = self.load_extension_alias(cached_path, ctx)? {
return Ok(Some(path));
match self.load_extension_alias(cached_path, ctx) {
Ok(Some(path)) => {
return Ok(Some(path));
}
Err(err) => {
if !ctx.resolve_in_node_modules {
return Err(err);
}
}
_ => {}
}
if self.options.enforce_extension.is_disabled() {
// 1. If X is a file, load X as its file extension format. STOP
Expand Down Expand Up @@ -728,7 +736,9 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
// c. LOAD_AS_DIRECTORY(DIR/X)
let node_module_file = cached_path.path().normalize_with(specifier);
let cached_path = self.cache.value(&node_module_file);
ctx.with_resolve_in_node_modules(true);
if let Some(path) = self.load_as_file_or_directory(&cached_path, specifier, ctx)? {
ctx.with_resolve_in_node_modules(false);
return Ok(Some(path));
}
}
Expand Down
15 changes: 15 additions & 0 deletions tests/resolve_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,18 @@ fn postcss() {
let resolution = resolver.resolve(&module_path, "./lib/terminal-highlight");
assert_eq!(resolution, Err(ResolveError::Ignored(module_path.join("lib/terminal-highlight"))));
}

#[test]
fn resolve_package_name_with_dot_js() {
let dir = dir();
let path = dir.join("fixtures/pnpm");
let module_path =
dir.join("node_modules/.pnpm/ipaddr.js@2.2.0/node_modules/ipaddr.js/lib/ipaddr.js");
let resolver = Resolver::new(ResolveOptions {
extension_alias: vec![(".js".into(), vec![".js".into(), ".ts".into(), ".tsx".into()])],
..ResolveOptions::default()
});
let resolution: Result<oxc_resolver::Resolution, ResolveError> =
resolver.resolve(path, "ipaddr.js");
assert_eq!(resolution.map(oxc_resolver::Resolution::into_path_buf), Ok(module_path));
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy