Gathering detailed insights and metrics for cjs-module-lexer-rs
Gathering detailed insights and metrics for cjs-module-lexer-rs
Gathering detailed insights and metrics for cjs-module-lexer-rs
Gathering detailed insights and metrics for cjs-module-lexer-rs
npm install cjs-module-lexer-rs
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
5 Stars
14 Commits
2 Watching
1 Branches
1 Contributors
Updated on 05 Jun 2024
Rust (98.15%)
TypeScript (1.08%)
JavaScript (0.77%)
Cumulative downloads
Total Downloads
Last day
-66.7%
2
Compared to previous day
Last week
112.5%
17
Compared to previous week
Last month
300%
28
Compared to previous month
Last year
-66.3%
304
Compared to previous year
This is a rewrite of cjs-module-lexer in Rust. It is a CommonJS lexer used to detect the most likely list of named exports of a CommonJS module.
npm i cjs-module-lexer-rs
yarn add cjs-module-lexer-rs
pnpm add cjs-module-lexer-rs
1import { init, parse } from "https://esm.sh/cjs-module-lexer-rs";
Coming soon...
1// example.js 2const { init, parse } = require("cjs-module-lexer-rs"); 3 4const code = ` 5 module.exports.asdf = 'asdf'; 6 exports = 'asdf'; 7 module.exports = require('./asdf'); 8 if (maybe) 9 module.exports = require("./another"); 10`; 11 12init().then(() => console.log(parse(code, 'filename.js')));
1{ 2 imports: [ './asdf', './another'], 3 exports: [ 'asdf' ], 4 reexports: [ './another' ], 5 errors: [] 6}
1<!DOCTYPE html> 2<html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 6 <title>Lexer Example</title> 7 </head> 8 <body> 9 <script type="module"> 10 import { init, parse } from "https://esm.sh/cjs-module-lexer-rs"; 11 12 const code = ` 13 module.exports.asdf = 'asdf'; 14 exports = 'asdf'; 15 module.exports = require('./asdf'); 16 if (maybe) 17 module.exports = require("./another"); 18 `; 19 20 init().then(() => console.log(parse(code, 'filename.js'))); 21 </script> 22 </body> 23</html>
1{ 2 "imports": [ 3 "./asdf", 4 "./another", 5 ], 6 "exports": [ 7 "asdf" 8 ], 9 "reexports": [ 10 "./another" 11 ], 12 "errors": [] 13}
The frontend tooling has migrated to usage of moderm native languages to accelerate development and improve developer experience. cjs-module-lexer
remains useful in many scenes, but it assumes the code is UTF16 and only allows single-thread. Making cjs-module-lexer
working with Rust requires FFI and unsafe code. Hopefully this Rust library will be useful for Rust tooling authors to interop between CJS and ESM.
Feature | Status | Since | Note |
---|---|---|---|
exports.asdf = x | 👌 | 0.1.0 | |
exports['asdf'] = x | 👌 | 0.1.0 | |
module.exports = { ... } | 👌 | 0.1.0 | { ... } is like { a, b, c: d } , where d is Literal or Identifier |
require('module') | 👌 | 0.1.0 | |
Object.defineProperty(exports, 'q', { enumerable: true, get() { return q } }) | 👷 | TypeScript: export {colorFactory} from './color-factory'; | |
__export , __exportStar | 👷 | TypeScript: export * from 'external' | |
Skip StringLiteral | 👌 | 0.1.0 | |
Skip RegularExpressionLiteral | 👌 | 0.1.0 | |
Skip Template | 👌 | 0.1.0 | |
Non-unicode Named Export | ❌ | Not supported due to std::str only allows unicode strings |
https://babeljs.io/docs/en/babel-plugin-transform-modules-commonjs
cargo bench
test tests::bench_angular ... bench: 5,062,561 ns/iter (+/- 274,023)
test tests::bench_angular_min ... bench: 2,123,690 ns/iter (+/- 110,055)
test tests::bench_d3 ... bench: 3,066,050 ns/iter (+/- 1,230,751)
test tests::bench_d3_min ... bench: 1,786,949 ns/iter (+/- 686,781)
test tests::bench_magic_string ... bench: 203,876 ns/iter (+/- 28,393)
test tests::bench_magic_string_min ... bench: 134,005 ns/iter (+/- 9,085)
test tests::bench_rollup ... bench: 3,754,485 ns/iter (+/- 196,298)
test tests::bench_rollup_min ... bench: 2,581,948 ns/iter (+/- 144,913)
No vulnerabilities found.
No security vulnerabilities found.