Skip to content

Commit fbf7bc0

Browse files
committed
Get rid of console_error_panic_hook
1 parent c6cd2cf commit fbf7bc0

File tree

5 files changed

+23
-56
lines changed

5 files changed

+23
-56
lines changed

Cargo.lock

Lines changed: 0 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
[package]
22
name = "esm-cjs-lexer"
33
version = "0.10.1"
4-
description = "A WASM module to parse the `module.exports` of a commonjs module for ESM converting."
5-
repository = "https://github.com/esm-dev/esm.sh"
4+
description = "A WASM module to parse the `module.exports` of a commonjs module."
5+
repository = "https://github.com/esm-dev/esm-cjs-lexer"
66
license = "MIT"
77
edition = "2021"
88

99
[lib]
1010
crate-type = ["cdylib", "rlib"]
1111

12-
[features]
13-
default = ["console_error_panic_hook"]
14-
1512
[dependencies]
1613
anyhow = "1.0.86"
1714
indexmap = "2.2.6"
@@ -27,11 +24,8 @@ swc_ecma_visit = "0.99.1"
2724

2825
# wasm-bindgen
2926
# docs: https://rustwasm.github.io/docs/wasm-bindgen
30-
wasm-bindgen = {version = "0.2.92", features = ["serde-serialize"]}
27+
wasm-bindgen = { version = "0.2.92", features = ["serde-serialize"] }
3128
serde-wasm-bindgen = "0.6.5"
32-
console_error_panic_hook = { version = "0.1.7", optional = true }
33-
# We get build error without this
34-
# https://docs.rs/getrandom/latest/getrandom/#webassembly-support
3529
getrandom = { version = "0.2.15", features = ["js"] }
3630

3731
[profile.release]

src/error.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use std::{fmt, sync::Arc, sync::RwLock};
1+
use std::fmt;
2+
use std::sync::{Arc, RwLock};
23
use swc_common::{
34
errors::{Diagnostic, DiagnosticBuilder, Emitter},
45
Loc, Span,

src/lib.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ mod test;
55

66
use serde::{Deserialize, Serialize};
77
use swc::SWC;
8-
use wasm_bindgen::prelude::{wasm_bindgen, JsValue};
8+
use wasm_bindgen::prelude::*;
99

1010
#[derive(Deserialize)]
1111
#[serde(deny_unknown_fields, rename_all = "camelCase")]
@@ -23,25 +23,22 @@ pub struct Output {
2323

2424
#[wasm_bindgen(js_name = "parse")]
2525
pub fn parse(specifier: &str, code: &str, options: JsValue) -> Result<JsValue, JsValue> {
26-
console_error_panic_hook::set_once();
27-
28-
let options: Options = serde_wasm_bindgen::from_value(options).unwrap_or(Options{
26+
let options: Options = serde_wasm_bindgen::from_value(options).unwrap_or(Options {
2927
node_env: None,
3028
call_mode: None,
3129
});
32-
let swc = SWC::parse(specifier, code).expect("could not parse module");
30+
let ret = match SWC::parse(specifier, code) {
31+
Ok(ret) => ret,
32+
Err(e) => {
33+
return Err(JsError::new(&e.to_string()).into());
34+
}
35+
};
3336
let node_env = if let Some(env) = options.node_env {
3437
env
3538
} else {
3639
"production".to_owned()
3740
};
3841
let call_mode = if let Some(ok) = options.call_mode { ok } else { false };
39-
let (exports, reexports) = swc.parse_cjs_exports(node_env.as_str(), call_mode).unwrap();
40-
Ok(
41-
serde_wasm_bindgen::to_value(&Output {
42-
exports: exports,
43-
reexports: reexports,
44-
})
45-
.unwrap(),
46-
)
42+
let (exports, reexports) = ret.parse_cjs_exports(&node_env, call_mode).unwrap();
43+
Ok(serde_wasm_bindgen::to_value(&Output { exports, reexports }).unwrap())
4744
}

src/swc.rs

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::cjs::CJSLexer;
22
use crate::error::{DiagnosticBuffer, ErrorBuffer};
33

44
use indexmap::{IndexMap, IndexSet};
5-
use std::{path::Path, rc::Rc};
5+
use std::path::Path;
66
use swc_common::{
77
comments::SingleThreadedComments,
88
errors::{Handler, HandlerFlags},
@@ -16,10 +16,7 @@ use swc_ecmascript::{
1616
};
1717

1818
pub struct SWC {
19-
pub specifier: String,
2019
pub module: Module,
21-
pub source_map: Rc<SourceMap>,
22-
pub comments: SingleThreadedComments,
2320
}
2421

2522
impl SWC {
@@ -29,7 +26,7 @@ impl SWC {
2926
let source_file = source_map.new_source_file(FileName::Real(Path::new(specifier).to_path_buf()), source.into());
3027
let sm = &source_map;
3128
let error_buffer = ErrorBuffer::new(specifier);
32-
let syntax = Syntax::Es(get_es_config());
29+
let syntax = Syntax::Es(EsConfig::default());
3330
let input = StringInput::from(&*source_file);
3431
let comments = SingleThreadedComments::default();
3532
let lexer = Lexer::new(syntax, EsVersion::Es2020, input, Some(&comments));
@@ -51,42 +48,31 @@ impl SWC {
5148
})
5249
.unwrap();
5350

54-
Ok(SWC {
55-
specifier: specifier.into(),
56-
module,
57-
source_map: Rc::new(source_map),
58-
comments,
59-
})
51+
Ok(SWC { module })
6052
}
6153

62-
/// parse export names in the cjs module.
54+
/// get named exports and reexports of the module.
6355
pub fn parse_cjs_exports(
6456
&self,
6557
node_env: &str,
6658
call_mode: bool,
6759
) -> Result<(Vec<String>, Vec<String>), anyhow::Error> {
6860
let mut lexer = CJSLexer {
61+
call_mode,
6962
node_env: node_env.to_owned(),
70-
call_mode: call_mode,
7163
fn_returned: false,
7264
idents: IndexMap::new(),
7365
exports_alias: IndexSet::new(),
74-
exports: IndexSet::new(),
66+
named_exports: IndexSet::new(),
7567
reexports: IndexSet::new(),
7668
};
7769
let program = Program::Module(self.module.clone());
7870
program.fold_with(&mut lexer);
7971
Ok((
80-
lexer.exports.into_iter().collect(),
72+
lexer.named_exports.into_iter().collect(),
8173
lexer.reexports.into_iter().collect(),
8274
))
8375
}
8476
}
8577

86-
fn get_es_config() -> EsConfig {
87-
EsConfig {
88-
import_attributes: true,
89-
jsx: false,
90-
..EsConfig::default()
91-
}
92-
}
78+

0 commit comments

Comments
 (0)