es-module-lexer
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
The es-module-lexer is a JavaScript library that focuses on lexing ES modules and returning their import/export metadata. It is primarily used in es-module-shims, and has been designed to be compact and rapid. The package uses an inlined Web Assembly for a very rapid analysis of ECMAScript module syntax. A useful comparison of its efficiency is that Angular 1 (720KiB) is completely parsed just in a fraction of time (5ms) as compared to over 100ms taken by Acorn, which is considered to be one of the fastest JavaScript parsers.
To use this library, you first need to install it via the npm package manager using npm install es-module-lexer
. The usage varies depending on whether you're working with CommonJS or an ES module.
For CommonJS, the code looks like:
const { init, parse } = require('es-module-lexer');
(async () => {
await init;
const source = 'export var p = 5';
const [imports, exports] = parse(source);
// Returns "p"
source.slice(exports[0].s, exports[0].e);
// Returns "p"
source.slice(exports[0].ls, exports[0].le);
})();
For an ES module version, the sample usage code can be like the following:
import { init, parse } from 'es-module-lexer';
(async () => {
await init;
const source = `...`; // put your source here
const [imports, exports] = parse(source, 'optional-sourcename');
// Here, you can manipulate the parsed imports and exports
})();
The documentation for the es-module-lexer can be found in the README file of its GitHub repository. As the repository is hosted at https://github.com/guybedford/es-module-lexer
, you can find the README at the root of this project. The README provides a comprehensive insight into the usage, environment support, grammar support, and other details related to the es-module-lexer package. This README serves as the primary source of documentation for the package.