Skip to content

Commit 34cd78a

Browse files
committed
native: improve argument input
1 parent 9e27b4d commit 34cd78a

File tree

2 files changed

+24
-27
lines changed

2 files changed

+24
-27
lines changed

native/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# cjs-module-lexer/native
2+
3+
This is a native build of cjx-module-lexer, it uses [oxc-resolver](https://github.com/oxc-project/oxc-resolver) to resolve module specifiers.
4+
5+
## Usage
6+
7+
```bash
8+
npm i react
9+
cjs-module-lexer react
10+
```

native/src/main.rs

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
use indexmap::IndexSet;
22
use lexer::CjsModuleLexer;
33
use oxc_resolver::{ResolveError, ResolveOptions, Resolver};
4-
use std::io::{self, BufRead, Write};
4+
use std::io::{stdout, Write};
55
use std::path::Path;
6-
use std::{fs, path};
6+
use std::{env, fs};
77

88
fn main() {
9-
let stdin = io::stdin();
10-
let mut stdout = io::stdout();
11-
let mut iterator = stdin.lock().lines();
12-
let wd = iterator.next().expect("missing wd argument").unwrap();
13-
let pkg_name = iterator.next().expect("missing pkg_name argument").unwrap();
14-
let specifier = iterator.next().expect("missing specifier argument").unwrap();
15-
let node_env = iterator.next().unwrap_or(Ok("production".to_owned())).unwrap();
16-
let js_filename = resolve(&wd, &pkg_name, &specifier, None).expect("failed to resolve specifier");
9+
let mut stdout = stdout();
10+
let specifier = env::args().skip(1).next().expect("missing specifier argument");
11+
let node_env = env::var("NODE_ENV").unwrap_or("production".to_owned());
12+
let wd = env::current_dir()
13+
.expect("failed to get current working directory")
14+
.to_str()
15+
.unwrap()
16+
.to_owned();
17+
let js_filename = resolve(&wd, &specifier, None).expect("failed to resolve specifier");
1718
let mut requires = vec![(js_filename, false)];
1819
let mut named_exports = IndexSet::new();
1920
while requires.len() > 0 {
@@ -25,7 +26,6 @@ fn main() {
2526
let reexport = reexports[0].clone();
2627
if !reexport.starts_with(".")
2728
&& !reexport.starts_with("/")
28-
&& !reexport.starts_with((pkg_name.to_owned() + "/").as_str())
2929
&& !reexport.ends_with("()")
3030
&& !is_node_builtin_module(&reexport)
3131
{
@@ -47,7 +47,7 @@ fn main() {
4747
};
4848
if !is_node_builtin_module(&reexport) {
4949
requires.push((
50-
resolve(&wd, &pkg_name, &reexport, Some(js_filename.clone())).expect("failed to resolve reexport"),
50+
resolve(&wd, &reexport, Some(js_filename.clone())).expect("failed to resolve reexport"),
5151
call_mode,
5252
));
5353
}
@@ -60,12 +60,7 @@ fn main() {
6060
}
6161
}
6262

63-
fn resolve(
64-
wd: &str,
65-
pkg_name: &str,
66-
specifier: &str,
67-
containing_filename: Option<String>,
68-
) -> Result<String, ResolveError> {
63+
fn resolve(wd: &str, specifier: &str, containing_filename: Option<String>) -> Result<String, ResolveError> {
6964
if specifier.starts_with("/") || specifier.starts_with("file://") {
7065
return Ok(specifier.to_owned());
7166
}
@@ -79,17 +74,9 @@ fn resolve(
7974
let ret = resolver.resolve(containing_dir, specifier)?;
8075
return Ok(ret.path().to_str().unwrap().to_owned());
8176
}
82-
if specifier.eq("..") || specifier.starts_with("../") {
77+
if specifier.starts_with(".") {
8378
return Err(ResolveError::NotFound(specifier.to_owned()));
8479
}
85-
if specifier.eq(".") {
86-
let ret = resolver.resolve(wd, pkg_name)?;
87-
return Ok(ret.path().to_str().unwrap().to_owned());
88-
}
89-
if specifier.starts_with("./") {
90-
let ret = resolver.resolve(wd, path::Path::new(pkg_name).join(specifier).to_str().unwrap())?;
91-
return Ok(ret.path().to_str().unwrap().to_owned());
92-
}
9380
let ret = resolver.resolve(wd, specifier)?;
9481
Ok(ret.path().to_str().unwrap().to_owned())
9582
}

0 commit comments

Comments
 (0)