ES Module Lexer——模具行业从业者必备知识手册
-
<h2>ES Module Lexer</h2>
<p>[![Build Status][actions-image]][actions-url]</p>
<p>A JS module syntax lexer used in <a href="https://github.com/guybedford/es-module-shims">es-module-shims</a>.</p>
<p>Outputs the list of exports and locations of import specifiers, including dynamic import and import meta handling.</p>
<p>Supports new syntax features including import attributes and source phase imports.</p>
<p>A very small single JS file (4KiB gzipped) that includes inlined Web Assembly for very fast source analysis of ECMAScript module syntax only.</p>
<p>For 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.</p>
<p>Comprehensively handles the JS language grammar while remaining small and fast. - ~10ms per MB of JS cold and ~5ms per MB of JS warm, <a href="#benchmarks">see benchmarks</a> for more info.</p>
<blockquote>
<p><a href="https://github.com/guybedford/es-module-lexer/blob/main/chompfile.toml">Built with</a> <a href="https://chompbuild.com/">Chomp</a></p>
</blockquote><h4>Usage</h4>
<pre><code>
npm install es-module-lexer
</code></pre><p>See <a href="types/lexer.d.ts">types/lexer.d.ts</a> for the type definitions.</p>
<p>For use in CommonJS:</p>
<pre><code>
const { init, parse } = require('es-module-lexer');(async () => {
// either await init, or call parse asynchronously
// this is necessary for the Web Assembly boot
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);
})();
</code></pre><p>An ES module version is also available:</p>
<pre><code>
import { init, parse } from 'es-module-lexer';(async () => {
await init;const source = `
import { name } from 'mod\u1011';
import json from './json.json' assert { type: 'json' }
export var p = 5;
export function q () {}; export { x as 'external name' } from 'external'; // Comments provided to demonstrate edge cases import /*comment!*/ ( 'asdf', { assert: { type: 'json' }}); import /*comment!*/.meta.asdf; // Source phase imports: import source mod from './mod.wasm'; import.source('./mod.wasm);`;
const [imports, exports] = parse(source, 'optional-sourcename');
// Returns "modထ"
imports[0].n
// Returns "mod\u1011"
source.slice(imports[0].s, imports[0].e);
// "s" = start
// "e" = end// Returns "import { name } from 'mod'"
source.slice(imports[0].ss, imports[0].se);
// "ss" = statement start
// "se" = statement end// Returns "{ type: 'json' }"
source.slice(imports[1].a, imports[1].se);
// "a" = assert, -1 for no assertion// Returns "external"
source.slice(imports[2].s, imports[2].e);// Returns "p"
source.slice(exports[0].s, exports[0].e);
// Returns "p"
source.slice(exports[0].ls, exports[0].le);
// Returns "q"
source.slice(exports[1].s, exports[1].e);
// Returns "q"
source.slice(exports[1].ls, exports[1].le);
// Returns "'external name'"
source.slice(exports[2].s, exports[2].e);
// Returns -1
exports[2].ls;
// Returns -1
exports[2].le;// Import type is provided by
tvalue
// (1 for static, 2, for dynamic)
// Returns true
imports[2].t == 2;// Returns "asdf" (only for string literal dynamic imports)
imports[2].n
// Returns "import /comment!/ ( 'asdf', { assert: { type: 'json' } })"
source.slice(imports[3].ss, imports[3].se);
// Returns "'asdf'"
source.slice(imports[3].s, imports[3].e);
// Returns "( 'asdf', { assert: { type: 'json' } })"
source.slice(
哪个方案更实用?欢迎投票讨论。
</code></pre>