UNPKG

4.81 kBMarkdownView Raw
1# ES Module Lexer
2
3[![Build Status][travis-image]][travis-url]
4
5A JS module syntax lexer used in [es-module-shims](https://github.com/guybedford/es-module-shims).
6
7Outputs the list of exports and locations of import specifiers, including dynamic import and import meta handling.
8
9A very small single JS file (4KiB gzipped) that includes inlined Web Assembly for very fast source analysis of ECMAScript module syntax only.
10
11For an example of the performance, Angular 1 (720KiB) is fully parsed in 5ms, in comparison to the fastest JS parser, Acorn which takes over 100ms.
12
13_Comprehensively handles the JS language grammar while remaining small and fast - ~10ms per MB of JS cold and ~5ms per MB of JS warm, [see benchmarks](#benchmarks) for more info._
14
15### Usage
16
17```
18npm install es-module-lexer
19```
20
21For use in CommonJS:
22
23```js
24const { init, parse } = require('es-module-lexer');
25
26(async () => {
27 // either await init, or call parse asynchronously
28 // this is necessary for the Web Assembly boot
29 await init;
30
31 const [imports, exports] = parse('export var p = 5');
32 exports[0] === 'p';
33})();
34```
35
36An ES module version is also available from `dist/es-module-lexer.js`:
37
38```js
39import { init, parse } from 'es-module-lexer/dist/es-module-lexer.js';
40
41(async () => {
42 await init;
43
44 const source = `
45 import { a } from 'asdf';
46 export var p = 5;
47 export function q () {
48
49 };
50
51 // Comments provided to demonstrate edge cases
52 import /*comment!*/ ('asdf');
53 import /*comment!*/.meta.asdf;
54 `;
55
56 const [imports, exports] = parse(source, 'optional-sourcename');
57
58 // Returns "asdf"
59 source.substring(imports[0].s, imports[0].e);
60
61 // Returns "p,q"
62 exports.toString();
63
64 // Dynamic imports are indicated by imports[1].d > -1
65 // In this case the "d" index is the start of the dynamic import
66 // Returns true
67 imports[1].d > -1;
68
69 // Returns "'asdf'"
70 source.substring(imports[1].s, imports[1].e);
71 // Returns "import /*comment!*/ ("
72 source.substring(imports[1].d, imports[1].s);
73
74 // import.meta is indicated by imports[2].d === -2
75 // Returns true
76 imports[2].d === -2;
77 // Returns "import /*comment!*/.meta"
78 source.substring(imports[2].s, imports[2].e);
79})();
80```
81
82### Environment Support
83
84Node.js 10+, and [all browsers with Web Assembly support](https://caniuse.com/#feat=wasm).
85
86### Grammar Support
87
88* Token state parses all line comments, block comments, strings, template strings, blocks, parens and punctuators.
89* Division operator / regex token ambiguity is handled via backtracking checks against punctuator prefixes, including closing brace or paren backtracking.
90
91### Limitations
92
93The lexing approach is designed to deal with the full language grammar including RegEx / division operator ambiguity through backtracking and paren / brace tracking.
94
95The only limitation to the reduced parser is that the "exports" list may not correctly gather all export identifiers in the following edge cases:
96
97```js
98// Only "a" is detected as an export, "q" isn't
99export var a = 'asdf', q = z;
100
101// "b" is not detected as an export
102export var { a: b } = asdf;
103```
104
105The above cases are handled gracefully in that the lexer will keep going fine, it will just not properly detect the export names above.
106
107### Benchmarks
108
109Benchmarks can be run with `npm run bench`.
110
111Current results:
112
113```
114Cold Run, All Samples
115test/samples/*.js (3057 KiB)
116> 24ms
117
118Warm Runs (average of 25 runs)
119test/samples/angular.js (719 KiB)
120> 5.12ms
121test/samples/angular.min.js (188 KiB)
122> 3.04ms
123test/samples/d3.js (491 KiB)
124> 4.08ms
125test/samples/d3.min.js (274 KiB)
126> 2.04ms
127test/samples/magic-string.js (34 KiB)
128> 0ms
129test/samples/magic-string.min.js (20 KiB)
130> 0ms
131test/samples/rollup.js (902 KiB)
132> 5.92ms
133test/samples/rollup.min.js (429 KiB)
134> 3.08ms
135
136Warm Runs, All Samples (average of 25 runs)
137test/samples/*.js (3057 KiB)
138> 17.4ms
139```
140
141### Building
142
143To build download the WASI SDK from https://github.com/CraneStation/wasi-sdk/releases.
144
145The Makefile assumes that the `clang` in PATH corresponds to LLVM 8 (provided by WASI SDK as well, or a standard clang 8 install can be used as well), and that `../wasi-sdk-6` contains the SDK as extracted above, which is important to locate the WASI sysroot.
146
147The build through the Makefile is then run via `make lib/lexer.wasm`, which can also be triggered via `npm run build-wasm` to create `dist/lexer.js`.
148
149On Windows it may be preferable to use the Linux subsystem.
150
151After the Web Assembly build, the CJS build can be triggered via `npm run build`.
152
153Optimization passes are run with [Binaryen](https://github.com/WebAssembly/binaryen) prior to publish to reduce the Web Assembly footprint.
154
155### License
156
157MIT
158
159[travis-url]: https://travis-ci.org/guybedford/es-module-lexer
160[travis-image]: https://travis-ci.org/guybedford/es-module-lexer.svg?branch=master
\No newline at end of file