Skip to content

Commit f5d7d1f

Browse files
authored
feat: Allow unknown module type (#11)
1 parent 8790c4e commit f5d7d1f

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

src/wasm.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,32 @@ use std::path::PathBuf;
33
use swc::config::IsModule;
44
use wasm_bindgen::prelude::*;
55

6+
#[cfg_attr(
7+
feature = "serde",
8+
derive(serde::Serialize, serde::Deserialize),
9+
serde(rename_all = "lowercase")
10+
)]
11+
#[cfg_attr(
12+
feature = "wasm",
13+
derive(tsify::Tsify),
14+
tsify(into_wasm_abi, from_wasm_abi)
15+
)]
16+
pub enum ModuleType {
17+
ESM,
18+
CJS,
19+
Unknown,
20+
}
21+
22+
impl From<ModuleType> for IsModule {
23+
fn from(value: ModuleType) -> Self {
24+
match value {
25+
ModuleType::ESM => IsModule::Bool(true),
26+
ModuleType::CJS => IsModule::Bool(false),
27+
ModuleType::Unknown => IsModule::Unknown,
28+
}
29+
}
30+
}
31+
632
#[wasm_bindgen]
733
pub struct InstrumentationMatcher(Instrumentor);
834

@@ -33,10 +59,9 @@ pub struct Transformer(InstrumentationVisitor);
3359
#[wasm_bindgen]
3460
impl Transformer {
3561
#[wasm_bindgen]
36-
pub fn transform(&mut self, contents: &str, is_module: bool) -> Result<String, JsError> {
37-
let is_module = IsModule::Bool(is_module);
62+
pub fn transform(&mut self, contents: &str, is_module: ModuleType) -> Result<String, JsError> {
3863
self.0
39-
.transform(contents, is_module)
64+
.transform(contents, is_module.into())
4065
.map_err(|e| JsError::new(&e.to_string()))
4166
}
4267
}

tests/wasm/tests.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ const matchedTransforms = instrumentor.getTransformer(
2929
assert.ok(matchedTransforms);
3030

3131
const original = await fs.readFile(path.join(import.meta.dirname, './testdata/original.mjs'))
32-
const output = matchedTransforms.transform(original.toString('utf8'), true);
32+
const output = matchedTransforms.transform(original.toString('utf8'), 'esm');
3333

3434
const expected = await fs.readFile(path.join(import.meta.dirname, './testdata/expected.mjs'))
3535
assert.strictEqual(output, expected.toString('utf8'));
3636

3737
const originalCjs = await fs.readFile(path.join(import.meta.dirname, './testdata/original-cjs.js'))
38-
const outputCjs = matchedTransforms.transform(originalCjs.toString('utf8'), false);
38+
const outputCjs = matchedTransforms.transform(originalCjs.toString('utf8'), 'cjs');
3939

4040

4141
const expectedCjs = await fs.readFile(path.join(import.meta.dirname, './testdata/expected-cjs.js'))
@@ -44,5 +44,5 @@ assert.strictEqual(outputCjs, expectedCjs.toString('utf8'));
4444
const noMatch = await fs.readFile(path.join(import.meta.dirname, './testdata/no-match.mjs'));
4545

4646
assert.throws(() => {
47-
matchedTransforms.transform(noMatch.toString('utf8'), true);
47+
matchedTransforms.transform(noMatch.toString('utf8'), 'unknown');
4848
}, { message: "Failed to find injection points for: [\"constructor\", \"fetch\"]" });

0 commit comments

Comments
 (0)