UNPKG

7.25 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:
37
38```js
39import { init, parse } from 'es-module-lexer';
40
41(async () => {
42 await init;
43
44 const source = `
45 import { name } from 'mod';
46 import json from './json.json' assert { type: 'json' }
47 export var p = 5;
48 export function q () {
49
50 };
51
52 // Comments provided to demonstrate edge cases
53 import /*comment!*/ ('asdf', { assert: { type: 'json' }});
54 import /*comment!*/.meta.asdf;
55 `;
56
57 const [imports, exports] = parse(source, 'optional-sourcename');
58
59 // Returns "mod"
60 imports[0].n
61 source.substring(imports[0].s, imports[0].e);
62 // "s" = start
63 // "e" = end
64
65 // Returns "import { name } from 'mod'"
66 source.substring(imports[0].ss, imports[0].se);
67 // "ss" = statement start
68 // "se" = statement end
69
70 // Returns "{ type: 'json' }"
71 source.substring(imports[1].a, imports[1].se);
72 // "a" = assert
73
74 // Returns "p,q"
75 exports.toString();
76
77 // Dynamic imports are indicated by imports[2].d > -1
78 // In this case the "d" index is the start of the dynamic import
79 // Returns true
80 imports[2].d > -1;
81
82 // Returns "asdf"
83 imports[2].n
84 // Returns "'asdf'"
85 source.substring(imports[2].s, imports[2].e);
86 // Returns "import /*comment!*/ ("
87 source.substring(imports[2].d, imports[2].s);
88 // Returns "import /*comment!*/ ('asdf', { assert: { type: 'json' } })"
89 source.substring(imports[2].d, imports[2].se + 1);
90 // Returns "{ assert: { type: 'json' } }"
91 source.substring(imports[2].a, imports[2].e);
92 // ss is the same as d
93 // as, ae not used for dynamic imports
94
95 // import.meta is indicated by imports[2].d === -2
96 // Returns true
97 imports[2].d === -2;
98 // Returns "import /*comment!*/.meta"
99 source.substring(imports[2].s, imports[2].e);
100})();
101```
102
103### CSP asm.js Build
104
105The default version of the library uses Wasm and (safe) eval usage for performance and a minimal footprint.
106
107Neither of these represent security escalation possibilities since there are no execution string injection vectors, but that can still violate existing CSP policies for applications.
108
109For a version that works with CSP eval disabled, use the `es-module-lexer/js` build:
110
111```js
112import { parse } from 'es-module-lexer/js';
113```
114
115Instead of Web Assembly, this uses an asm.js build which is almost as fast as the Wasm version ([see benchmarks below](#benchmarks)).
116
117### Escape Sequences
118
119To handle escape sequences in specifier strings, the `.n` field of imported specifiers will be provided where possible.
120
121For dynamic import expressions, this field will be empty if not a valid JS string.
122
123### Facade Detection
124
125Facade modules that only use import / export syntax can be detected via the third return value:
126
127```js
128const [,, facade] = parse(`
129 export * from 'external';
130 import * as ns from 'external2';
131 export { a as b } from 'external3';
132 export { ns };
133`);
134facade === true;
135```
136
137### Environment Support
138
139Node.js 10+, and [all browsers with Web Assembly support](https://caniuse.com/#feat=wasm).
140
141### Grammar Support
142
143* Token state parses all line comments, block comments, strings, template strings, blocks, parens and punctuators.
144* Division operator / regex token ambiguity is handled via backtracking checks against punctuator prefixes, including closing brace or paren backtracking.
145* Always correctly parses valid JS source, but may parse invalid JS source without errors.
146
147### Limitations
148
149The lexing approach is designed to deal with the full language grammar including RegEx / division operator ambiguity through backtracking and paren / brace tracking.
150
151The only limitation to the reduced parser is that the "exports" list may not correctly gather all export identifiers in the following edge cases:
152
153```js
154// Only "a" is detected as an export, "q" isn't
155export var a = 'asdf', q = z;
156
157// "b" is not detected as an export
158export var { a: b } = asdf;
159```
160
161The above cases are handled gracefully in that the lexer will keep going fine, it will just not properly detect the export names above.
162
163### Benchmarks
164
165Benchmarks can be run with `npm run bench`.
166
167Current results for a high spec machine:
168
169#### Wasm Build
170
171```
172Module load time
173> 5ms
174Cold Run, All Samples
175test/samples/*.js (3123 KiB)
176> 20ms
177
178Warm Runs (average of 25 runs)
179test/samples/angular.js (739 KiB)
180> 2.12ms
181test/samples/angular.min.js (188 KiB)
182> 1ms
183test/samples/d3.js (508 KiB)
184> 3.04ms
185test/samples/d3.min.js (274 KiB)
186> 2ms
187test/samples/magic-string.js (35 KiB)
188> 0ms
189test/samples/magic-string.min.js (20 KiB)
190> 0ms
191test/samples/rollup.js (929 KiB)
192> 4.04ms
193test/samples/rollup.min.js (429 KiB)
194> 2.16ms
195
196Warm Runs, All Samples (average of 25 runs)
197test/samples/*.js (3123 KiB)
198> 14.4ms
199```
200
201#### JS Build (asm.js)
202
203```
204Module load time
205> 2ms
206Cold Run, All Samples
207test/samples/*.js (3123 KiB)
208> 35ms
209
210Warm Runs (average of 25 runs)
211test/samples/angular.js (739 KiB)
212> 3ms
213test/samples/angular.min.js (188 KiB)
214> 1.08ms
215test/samples/d3.js (508 KiB)
216> 3.04ms
217test/samples/d3.min.js (274 KiB)
218> 2ms
219test/samples/magic-string.js (35 KiB)
220> 0ms
221test/samples/magic-string.min.js (20 KiB)
222> 0ms
223test/samples/rollup.js (929 KiB)
224> 5.04ms
225test/samples/rollup.min.js (429 KiB)
226> 3ms
227
228Warm Runs, All Samples (average of 25 runs)
229test/samples/*.js (3123 KiB)
230> 17ms
231```
232
233### Building
234
235To build download the WASI SDK from https://github.com/WebAssembly/wasi-sdk/releases.
236
237The Makefile assumes the existence of "wasi-sdk-11.0" and "wabt" (optional) as sibling folders to this project.
238
239The 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`.
240
241On Windows it may be preferable to use the Linux subsystem.
242
243After the Web Assembly build, the CJS build can be triggered via `npm run build`.
244
245### License
246
247MIT
248
249[travis-url]: https://travis-ci.org/guybedford/es-module-lexer
250[travis-image]: https://travis-ci.org/guybedford/es-module-lexer.svg?branch=master
251