UNPKG

5 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 "import { a } from 'asdf';"
62 source.substring(imports[0].ss, imports[0].se);
63
64 // Returns "p,q"
65 exports.toString();
66
67 // Dynamic imports are indicated by imports[1].d > -1
68 // In this case the "d" index is the start of the dynamic import
69 // Returns true
70 imports[1].d > -1;
71
72 // Returns "'asdf'"
73 source.substring(imports[1].s, imports[1].e);
74 // Returns "import /*comment!*/ ("
75 source.substring(imports[1].d, imports[1].s);
76
77 // import.meta is indicated by imports[2].d === -2
78 // Returns true
79 imports[2].d === -2;
80 // Returns "import /*comment!*/.meta"
81 source.substring(imports[2].s, imports[2].e);
82})();
83```
84
85### Environment Support
86
87Node.js 10+, and [all browsers with Web Assembly support](https://caniuse.com/#feat=wasm).
88
89### Grammar Support
90
91* Token state parses all line comments, block comments, strings, template strings, blocks, parens and punctuators.
92* Division operator / regex token ambiguity is handled via backtracking checks against punctuator prefixes, including closing brace or paren backtracking.
93* Always correctly parses valid JS source, but may parse invalid JS source without errors.
94
95### Limitations
96
97The lexing approach is designed to deal with the full language grammar including RegEx / division operator ambiguity through backtracking and paren / brace tracking.
98
99The only limitation to the reduced parser is that the "exports" list may not correctly gather all export identifiers in the following edge cases:
100
101```js
102// Only "a" is detected as an export, "q" isn't
103export var a = 'asdf', q = z;
104
105// "b" is not detected as an export
106export var { a: b } = asdf;
107```
108
109The above cases are handled gracefully in that the lexer will keep going fine, it will just not properly detect the export names above.
110
111### Benchmarks
112
113Benchmarks can be run with `npm run bench`.
114
115Current results:
116
117```
118Cold Run, All Samples
119test/samples/*.js (3057 KiB)
120> 24ms
121
122Warm Runs (average of 25 runs)
123test/samples/angular.js (719 KiB)
124> 5.12ms
125test/samples/angular.min.js (188 KiB)
126> 3.04ms
127test/samples/d3.js (491 KiB)
128> 4.08ms
129test/samples/d3.min.js (274 KiB)
130> 2.04ms
131test/samples/magic-string.js (34 KiB)
132> 0ms
133test/samples/magic-string.min.js (20 KiB)
134> 0ms
135test/samples/rollup.js (902 KiB)
136> 5.92ms
137test/samples/rollup.min.js (429 KiB)
138> 3.08ms
139
140Warm Runs, All Samples (average of 25 runs)
141test/samples/*.js (3057 KiB)
142> 17.4ms
143```
144
145### Building
146
147To build download the WASI SDK from https://github.com/CraneStation/wasi-sdk/releases.
148
149The 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.
150
151The 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`.
152
153On Windows it may be preferable to use the Linux subsystem.
154
155After the Web Assembly build, the CJS build can be triggered via `npm run build`.
156
157Optimization passes are run with [Binaryen](https://github.com/WebAssembly/binaryen) prior to publish to reduce the Web Assembly footprint.
158
159### License
160
161MIT
162
163[travis-url]: https://travis-ci.org/guybedford/es-module-lexer
164[travis-image]: https://travis-ci.org/guybedford/es-module-lexer.svg?branch=master
\No newline at end of file