Skip to content

Commit 9e27b4d

Browse files
committed
Change npm package to ESM
1 parent 6f38969 commit 9e27b4d

File tree

8 files changed

+77
-49
lines changed

8 files changed

+77
-49
lines changed

Cargo.lock

Lines changed: 11 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: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,15 @@
22
members = ["native", "lexer", "wasm"]
33
resolver = "2"
44

5-
[profile.release.package.cjs-module-lexer]
6-
# optimization for size (more aggressive)
7-
opt-level = "z"
8-
95
[profile.release]
6+
codegen-units = 1
107
debug = false
11-
# optimization for size (more aggressive)
8+
incremental = false
9+
lto = true
1210
opt-level = 3
13-
# less code to include into binary
1411
panic = "abort"
1512
strip = "symbols"
16-
# optimization over all codebase (better optimization, slower build)
17-
codegen-units = 1
18-
# link time optimization using using whole-program analysis
19-
lto = true
20-
incremental = false
13+
14+
[profile.release.package.wasm]
15+
# optimization for size (more aggressive)
16+
opt-level = "z"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ npm i @esm.sh/cjs-module-lexer
1515
cjs-module-lexer provides a `parse` function that detects the `module.exports` of a commonjs module. The function returns an object with two properties: `exports` and `reexports`. The `exports` property is an array of the exported names, and the `reexports` property is an array of the reexported modules.
1616

1717
```js
18-
const { parse } = require("@esm.sh/cjs-module-lexer");
18+
import { parse } from "@esm.sh/cjs-module-lexer";
1919

2020
// named exports by assignment
2121
// exports: ["a", "b", "c", "__esModule", "foo"]

wasm/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "cjs-module-lexer"
2+
name = "wasm"
33
version = "0.0.0"
44
edition = "2021"
55

wasm/README.md

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@
22

33
A lexer for detecting the `module.exports` of a CJS module, written in Rust.
44

5-
## Usage
5+
## Installation
66

7-
cjs-module-lexer currently only supports Node.js environment. You can install it via npm CLI:
7+
You can install cjs-module-lexer via npm CLI:
88

99
```bash
1010
npm i @esm.sh/cjs-module-lexer
1111
```
1212

13+
## Usage
14+
1315
cjs-module-lexer provides a `parse` function that detects the `module.exports` of a commonjs module. The function returns an object with two properties: `exports` and `reexports`. The `exports` property is an array of the exported names, and the `reexports` property is an array of the reexported modules.
1416

1517
```js
16-
const { parse } = require("@esm.sh/cjs-module-lexer");
18+
import { parse } from "@esm.sh/cjs-module-lexer";
1719

1820
// named exports by assignment
1921
// exports: ["a", "b", "c", "__esModule", "foo"]
@@ -144,19 +146,3 @@ export function parse(
144146
reexports: string[],
145147
};
146148
```
147-
148-
## Development Setup
149-
150-
You will need [rust](https://www.rust-lang.org/tools/install) 1.56+ and [wasm-pack](https://rustwasm.github.io/wasm-pack/installer/).
151-
152-
## Build
153-
154-
```bash
155-
wasm-pack build --target nodejs
156-
```
157-
158-
## Run tests
159-
160-
```bash
161-
cargo test --all
162-
```

wasm/index.mjs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { initSync, parse as __wbg_parse } from "./pkg/cjs-module-lexer.js";
2+
const wasmPath = "./pkg/cjs-module-lexer_bg.wasm";
3+
4+
let wasm;
5+
if (globalThis.process || globalThis.Deno || globalThis.Bun) {
6+
const { readFileSync } = await import("node:fs");
7+
const url = new URL(wasmPath, import.meta.url);
8+
const wasmData = readFileSync(url.pathname);
9+
wasm = await WebAssembly.compile(wasmData);
10+
} else {
11+
const url = new URL(wasmPath, import.meta.url);
12+
const pkgPrefix = "/@esm.sh/cjs-module-lexer@";
13+
if (url.pathname.startsWith(pkgPrefix)) {
14+
const version = url.pathname.slice(pkgPrefix.length).split("/", 1)[0];
15+
url.pathname = pkgPrefix + version + wasmPath.slice(1);
16+
}
17+
const res = await fetch(url);
18+
if (!res.ok) {
19+
throw new Error(`failed to fetch wasm: ${res.statusText}`);
20+
}
21+
wasm = await WebAssembly.compileStreaming(res);
22+
}
23+
24+
initSync({ module: wasm });
25+
26+
/**
27+
* parse the given code and return the exports and reexports
28+
* @param {string} filename
29+
* @param {string} code
30+
* @param {{ nodeEnv?: 'development' | 'production', callMode?: boolean }} options
31+
* @returns {{ exports: string[], reexports: string[] }}
32+
*/
33+
export function parse(filename, code, options = {}) {
34+
return __wbg_parse(filename, code, options);
35+
}

wasm/package.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
"name": "@esm.sh/cjs-module-lexer",
33
"description": "A lexer for detecting the `module.exports` of a CJS module.",
44
"version": "1.0.2",
5-
"main": "./pkg/cjs_module_lexer.js",
6-
"types": "./pkg/cjs_module_lexer.d.ts",
5+
"type": "module",
6+
"main": "index.mjs",
77
"scripts": {
8-
"prepublishOnly": "wasm-pack build --target nodejs"
8+
"prepublishOnly": "wasm-pack build --target web --out-name cjs-module-lexer",
9+
"test": "node test.mjs"
910
},
1011
"files": [
11-
"./pkg/cjs_module_lexer_bg.wasm",
12-
"./pkg/cjs_module_lexer.js",
13-
"./pkg/cjs_module_lexer.d.ts"
12+
"./index.mjs",
13+
"./pkg/cjs-module-lexer_bg.wasm",
14+
"./pkg/cjs-module-lexer.js"
1415
],
1516
"repository": {
1617
"type": "git",

wasm/test.mjs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { parse } from "./index.mjs";
2+
3+
console.log(parse("test.cjs", `
4+
exports.a = "a";
5+
module.exports.b = "b";
6+
Object.defineProperty(exports, "c", { value: 1 });
7+
Object.defineProperty(module.exports, "__esModule", { value: true })
8+
const key = "foo"
9+
Object.defineProperty(exports, key, { value: "e" });
10+
`));

0 commit comments

Comments
 (0)